Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 11 – Conditional Statements

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 11 – Conditional Statements

Spread the love

Sometimes we want our code to be executed conditionally, not every time. Or, we may want to define several different paths of execution depending on some condition.

We use if to make conditions. The condition is followed by a colon and the block of code which follows is indented.

We can add more conditions with elif. We can add an else block. It’s executed if the condition is not met. There may be only one else statement, but there may be any number of elif statements. As soon as an elif condition is met, its conditional block is executed and the following elif statements are ignored.

Example:

Possible outputs:

or:

etc.

We can use any comparison operator in the condition. We use a double equals sign for equality, because a single equals sign is used for assigning values to variables. Here are all the comparison operators we can use:

==equal to
!=not equal to
greater than
less than
>=greater than or equal to
<=less than or equal to

Example:

Output:

QUIZ

1. An if-condition is followed by a:
    A) colon
    B) semicolon
    C) open parenthesis
 
2. We can add more conditions with:
    A) else if
    B) elsif
    C) elif
 
3. The comparison operator ‘not equal to’ is written as:
    A) <>
    B)!=
    C)!==
 
4. The comparison operator ‘greater than or equal to’ is written as:
    A) >>
    B) >=
    C) >==
 

TRUE OR FALSE?

1) A conditional block is only executed if the condition is true.
2) There may be any number of else statements in a conditional statement.
3) If an elif condition is met, its conditional block is executed and the following elif statements are ignored.
4) All statements within a block must be indented the same number of spaces.
5) Indentation in Python is just a matter of style, but it’s not obligatory.

WHAT’S THE OUTPUT?

1)

2)

3)

SOLUTION

QUIZ

1. An if-condition is followed by a:
    A) colon
    B) semicolon
    C) open parenthesis
 
2. We can add more conditions with:
    A) else if
    B) elsif
    C) elif
 
3. The comparison operator ‘not equal to’ is written as:
    A) <>
    B)!=
    C)!==
 
4. The comparison operator ‘greater than or equal to’ is written as:
    A) >>
    B) >=
    C) >==
 

TRUE OR FALSE?

1) A conditional block is only executed if the condition is true. True
2) There may be any number of else statements in a conditional statement. False
3) If an elif condition is met, its conditional block is executed and the following elif statements are ignored. True
4) All statements within a block must be indented the same number of spaces. True
5) Indentation in Python is just a matter of style, but it’s not obligatory. False

WHAT’S THE OUTPUT?

1)
>>> a = 2 * 5
>>> if a < 4:
…     print(“hello”)
…  

Output:   (empty output because the condition was not met)  
2)
>>> x = “abc”
>>> if x == “abc”:
…     print(“yes”)
… else:
…     print(“no”) …  

Output: yes  
3)
>>> z = 8
>>> if z <= 8:
…     print(“OK”)
…  

Output: OK

PROJECT

PROJECT SOLUTION


Spread the love

Leave a Reply