![Page 1: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/1.jpg)
Programming for GCSE
Topic 3.1: If Statements in Python
Teaching London Computing
William MarshSchool of Electronic Engineering and Computer Science
Queen Mary University of London
![Page 2: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/2.jpg)
Aims
• If statement basics • Visualising ‘If’• Language• Boxes• Scratch
• Comparisons• Boolean expressions
• More complex ‘If’ statements
![Page 3: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/3.jpg)
SIMPLE ‘IF’ STATEMENTS
![Page 4: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/4.jpg)
Simple ‘If’ Statement
• Change the statements in a program
age = int(input("How old?"))if age > 21: print("Cool! A grown up")
Key word
Condition
:
indentation
![Page 5: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/5.jpg)
VISUALISING ‘IF’Indentation versus brackets
![Page 6: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/6.jpg)
Boxes
• An 'if' statement has an inside
if condition A: Only enter the box when 'A'
![Page 7: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/7.jpg)
Boxes
• An 'if' statement has an inside
if condition A:
else:
Only enter this box when 'A'
Only enter this box when not 'A'
![Page 8: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/8.jpg)
If in Scratch
• Scratch blocks make the ‘inside’ idea obvious
Condition
Inside
Condition
Inside – condition true
Inside – condition false
![Page 9: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/9.jpg)
Other Languages
• Java uses • ( ) – condition in brackets; no colon• '{' – start of box • '}' – end of box
• Pascal• THEN instead of the colon• BEGIN – start of box• END – end of box
![Page 10: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/10.jpg)
'If' Language• Many instructions have 'if' language
Do not use this service if you are travelling within the next four weeks, or if you are applying from outside the UK
if travelling in next 4 weeks: print("Do not use this service")elif applying from outside UK: print("Do not use this service")else: print("Ok; use this service")
![Page 11: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/11.jpg)
COMPARISONSTrue and false expression – used in 'if'
![Page 12: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/12.jpg)
True and False Expressions• Comparisons• Result can be 'True' or 'False'
Operator Meaning x > y x is greater than y x < y x is less than y x == y x is equals to y x != y x is NOT equal to y x <= y x is less than or equal to y x >= y x is greater than or equal to y
![Page 13: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/13.jpg)
Examples
• What are the values of the following?
Expression True or False? 10 > 5 2 + 3 < 10 10 != 11 10 <= 10 10 >= 11
![Page 14: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/14.jpg)
Comparison of Strings
• Comparison operators work for strings• Look at the following examples>>> "David" < "David Cameron"True>>> "Dave" < "David"True>>> "Tom" < "Thomas"False>>> "Bill" < "William"True>>> "AAA" < "AAB"True>>> "AAA" < "aaa"True>>> "aaa" < "AAA"False
Using ASCII
ordering, letter
by letter
![Page 15: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/15.jpg)
'=' and '=='
• Do not confuse
• Assignment• Operator =• Set variable to value (of expression)
• Equality• Operator ==• Compare two expressions
![Page 16: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/16.jpg)
MORE COMPLEX CONDITIONAL STATEMENTS
![Page 17: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/17.jpg)
Else and Else If• 'If' statement with an alternative
if condition: statement 1 – when condition trueelse: statement 2 – when condition false
if condition A: statement 1 – when condition A trueelif condition B: statement 2 – when A false and B trueelse: statement 3 – when both A and B false
![Page 18: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/18.jpg)
Compare these programs:
age = input("How old are you?")if age < 22: print("Ah, youth!")if age >= 22: print("Cool! A grown up")
age = input("How old are you?")if age < 22: print("Ah, youth!")else: print("Cool! A grown up")
Equivalent
![Page 19: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/19.jpg)
Teaching Issue• Strong understanding of statements
within statements • Needed for loops too
• Only 'if' and 'if … else' are essential• Boolean expression: true and false as
values
![Page 20: Programming for GCSE Topic 3.1: If Statements in Python](https://reader034.vdocuments.site/reader034/viewer/2022051020/56816023550346895dcf26e0/html5/thumbnails/20.jpg)
Summary
• 'If' allows statements in the program to vary
• Comparison operator create 'conditions' (also called 'boolean expressions')
• Python uses 'indentation' to inside the 'if' from outside
• 'Inside' versus 'outside' essential for next topic: loops