Control Flow - Conditional Statements

From the previous tutorials, you now know how to write basic Python code. Everything you have seen so far has consisted of sequential execution - lines of code are always performed one after the next, in exactly the order specified.

But we often use more complicated executions than that. Frequently, a program needs to skip over some statements, or choose between alternate sets of statements to execute. That is where control flow comes in. The use of different control statements that order the execution of the statements in a program (referred to as the program’s control flow).

The if Statement

If is a conditional statement. The if statement defines a condition. The condition must return a Boolean value - True or False. If the condition is True, the code underneath the statement is executed.

The Syntax-
If [condition]:
     Statement

Remember - the statement ends with a colon. The code block must be indentated.

Here is an example:

#If x = 10 y = 4 if x > 5: print("x is bigger than 5") if y < 5: print("y is less than 5") if x == 5: print("x equals to 5") x = [1, 4, 7, 10] if len(x) > 2: print("x is a large list")

Comparison and logical operators can be used inside an if statement:

#If x = [1, 4, 7, 10] if len(x) > 2 and len(x) < 10: print("x is a medium sized list") if not False: print("The operand is False")

Nested if

An if statement can be placed inside an if statement:

#Nested if my_list = [10, 8, 6, 4] if len(my_list) >= 1: print("my_list length is greater than or equals 1") if len(my_list) > 1: print("my_list length is greter than 1")

The else Statement

The else statement may come only after an if statement. When the if statement returns a False value, the code below the else block is executed.

Here is an example:

#If - else x = 10 y = 4 if x < 5: print("x is greater than 5") else: print("x is less than 5") if y > 5: print("y is less than 5") else: print("y is greater than 5") z = [1, 4, 7, 10] if len(z) < 2: print("z is a long list") else: print("z is short list")

The elif Statement

Elif (else if) is an if statement that may come after a regular if statement. When the first if returns False, the next elif statement is calculated. If it returns True than the block below is executed. If the elif also returns False, the next elif statement is calculated the same way. It is possible to use an if statement and then as many elif statements as we wish and then else:

#If - elif - else x = 10 if x > 100: print("x is greater than 100") elif x > 70: print("x is greater than 70") elif x > 60: print ("x is greater than 60") elif x > 50: print ("x is greater than 50") else: print("We don't have a clue")

The pass Statement

An if statement can't be empty - an empty block below an if raises an error.
The pass statement lets us use an empty if without getting an error:

#pass if True: pass print('Success')

Exercise 1

  1. Create an if statement that is always True
  2. Inside the if block print "Python Tutorial is the best tutorial"

#Write your code here:

Exercise 2

  1. Create a dictionary with a few key-value pairs
  2. Create an if statement that checks if the length of the dictionary is greater than 3
  3. If so check if the dictionary has the key - "python" (you can use the keys() method)
  4. Print out if the dictionary has the key

#Create a dictionary #Create an if statement #Check if the dictionary has the key #Print

Exercise 3

  1. Create an if statement that is never True.
  2. Create several elif statements.
  3. Create an else statement.

#Create an if statement #Create elif statements #Create an else statement

Exercise 4

  1. Create a dictionary with a few key-value pairs, add a key - "python".
  2. Create an if statement that checks if the length of the dictionary is greater than 100
  3. Check if the dictionary has the key - "nokey" (you can use the keys() method)
  4. Else check if the dictionary has the key "python"

#Create a dictionary #Create an if statement #Check if the dictionary has the key #Create an else statement