Python How to Use If-Else Statement

Sovary June 4, 2022 373
6 minutes read

In this article, I show you how to create decisions in a Python program using different forms of if-else statement and explain step by step with examples. In logically, in programming language decision making is required when we want condition to execute in certain code.

thumbnail_python_if_else_usage

Python is support logical from mathematic symbols

Symbols Meaning Example Explain
== Is Equal? 2 == 3 Is number 2 equal to 3 ? false
!= Is not equal? 2 != 3 Is number 2 not equal to 3 ? true
< Is less than? 4 < 5 Is number 4 less than 5? true
<= Is less than or equal to? 8 <= 9 Is number 8 less than or equal to 9 ? true
> Is greater than? 8 > 5 Is number 8 greater than 5 ? true
>= Is greater than or equal to? 5 >= 6 Is number 5 greater than or equal to 6 ? false

If Statment

"if statment" is written if keywords to check condition or expression and execute only true part.

Syntax

if condition :
  # to do code here if expression true

Example #1:

if 200 < 350 :
  print("correct 200 is less than 350")

Output #1:

correct 200 is less than 350

Explain #1:

In example #1 using two values to check, is 200 less than 350? for real it is true, we know 350 is greater than 200 so condition satisfied and execute print line "correct 200 is less than 350" as output.

Python identify indentation as its block statment to execute. See below example

Example #2:

num1 = 100
num2 = 50

if num2 < num1 :
  print("Correct! num2 is less than num1")
print("You always see me.")

if num2 > num1 :
  print("You never see me")

Output #2:

Correct! num2 is less than num1
You always see me.

Explain #2:

You can see example #2 we have written 3 print outputs but result only two print lines.

Line 4: The code execute from top to bottom in case num2 is holding value 50 and num1 is holding value 100 as first condition check is true (in fact 50 is always less than 100) so the result print "Correct! num2 is less than num1". 

Line 6: The code statment is out of if condition block statment (see no indentation under if statment), so it always print out not matter if num2 and num1 true or false, so the result print "You always see me".

Line 8: It is the same as Line 4 but we have changed logical symbol to greater than which is we know in if condition never satisfied as 50 never greater than 100 in real life.

If-else Statment

if-else statment is written in if keyword and else keyword, we apply this statment when we want both part execute with given condition. If the condition is true, if statment will be executed and if statment is false then else statment will be executed.

Syntax

if condition :
  #to do code when condition is true
else:
  #to do code when condition is false

Example #1:

num1 = 100
num2 = 50

if num2 > num1 :
  print("num2 is greater than num1")
else:
  print("num2 is less than num1")

print("This is end.")

Output #1:

num2 is less than num1
This is end.

Explain #1:

Line 4: Once the compiler executed, this line is never true (50 is not greater than 100), so the statment treat as false condition which is fall into else statment to be executed.

Line 9: The statment always print no matter if condition is true or false because it is stand out of condition section.

Elif Statment

elif keyword is used with if statment, we can use to test condition more than two conditions just like we say if condition A were not true then try condition B and if still not true then try condition C...
If one of condition satisfied, the remain conditions will never test check.

Syntax

if conditionA :
  #to do code when conditionA is true
elif conditionB :
  #to do code when conditionB is true
elif conditionC :
  #to do code when conditionC is true
else :
  #to do if all condition(A,B,C) are false

Example:

# get value from user input
alpha = input()

if alpha == "a" :
  print("You input a")
elif alpha == "b" :
  print("You input b")
elif alpha == "c" :
  print("You input c")
else :
  print("You input something none of abc")

Output:

You input b

NOTE: Output base on what you have input, I was input letter b.

Explain:

Line 2: Getting input value from user. Example I was input letter b.

Line 4: alpha is holding letter b now. Start test each condition to find matching value, and it is true on 3rd condition which is alpha equal to letter c. The result print out "You input c". After fall in one of condition, it will leave to check any remain condition.

Line 10: else block statment will be executed unless all above condition are true.

Shortcut if

If you have one if statement to execute, you can write in single line as below.

Example:

if 6 <= 6: print("6 is less than or equal 11")

Output:

6 is less than or equal 11

Shortcut if-else

for if-else statment we also can write in one line as well

Example:

print("if executed") if 33 > 2 else print("else executed")

Output:

if executed

Logical Operator

There are two logical operators AND , OR use for combine multiple conditions together.

Operator and

We use and operator to combine more than two conditions together. The combination condition true when all conditions are true, otherwise one condition false the combination condition will false as well.

In short, and operator is true when all conditions are true.

Example #1

num1 = 3
num2 = 5
num3 = 0

if num1 > num3 and num2 > num1 :
  print("Both condition are true")
else :
  print("One condition have false or both false")

Output #1:

Both condition are true

Explain #1:

The result is print in if condition because num1 is greater than num3  is true and num2 is greater than num1 is true, so all conditions are true then the combination condition is true as well.

Example #2

num1 = 3
num2 = 5
num3 = 0

if num1 > num3 and num2 < num1 :
  print("Both condition are true")
else :
  print("One condition have false or both false")

 Output #2:

One condition have false or both false

Explain #2:

I have changed the operator (num2 < num1), so one condition is false, then combination condition is false and it will fall into else condition.

Operator or

We use or operator to combine more than two conditions together. The combination condition true when one of condition is true, otherwise all conditions are false the combination condition will false as well.

In short, or operator is true when at least one of condition is true.

Example:

num1 = 3
num2 = 5
num3 = 0

if num1 > num3 or num2 < num1 :
  print("One condition or both is true")
else :
  print("Both conditions are false")

 Output:

One condition is true or both

Explain:

num1 is greater than num3 is true ? it is correct but num2 is not less than num1. For or operator is true when at least one condition is true. The result will print in if condition.

Nested if Statement

We can write if or if else condition inside other if else statement. The combination will help to solve different problem.

Example

num1 = 3
num2 = 5
num3 = 0

if num1 > num3 :
  if num2 > num1:
    print("Both condition are true")
else :
  print("One condition have false or both false")

Output:

Both condition are true

You will this example is similar to example #1 in and operator above. The both if conditions at line 5-6 is combination like and operator.

But becareful it is not completly the same, the above case will execute else once the num1 is less than num3 which mean not both statments are checked, because other condition statement is inside the first condition.

Hope you learn and help you to understand basic how to using if-else statement in Python.

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him