Intro To Python Programming
Intro To Python Programming
Starter activity
lucky = 13 Question .
print("My lucky number is", lucky) What will be the output of print when this program is
executed?
A.
1 My lucky number is lucky
B.
2 My lucky number is 13
3
C. It is not possible to know the output without
executing the program
▹ D.
4 There is an error in the program
A.
1 Hello user
▹ B.
2 Hello and whatever the user has typed on the
keyboard
C.
3 It is not possible to know the output without
executing the program
D.
4 There is an error in the program
Assignments
arithmetic expressions.
+ addition a+1 a plus 1
- difference b-c b minus c
* multiplication 3*d 3 times d
/ division 9/4 9 divided by 4 (value: 2.25)
Referring to variables
The machine
executes the code
State .
days 365
Output .
Activity 1
The machine
executes the code
State .
days 365
quad 1461
Output .
Activity 1
The machine
executes the code
State .
days 365
quad 1461
Output .
1461 days in four years
Activity 1
Order matters
Subtle points
number = 5 Question .
double = 2 * number What will be the value of double, after executing line
A number = 15 ? A
▹ A.
1 10
B.
2 30
C.
3 Line A is not a valid assignment: number
already has a value
Why .
Line A only affects the number variable.
The value of double is not ‘updated’.
Activity 2
Subtle points
number = 5 Question .
A number = number + 10 What will be the value of number, after executing line
? A
A.
1 5 and 15
▹ B.
2 15
C.
3 There are no valid values for number
D.
4 Line A is not a valid assignment: number
already has a value
Why .
The expression number + 10 is evaluated
and the result is assigned to number.
The previous value of number is replaced.
Activity 3
Driver
Control the keyboard and mouse.
Navigator
Provide support and instructions.
Live coding
print("Year of birth?") The input function always returns what the user typed
birth_year = input() as a string, i.e. a piece of text.
age = 2023 - birth_year
print("You are", age, "years old") The text returned by input is assigned to the birth_year
variable:
birth_year "2008"
Activity 3
print("Year of birth?") The input function always returns what the user typed
birth_year = int(input()) as a string: a piece of text.
age = 2023 - birth_year
print("You are", age, "years old") This value is passed to the int function, which returns
the corresponding integer.
birth_year 2008
age 12
Activity 4
print("Weight on Earth?")
weight_earth = int(input())
weight_moon = weight_earth / 6
print("Weight on moon:", weight_moon)
Activity 4