Python Programming — Conditional Statement
Python conditional statements are streams that control Python program code based on conditional statement tests. Conditional statements can contain functions, mathematical operators and logical operators.

The syntax used to create control flow in the form of conditional statements in Python is if, elif, and else. Where if is the main condition, while elif is the second or third condition any condition, while else is the last condition if all conditions are not met.
IF Statement
If statement in Python is used to make decisions based on boolean conditions. When a boolean condition evaluates to true, the corresponding code block will be executed. If the condition evaluates to false it will be ignored immediately.
Example :
x = float (input ('Enter a number = '));if(x >= 10):
print ('Congratulations, the number you entered is correct')
In the program if we enter a number equal to ten or more than ten ( x >= 10) then the condition will be true, and will be executed and print an output.
Output :
Enter a number = 11Congratulations, the number you entered is correct
But if you enter a number less than ten (x<10) then the condition will be false and will not be executed or ignored.
Output :
Enter a number = 9#program not executed
ELSE Statement
else statement is used when we know that there is a possibility that the condition in the if statement will not be met. A condition where if the statement is true then the code in the if will be executed, but if the value is false then the code in the else will be executed
Example :
x = int(input('enter the value of x: '))
y = int(input('enter the value of y: '))if x < y:
print('x is less than y')
else:
print('x is more than y')print('x={}, y={}'.format(x, y))
Output :
enter the value of x: 5
enter the value of y: 2
x is more than y
x=5, y=2
In the program, because the value of x is greater than y, the conditions in the if statement are not met and are ignored. Then the program will immediately run the command in the else statement
But if the values of x and y are the same then the output will be incorrect.
Output :
enter the value of x: 5
enter the value of y: 5
x is more than y
x=5, y=5
In the program, x should be equal to y because they are both equal to 5. Why doesn’t this happen?
Because we haven’t told the computer about the output when the values for x and y are the same. For that we can use the elif statement.
ELIF Statement
The elif function is more or less the same as else combined with if. Just like else, when the conditions in the if statement are not met, it will proceed to the elif statement. The difference with else is, the elif statement will check a condition first before continuing to run the program code.
Example :
x = int(input('enter the value of x: '))
y = int(input('enter the value of y: '))if x < y :
print('x is less than y')
elif x > y :
print('x is more than y')
else :
print('x is equal to y')
print('x={}, y={}'.format(x, y))
Output :
enter the value of x: 5
enter the value of y: 5
x is equal to y
x=5, y=5
The program is already perfect after adding the elif condition. It can be seen that when entering the same x and y values, the elif condition is replaced by the else condition.
The following is a systematic if, elif, and else syntax in Python.
- The conditional statement starts from the first, when a statement is fulfilled, the statement command will be executed and the conditional statement will be terminated
- Conditional expressions start with the syntax if
- Conditional expression is a conditional statement that is tested which can contain mathematical operators, functions, and logic
- Each conditional expression ends with a colon :
- Every conditional expression has a command code in writing indents
- Conditional_expression_1 is the first conditional statement with the command command_1
- The second and so on conditional expressions start with the syntax elif
- Conditional_expression_2 is the second conditional statement with the command command_2
- Conditional_expression_3 is the third conditional statement with the command command_3
- Alternative conditions are written ending with the else syntax:
- alternative_command is an alternative command, used when all conditional conditions are not met
Chasier Program
Now we will make a cashier program for a burger sale, with the conditions
- if you buy it will display the number of items purchased and the total price of the item
- If after buying the remaining money will be displayed in the wallet
- If you can’t buy an item, it will show that you have less money
Example :
burger_price = 5
money = 20input_count = input('How many burgers do you want : ')
count = int(input_count)
total_price = burger_price * countprint('You will buy ' + str(count) + ' burgers')
print('Total price is ' + str(total_price) + ' dolars')
if money > total_price:
print('You have bought ' + str(count) + ' burgers')
print('your money is left ' + str(money - total_price) + ' dolar')elif money == total_price:
print('You have bought ' + str(count) + ' burgers')
print('your wallet is empty')else:
print('your money is not enough')
print('you cant buy that many burgers')
Output :
#if can buy a burgerHow many burgers do you want : 3
You will buy 3 burgers
Total price is 15 dolars
You have bought 3 burgers
your money is left 5 dolar#if can't buy a burger
How many burgers do you want : 10
You will buy 10 burgers
Total price is 50 dolars
your money is not enough
you cant buy that many burgers