Conditional statements
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
We are now heading into the sections where we learn how a computer makes its decisions.
"Oye, python, wake up"
"What are you so hyped up about in the early morning, Tikles. I didn't even have my morning electro-coffee yet"
"(Tickles smirk) You wanna learn how to communicate with humans"
"What are you, a kid? *facepalm I'm not so enthusiastic as you"
"Cmon man, here's your 800 watt coffee"
"Ohh, yeahhhhh. What do you want me to do, Sir Tickles"
Code Blocks
So far, we've studied data types, variables and some operations like assignment, comparison. All of those topics didn't require the knowledge of indentation or code blocks. But from here onwards, it'll become critical to understand what exactly a code block is.
We have used whitespace and code lines as how we liked till now. It was because the functions and operations we have used so far were part of just one code block.
Lets take an example to understand this.
a = 10When we run $$a=10$$ this whole statement is complete which means its a complete code block of one statement. There might be other code chunks which uses the variable a, but those other code chunks are part of different code block.
a = 10
print(a)10
In here, $$a=10$$ and $$print(a)$$ have different code block but they have same indentation.
Lets look a more challenging example
If statement
if statements are used to execute a certain code block depending on a condition. The condition could be True or False.
a = 0if True:
print("Code is executed")Code is executed
Notice the indentation of the print statement.
The print("Code is executed") is indented under the if code block
We usually use 4 whitespaces as indentation for better code readability.
The whole syntax goes
# if condition:
# do somethingLets use a False boolean condition. It'll not be executed as the condition is set to false.
if False:
print("Does not matter, this code will never be printed")Lets look at a more practical example of why we use booleans as conditions.
a = 10
b = 11
print(a<b)True
Remember, the comparison operators output a boolean value. We use this boolean value as a condition.
a = 10
b = 11
if a<b:
print("a is less than b")a is less than b
Here, $$a = 10$$ $$b = 11$$ and the if statement had same indentation from the left.
But the print statement was written inside the if code block, so the print statement needs to be indented 4 whitespaces, to make the print a part of if statement
a = 10
b = 11
if a<b:
print("a is less than b")
if a>b:
print("a is greater than b")a is less than b
Here, we use two if code blocks, which have their own print statements indented.
The two if statements here are mutually exclusive so they share the same indentation. One if does not depend on other if statement here.
a = 10
b = 11
if True:
print("first if executed")
if True:
print("second if executed")
if False:
print("third if executed")first if executed
second if executed
There are three if statements now. But of course only the first two if statements are executed because of the condition True.
Now, let's talk about else block. Things will become clear as we practice on these topics.
if else block
Here we will first use True condition. Python reads it as
if the condition is True, do something.
In the following code, we have written, if the condition is true, print("We are inside if block"),
otherwise print("We are inside else block")
In one if-else condition, either the if or the else gets executed.
if True:
print("We are inside if block")
else:
print("We are inside else block")
# because the condition is true, python will never
# look at the else blockWe are inside if block
# the condition is false here
# so instead of going inside the if block
# python directly goes inside the else block
if False:
print("We are inside if block")
else:
print("We are inside else block")We are inside else block
As you've seen, either the if statement gets executed at once or the else statement gets executed.
a = 10
b = 5
if a>b:
print("a is greater than b")
else:
print("a is not greater than b")a is greater than b
Lets experiment with variable values
a = 1
b = 5
if a>b:
print("a is greater than b")
else:
print("a is not greater than b")a is not greater than b
Because now a is 1 and b is 5, and that makes a>b False, so now the else block will run.
Lets make both a and b equal
a = 5
b = 5
if a>b:
print("a is greater than b")
else:
print("a is not greater than b")a is not greater than b
elif statement
Its something along the lines of "else if".
If in the previous example we wanted to use multiple condition checks to get an output of
a is equal to b
# if condition:
# do something
# else if condition2:
# do something
# else if condition3:
# do something
# else:
# do somethingIn python we write this else if statement as elif
# if condition:
# do something
# elif condition2:
# do something
# elif condition3:
# do something
# else:
# do somethinga = 5
b = 5
if a>b:
print("a is greater than b")
elif a==b:
print("a and b are equal")
else:
print("a is not greater than b")a and b are equal
In the previous statement, if elif and else share the same indentation but only one of them will be executed.
our_variable = 5
if our_variable == 1:
print("First condition: value of our_variable is 1")
elif our_variable == 2:
print("Second condition: value of our_variable is 2")
elif our_variable == 3:
print("Third condition: value of our_variable is 3")
elif our_variable == 4:
print("Fourth condition: value of our_variable is 4")
elif our_variable == 5:
print("Fifth condition: value of our_variable is 5")
else:
print("Sixth condition: don't know")Fifth condition: value of our_variable is 5
our_variable = 3
if our_variable == 1:
print("First condition: value of our_variable is 1")
elif our_variable == 2:
print("Second condition: value of our_variable is 2")
elif our_variable == 3:
print("Third condition: value of our_variable is 3")
elif our_variable == 4:
print("Fourth condition: value of our_variable is 4")
elif our_variable == 5:
print("Fifth condition: value of our_variable is 5")
else:
print("Sixth condition: don't know")Third condition: value of our_variable is 3
our_variable = 7
if our_variable == 1:
print("First condition: value of our_variable is 1")
elif our_variable == 2:
print("Second condition: value of our_variable is 2")
elif our_variable == 3:
print("Third condition: value of our_variable is 3")
elif our_variable == 4:
print("Fourth condition: value of our_variable is 4")
elif our_variable == 5:
print("Fifth condition: value of our_variable is 5")
else:
print("Sixth condition: don't know")Sixth condition: don't know
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →