Python: Next steps
The basics
Do Now:
• https://wordwall.net/resource/29090242/l1-do-now-
data-types-recap
Recapping the Basics
Python: Next steps
Learning objectives
• Read and understand an existing Python program
• Recall different data types
• Use the int(), float() and str() functions to convert data
types
• Write an if-else statement
Python: Next steps
The basics
Starter
Reading Code: What does this program do?
Python: Next steps
The basics
Section 1
• Sets the values of three variables
• correctPassword
• guesses
• guess
Python: Next steps
The basics
Section 2
• Keeps running while the guess is different from the
correct password
• Asks the user to have a guess
• Counts the number of guesses
Python: Next steps
The basics
Section 3
• Works out how many guesses it took and prints out
the result
• What would happen if you just had the last line and
made 1 guess?
Python: Next steps
The basics
Data types
Variables have to be a certain type
• “3” is a string (or text)
____________
• 3 is an integer (or whole number)
____________
• 1.618 is a float (or decimal fraction)
___________
Python: Next steps
The basics
Casting
• Casting is the technical name for converting a variable from
one data type to another
• On MWB, what is the output of the Python code below:
value = "3"
print (value + value) “33”
value = int(value)
print (value + value) 6
Python: Next steps
The basics
int() and float()
• If you need a whole number, use int
• If the number has a decimal place, use float
• On MWB, what is the output of the Python code below:
value = 1.618
print (value) 1.618
value = int(value)
print (value) 1
Python: Next steps
The basics
Using str() to print
• On MWB, what is the output of the Python code below:
money = 15
print("You have £" + money)
• You will get an error. How to modify your code?
• It can be tricky to print a number and a string, so use str() to turn
the number into a string
print("You have £" + str(money))
Python: Next steps
The basics
Task:
Complete L1 - Workbook Section 1 & 2
Python: Next steps
The basics
If statements
• If you have a brother or sister then
hold your left hand in the air
• Now put your hands down
Python: Next steps
The basics
If – Else statements
• If you have a brother or sister then
hold your left hand in the air
• Else (if you don’t) then
hold your right hand in the air
• Now put your hands down
Python: Next steps
The basics
If – Elif - Else statements
• If you have only a brother then
hold your left hand in the air
• Else if you have only a sister then
hold your right hand in the air
• Else (everyone else) then
hold both arms out like zombies
Python: Next steps
The basics
Python syntax
if hasASister == True:
leftArm = "up"
elif hasABrother == True:
rightArm = "up"
else:
leftArm = "zombie"
rightArm = "zombie"
Python: Next steps
The basics
Common mistakes
• Each if, elif or else needs a : at the end of that line
(think “then”)
• Always use a double equals (==) for a test
• The instructions for what to do need to be indented
(use the Tab key)
Python: Next steps
The basics
Task : Workbook section 1 & 2
• Complete all the tasks