Introduction to Python - Lecture 3
Zohair ul Hasan
Yesterday's assignment?
In this lecture, we will talk about:
So far, we've seen programs running as a series of instructions
The strength of programming, however, lies in the ability to skip, repeat, or choose one of several instructions based on certain conditions
We can choose which pieces of code to run using flow control statements
We will explore them in detail in this lecture
But before we learn flow control statements in Python, we need to learn how to represent the "yes" and "no" options we saw in the flow chart
We represent the yes and no values using a new data type called the "boolean" data type
While the integer, floating point and string data types have an unlimited number of possible values, the Boolean data type only has two values: True and False
Boolean values:
>>> True
True
>>> False
False
>>> spam = True
>>> spam
True
>>> type(spam)
<class 'bool'>
>>> true
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined. Did you mean: 'True'?
>>> True = 2 + 2
File "<stdin>", line 1
True = 2 + 2
^^^^
SyntaxError: cannot assign to TrueComparison Operators compare two values and evaluate down to a single Boolean value
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
These operators evaluate to a True or False value depending on the given values
>>> 42 == 42
True
>>> 42 == 99
False
>>> 2 != 3
True
>>> 2 != 2
False
>>> 'hello' == 'hello'
True
>>> 'hello' == 'Hello'
False
>>> 'dog' != 'cat'
True
>>> True == True
True
>>> True != False
True
>>> 42 == 42.0
True
>>> 42 == '42'
False>>> 42 < 100
True
>>> 42 > 100
False
>>> 42 < 42
False
>>> egg_count = 42
>>> egg_count <= 42
True
>>> my_age = 29
>>> my_age >= 10
True
Just like the comparison operators, boolean operators are used to compare boolean values
There are three boolean operators in Python:
The and operator evaluates an expression to True if the expression on the left handside as well as the expression on the right hand side evaluates to True
Otherwise it evaluates to false
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False| Expression | Evaluates to... |
|---|---|
| True and True | True |
| True and False | False |
| False and True | False |
| False and False | False |
| Expression | Evaluates to... |
|---|---|
| True or True | True |
| True or False | True |
| False or True | True |
| False or False | False |
Unlike the and and or operators, the not operator operates on a single Boolean value (or expression)
The not operator simply evaluates to the opposite boolean value
>>> not True
False
>>> not False
True| Expression | Evaluates to... |
|---|---|
| True | False |
| False | True |
Boolean operators are named so because they operator only on boolean expressions
Since comparison operators (==, <, >, <=, >=) always evaluate down to boolean values, we can combine them together
>>> (4 < 5) and (5 < 6)
True
>>> (4 < 5) and (9 < 6)
False
>>> (1 == 2) or (2 == 2)
True>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
TrueFlow controls statements start with a condition, followed by a block of code called the clause
Conditions are expressions that evaluate to a Boolean value (i.e. True/False)
Lines of Python code can be grouped into "blocks"
There are three rules for blocks:
1. Blocks begin when the indentation increases
2. Blocks can contain other blocks
3. Blocks end when the indentation reduces to the containing block's indentation level
name = 'Mary'
password = 'swordfish'
if name == 'Mary':
print('Hello, Mary')
if password == 'swordfish':
print('Access granted.')
else:
print('Wrong password.')Block 1
Block 2
Block 3
An if statement's clause (the block following the if statement) will execute if the if statement's condition is True
The clause is skipped if the condition is False
In plain English, an if statement could be read as, “If this condition is true, execute the code in the clause.”
In Python, an if statement consists of the following:
if keywordif clause)name = 'Alice'
if name == 'Alice':
print('Hi, Alice.')An else statement's clause (the block following the if statement) will execute if the if statement's condition is False
The clause is skipped if the condition is True
In plain English, an else statement could be read as, “If this condition is true, execute this code. Otherwise, execute that code”
In Python, an else statement consists of the following:
else keywordelse clause)name = 'Zohair'
if name == 'Alice':
print('Hi, Alice.')
else:
print('Hello, stranger.')The elif statement is an “else if” statement that always follows an if or another elif statement.
It provides another condition that is checked only if all of the previous conditions were False.
In Python, an elif statement consists of the following:
elif keywordelif clause)name = 'Zohair'
age = 9
if name == 'Alice':
print('Hi, Alice.')
elif age < 13:
print('You are not Alice, kid.')The elif clause executes if age < 12 is True and name == 'Alice' is False. However, if both of the conditions are False, then both of the clauses are skipped
Now let's do an activity to practice control statements: https://itp.zohair.dev/activities/control-statements
Now let's do an activity to learn about loops: https://itp.zohair.dev/activities/loops
Now let's do an activity to learn about importing modules: https://itp.zohair.dev/activities/importing-modules
Using the things we learned in this lecture, you will now be tasked to write a program
You will implement a "guess the number" game
The game will save a random secret number at the start of the program between 1 and 20
It will then prompt the user for their guess. If their guess is correct, it will let them know. If it is too high or too low, it will let them know too.
Here is sample output for the game:
I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too low.
Take a guess.
15
Your guess is too low.
Take a guess.
17
Your guess is too high.
Take a guess.
16
Good job! You guessed my number in 4 guesses!User input