Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18
The basics
Python: Next Steps
Python: Next steps L1 The basics 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 L1 The basics Starter Reading Code What does this program do? Python: Next steps L1 The basics Section 1 Sets the values of three variables correctPassword guesses guess Python: Next steps L1 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 L1 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 L1 The basics Data Types Variables have to be a certain type A + B = AB Does 3 + 7 = 10? or 37?
A is a string (or text) 3 is an integer (or whole number) 1.618 is a float (or decimal fraction) Python: Next steps L1 The basics Data Types In IDLEs Interactive Mode type these lines and compare the results: value = "3" print (value + value) value = 3 print (value + value)
Python: Next steps L1 The basics Casting Casting is the technical name for converting a variable from one data type to another In IDLE type the following lines: value = "3" print (value + value) value = int(value) print (value + value) Python: Next steps L1 The basics int() and float() If you need a whole number, use int If the number has a decimal place, use float
In IDLE type the following lines: value = 1.618 print (value) value = int(value) print (value) Python: Next steps L1 The basics Using str() to print It can be tricky to print a number, so use str() to turn it into a string
In IDLE try the following lines: money = 15 print("You have " + money) You will get an error. Try this: print("You have " + str(money))
Python: Next steps L1 The basics Worksheet 1 Complete the three tasks Python: Next steps L1 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 L1 The basics If Else Statements If you have a brother or sister then hold your left hand in the air
Else (if you dont) then hold your right hand in the air
Now put your hands down Python: Next steps L1 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 L1 The basics Python Syntax if hasASister == True: leftArm = "up" elif hasABrother == True: rightArm = "up" else: leftArm = "zombie" rightArm = "zombie"
Python: Next steps L1 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 L1 The basics Worksheet 2 Complete the three tasks