DelphiLearn PythonPythonPython GUIRAD Studio

Easily Learn Conditions And Decision Making In Python GUI Apps Built In A Delphi

blog banner python conditions

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Conditions and Decision Making in Python GUI apps and gets the output.

Decisions in a program are used when the program has conditional choices to execute a code block. Let’s take an example of traffic lights, where different colors of lights lit up in different situations based on the conditions of the road or any specific rule. It is the prediction of conditions that occur while executing a program to specify actions. Multiple expressions get evaluated with an outcome of either TRUE or FALSE. These are logical decisions, and Python also provides decision-making statements that to make decisions within a program for an application based on the user requirement.

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub.

0 rundemo1
Open Demo01dproj

 

1. Python Conditions

Python supports the usual logical conditions adopted from Mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in “if statements” and loops.

An “if statement” is written by using the “if” keyword.

 

2. Python Decision Making using if Statements

Python provides various types of decision making statements:

StatementDescription
if StatementsIt consists of a Boolean expression which results are either TRUE or FALSE, followed by one or more statements.
if else StatementsIt also contains a Boolean expression. The if the statement is followed by an optional else statement & if the expression results in FALSE, then else statement gets executed. It is also called alternative execution in which there are two possibilities of the condition determined in which any one of them will get executed.
Nested if StatementsWe can implement if statement and or if-else statement inside another if or if – else statement. Here more than one if conditions are applied & there can be more than one if within elif.

The following implementations in Python GUI will make this concept clearer:

2.1. Python if Statement

if statement is the most simple form of decision-making statement. It takes an expression and checks if the expression evaluates to True then the block of code in if statement will be executed. If the expression evaluates to False, then the block of code is skipped.

Insert the following example to our Python GUI (lower Memo):

Click the Execute button, and let’s see it in action:  

1 pythonifstatement 8023943
Python if Statement using Python GUI from Python4Delphi

2.2. Python if-else Statement

The Python if-else statement checks the expression and executes the if block when the expression is True, otherwise it will execute the else block of code. The else block should be right after if block and it is executed when the expression is False.

Insert the following example to our Python GUI (lower Memo):

Click the Execute button, and let’s see it in action:   

2 pythonif elsestatement 1484812
Python if else Statement using Python GUI from Python4Delphi

2.3. Python if-elif Ladder

When other languages like C/C++ or Java have the else-if statements, in Python, we have an elif keyword to chain multiple conditions one after another.

With elif ladder, we can make complex decision-making statements. The elif statement helps you to check multiple expressions and it executes the code as soon as one of the conditions evaluates to True.

Insert the following example to our Python GUI (lower Memo):

The else keyword in this example catches anything which isn’t caught by the preceding conditions.

Click the Execute button, and let’s see it in action:

3 pythonif elifladder1 9283901
Python if elif Ladder using Python GUI from Python4Delphi

It’s amazing to learn how easy to make a program that expects your input, and showed it with a nice GUI, using Python4Delphi and RAD Studio!

Click “Execute script” to see the result:

3 pythonif elifladder2 3341785
Python if elif Ladder using Python GUI from Python4Delphi

2.4. Python Nested if Statement

The nested if statements is an if statement inside another if statement. Python allows us to stack any number of if statements inside the block of another if statements. They are useful when we need to make a series of decisions.

 Click the Execute button, and let’s see it in action (input the first number):

4 pythonnestedifstatement1 4510308
Python Nested if Statement using Python GUI from Python4Delphi

Input the second number:

4 pythonnestedifstatement2 2359782
Python Nested if Statement using Python GUI from Python4Delphi

Output:

4 pythonnestedifstatement3 7762548
Python Nested if Statement using Python GUI from Python4Delphi

That’s all! We learned how the Python Condition and Decision Making feature combined well with nice GUI created with Delphi (Python4Delphi).

From now, the applications are endless, as you might know that Conditions are the basis for creating AI or any Automatic Systems.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *