Skip to main content

Python Conditions


In this tutorial you will learn:

  • Conditional statements
  • Indentation in Python
  • If statement in Python
  • Else statement in Python
  • Elif statement in Python
  • Nested if statements in Python

Conditional statements

Conditional statements as the name suggests define some kind of condition before an action is taken. In terms of programming conditional statements constrains whether a part of code should be executed or not. These decision making statements are an integral part of any programming language as most of the time we don’t want all parts of our code to run at once rather we wan’t some parts to execute if certain conditions are satisfied.

Indentation in Python

Indentation is empty space before the code starts. It’s important to learn that in Python indentation matters a lot unlike other programming languages. In Python indentation indicates a block of code while in other programming languages it’s there just for code readability.

If statement in Python

This statement will only be executed if the result of a condition is true. It doesn’t execute in case of false condition.

For example:

  1. myVar1 = 45
  2. myVar2 = 88
  3. print("Output Result")
  4. if myVar1 > myVar2: #condition evaluates to false so won't execute
  5. print(“This statement won't be executed")
  6. if myVar1 < myVar2: #condition evaluates to true so will execute
  7. print("45 is less than 88")

Another good example of if statements is to use and, or and equality operator in if statement

  1. myVar3 = 98
  2. myVar4 = 98
  3.  
  4. print("\nOutput Result")
  5. if myVar3 == myVar4:
  6. print("Numbers are equal")
  7.  
  8. var1 = 3
  9. var2 = 5
  10. var3 = 9
  11.  
  12. print("\nUsing and in if")
  13. if var1 < var2 and var2 < var3:
  14. print(" 3 is less than 5 is less than 9")
  15.  
  16. print("\nUsing or in if")
  17. if var1 < var2 or var2 > var3:
  18. print(" 3 is less than 5 but 5 is not greater than 9")

Else statement in Python

This statement will only be executed if the result of a condition doesn’t satisfy any other condition. Note that in the code above the first print statement isn’t executed and in this case we could have used else statement to show the user the right output. So let’s re write the above program with else statement.

  1. myVar1 = 45
  2. myVar2 = 88
  3. print("Output Result")
  4. if myVar1 > myVar2: #condition evaluates to false so won't execute
  5. print("This statement won't be executed")
  6. else:
  7. print("45 is less than 88”)

The output using else statement is the same in this case. Another example is using multiple if statements and then using the else statement.

  1. a = 10
  2. b = 5
  3. print("\nOutput Result using else")
  4. if a == b:
  5. print("", a, "is less than", b)
  6. if a > b:
  7. print("", a, "is greater than", b)
  8. else:
  9. print("",a, "is equal to", b)

Elif statement in Python

This statement is commonly known as else if statement in most the programming languages. This statements is used to provide an alternate option to the if statement. If the if statement is false then go to else if and if it’s false as well then go to the next elif and then finally to else. It’s different from using subsequent if statements because elif code will only be executed if the condition above it evaluates to false. If we use multiple if statements instead then each if condition will be executed regardless whether the first condition is true or false.

  1. myVar1 = 45
  2. myVar2 = 88
  3. print("Output Result")
  4. if myVar1 > myVar2: #condition evaluates to false so won't execute
  5. print(myVar1, "is greater than ",myVar2)
  6. elif myVar1 < myVar2: #condition evaluates to true so will execute
  7. print(myVar1, "is less than ", myVar2)
  8. else:
  9. print(myVar1, "is equal to ", myVar2)

Nested if statements

Nested if statements are used when we want to satisfy multiple conditions subsequently after one condition is satisfied. You can do nesting in depth but it’s not recommended better approach is to use and, or and not operators to keep nesting level to a minimum.

  1. name = "Jack"
  2. age = 19
  3. print("Output Result")
  4. if age >= 18:
  5. if name == 'Jack':
  6. print('Yes you use this car')
  7. else:
  8. print("Sorry you can't use this car")
  9. print("\nAnother solution")
  10. if age >= 18 and name == 'Jack':
  11. print('Yes you use this car')
  12. else:
  13. print("Sorry you can't use this car")

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.