Basic Conditions
So far our code was very simple and had only one path of execution.
Every line of code got executed one after another.
A condition is something we can add to our code that will tell python which way to go if something is true or not.
Let’s look at a basic example:
user_age = int(input("Please Enter your Age: ")) if user_age < 21: print("You are not old enough to drink.") elif user_age > 130: print("Hey... you can't be %d years old..." % user_age) else: print("You are old enough to drink!") print("Goodbye")
Please Enter your Age: 37 You are old enough to drink! Goodbye
Please Enter your Age: 16 You are not old enough to drink. Goodbye
Please Enter your Age: 200 Hey... you can't be 200 years old... Goodbye
There are several new things in this code.
- if user_age < 21:
- This is the condition, its format is “if <condition>:”, this tells python to look at the condition and evaluate it if it is true or false.
The condition here will be true when user_age is less than 21.
- This is the condition, its format is “if <condition>:”, this tells python to look at the condition and evaluate it if it is true or false.
- elif user_age > 130:
- This is an optional addition to the condition, and tells python to execute the code inside this block if no previous condition was true, and if user_age is greater than 200.
- It’s format is similar to if, “elif <condition>:”
- You can have multiple elif conditions and blocks, one after the other.
- else:
- This is another optional addition, this tells python that the code block below it needs to be executed if none of the if/elif conditions before it were true.
- Some prints are indented with 4 spaces at the start of the line.
- This is important, this is the way python knows which line of code belongs to the condition and which is not.
- Lines that belong inside the condition, that we want to execute only if the condition is true need to have the same indentation (have the same amount of spaces before the start of the line).
- The indentation can be a tab or 4 spaces, it is recommended to always use 4 spaces, and most editors (including VSCode) will automatically convert a tab you press on your keyboard to 4 spaces.
- Note that the “Goodbye” message gets printed anyway, because after one of the condition that was true gets executed, the next line to be executed is after all the if/elif/else condition blocks.
Advanced Conditions
The conditions themselves could be more complex, chaining together multiple conditions, with “and” and “or” and “not”.
It’s important to understand that whatever is the final result in the <condition> area, this is what gets checked for it’s “truthiness”.
Things like a string, or a number have their own rules about what is considered True and what is False.
Let’s look at the following example.
empty_string = "" regular_string = "Hello" num1 = 0 num2 = 5 if regular_string: print("Non-Empty string is considered True.") if num2: print("Non-Zero number is considered True.") if not num1 and not empty_string or 1 == 2: print("This also gets printed!")
Non-Empty string is considered True. Non-Zero number is considered True. This also gets printed!
The simple conditions are clear, let’s analyze the complex condition at the end.
To understand it, we need to understand operator precedence, in other words, which operation comes first.
Like in math with multiplication coming before addition, the same thing applies to logical conditions.
- Logical Not – “not” – gets evaluated before the other logical conditions, and it converts a False value to True, and a True value to False.
- Logical And – “and” – gets evaluated next, and it gives us True, only if both sides of the “and” got evaluated to True. In any other situation it gives us False.
- Logical Or – “or” – gets evaluated last, it gives us True, if any of the values on it’s sides got evaluated to True, one is enough, if the one on the left is True, the right one won’t even be checked.
So, knowing this, let’s follow the complex condition above to understand how it is evaluated step by step.
- “not num1 and not empty_string or 1 == 2″
- left most “not” gets evaluated first here, it reverses whatever value “num1” gets evaluated to
- num1 is 0, so it’s evaluated to False
- so “not num1” is True, because it reversed num1’s False.
- “True and not empty_string or 1 == 2″
- Now is the other “not”‘s turn, it reverses empty_string’s “truthiness”
- empty_string is empty, so it’s evaluated to False
- so “not empty_string” is True
- “True and True or 1 == 2″
- “and”‘s turn now, it checks that it has True on both of it’s sides.
- it does, so it gives us True as well.
- “True or 1 == 2“
- “or” is the last one, It gets evaluated to True if any of the sides is True.
- Here it won’t even check the right side, because having True on the left side is enough to know it’s result.
- It’s result is True
- “True“
- Arriving at True, the if condition knows to execute the code inside the block following it.
While Loop
For Loop
use range at first, when getting to list elaborate.