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.
Table of Contents
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:
Statement | Description |
---|---|
if Statements | It consists of a Boolean expression which results are either TRUE or FALSE, followed by one or more statements. |
if else Statements | It 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 Statements | We 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):
1 2 3 4 5 |
a = 42 b = 212 if b > a: print("b is greater than a") |
Click the Execute button, and let’s see it in action:
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):
1 2 3 4 5 6 |
number1 = 42 ; number2 = 70 if(number1 >= number2 ): print("Number 1 is greater than Number 2") else: print("Number 2 is greater than Number 1") |
Click the Execute button, and let’s see it in action:
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
print("Select your ride:") print("1. Bike") print("2. Motorcycle") print("3. Car") print("4. SUV") print("5. Train") choice = int(input()) if(choice == 1): print("You have selected Bike") elif( choice == 2 ): print("You have selected Motorcycle") elif( choice == 3 ): print("You have selected Car") elif( choice == 4 ): print("You have selected SUV") elif( choice == 5 ): print("You have selected Train") else: print("Wrong choice!") |
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:
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:
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.
1 2 3 4 5 6 7 8 9 10 |
num1 = int(input()) num2 = int(input()) if(num1 >= num2): if(num1 == num2): print(f'{num1} and {num2} are equal') else: print(f'{num1} is greater than {num2}') else: print(f'{num1} is smaller than {num2}') |
Click the Execute button, and let’s see it in action (input the first number):
Input the second number:
Output:
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.