0% found this document useful (0 votes)
8 views

Intro To Python Programming

Uploaded by

Mamatha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Intro To Python Programming

Uploaded by

Mamatha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Intro to Python programming

Starter activity

Make predictions (think, pair, share)

lucky = 13 Question .
print("My lucky number is", lucky) What will be the output of print when this program is
executed?

1A. My lucky number is lucky


▹ 2B. My lucky number is 13
3C. It is not possible to know the output without

executing the program


4D. There is an error in the program

True or false? . True


This program will always produce the same output,
whenever it is executed.
Starter activity

Make predictions (think, pair, share)

print("My lucky number is", lucky) Question .


lucky = 13 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

During program execution, a variable must have been


assigned a value before that value
is referenced.
Starter activity

Make predictions (think, pair, share)

print("What’s your name?") Question .


user = input() What will be the output of print when this program is
print("Hello", user) executed?

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

True or false? . False


This program will always produce the same output,
whenever it is executed.
Objectives

In this lesson, you will...

● Use arithmetic expressions to calculate values


● Use variables to store and reference values
● Follow walk-throughs of code and keep track of variable values
● Write programs that receive numerical input from the keyboard
Activity 1

Assignments

days = 365 Assignments are not equations.


print(days, "days in a year")
This assignment does not mean that the days variable
always equals 365.

Assignments are instructions to be executed.

This is an instruction to assign the value 365 to the


days variable.

A subsequent assignment can assign a new value to


the days variable, replacing the previous value.
Activity 1

Assignments with expressions

days = 7 * 31 + 4 * 30 + 28 You can use expressions in assignments.


print(days, "days in a year")
This is an instruction to evaluate the expression on the
right and then assign the value to the days
variable on the left.

Tip: Read assignments from right to left.

A subsequent assignment can assign a new value to


the days variable, replacing the previous value.
Activity 1

Arithmetic operators (in Python)

You can use these operators to form Examples

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)

// integer division 15 // 2 quotient of 15÷2 (value: 7)


% remainder of integer division 15 % 2 remainder of 15÷2 (value: 1)
** exponentiation 2 ** 8 2 to the power of 8 (value:
256)
Activity 1

Referring to variables

days = 7 * 31 + 4 * 30 + 28 An expression can refer to the values of variables.


quad = 4 * days + 1
print(quad, "days in four years")
To evaluate this expression, the days variable must
have been assigned a value.

During program execution, a variable must have been


assigned a value before that value is referred to.
Activity 1

The machine
executes the code

days = 7 * 31 + 4 * 30 + 28 365 Current instruction .


quad = 4 * days + 1 Evaluate the expression
print(quad, "days in four years") and assign the value to days.
? Calculate the days in a year.

State .

days 365

Output .
Activity 1

The machine
executes the code

days = 7 * 31 + 4 * 30 + 28 Current instruction .


quad = 4 * days + 1 1461
Evaluate the expression
print(quad, "days in four years") and assign the value to quad.
? Calculate the days in four years.

State .

days 365

quad 1461

Output .
Activity 1

The machine
executes the code

days = 365 Current instruction .


quad = 4 * days + 1 Display the value of quad
print(quad, "days in four years") and the literal "days in four years".
? Display the result.

State .

days 365

quad 1461

Output .
1461 days in four years
Activity 1

Order matters

You will be given a program that is


supposed to convert a length of time from
seconds to minutes.

Rearrange (change the order of) the


statements, so that the program runs to
completion without errors.

Use your worksheet.


Activity 2

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

Calculate age from year of birth

Use pair programming .

Driver
Control the keyboard and mouse.
Navigator
Provide support and instructions.

Alternate between roles.


Activity 3

Calculate age from year of birth

print("Year of birth?") Create a program that asks the user for


birth_year = input()
their birth year and calculates their age.
age = 2024 - birth_year
print("You are", age, "years old")

Live coding

age = 2020 - birth_year


TypeError: unsupported operand type(s) for -: 'int' and 'str'
Activity 3

Calculate age from year of birth: commentary

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:

It is not possible to subtract a piece of text from a


number, hence the error.

what the call to


"2008"
input returns

birth_year "2008"
Activity 3

Calculate age from year of birth: commentary

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.

The expression is evaluated and the result is assigned


to the age variable.

what the call to


"2008"
input returns

birth_year 2008

age 12
Activity 4

How to input numbers

Work on programs that receive numerical


input from the keyboard and process it.

Use your worksheets.


Activity 4

How to input numbers: solutions

print("Weight on Earth?")
weight_earth = int(input())
weight_moon = weight_earth / 6
print("Weight on moon:", weight_moon)
Activity 4

How to input numbers: solutions

print("How old are you?")


age = int(input())
dog_years = age * 7
print("You are", dog_years, "years old in dog years")

You might also like