Introduction to If Statements in Python for GCSE Students

undefined
Programming for GCSE
Topic 3.1: If Statements
in Python
T
eaching
  
L
ondon 
 
C
omputing
William Marsh
School of Electronic Engineering and Computer Science
Queen Mary University of London
Aims
If statement basics
Visualising ‘If’
Language
Boxes
Scratch
Comparisons
Boolean expressions
More complex ‘If’ statements
undefined
S
IMPLE
 ‘I
F
’ S
TATEMENTS
 
Simple ‘If’ Statement
Change the statements in a program
age = int(input("How old?"))
if age > 21:
   print("Cool! A grown up")
Key word
:
undefined
V
ISUALISING
 ‘I
F
Indentation versus brackets
Boxes
An 'if' statement has an inside
if 
condition A
:
   
Only enter the box when 'A'
Boxes
An 'if' statement has an inside
if 
condition A
:
else:
   
Only enter this box when 'A'
Only enter this box when not 'A'
If in Scratch
Scratch blocks make the ‘inside’ idea obvious
Conditio
n
Inside
Conditio
n
Inside – condition true
Inside – condition false
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
'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")
undefined
C
OMPARISONS
True and false expression – used in 'if'
True and False Expressions
Comparisons
Result can be 'True' or 'False'
Examples
What are the values of the following?
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
'=' and '=='
Do not confuse
Assignment
Operator =
Set variable to value (of expression)
Equality
Operator ==
Compare two expressions
undefined
M
ORE
 C
OMPLEX
 C
ONDITIONAL
S
TATEMENTS
 
Else and Else If
'If' statement with an alternative
if 
condition
:
   
statement 1 – when condition true
else:
   
statement 2 – when condition false
if 
condition A
:
   
statement 1 – when condition A true
elif 
condition B
:
   
statement 2 – when A false and B true
else:
   
statement 3 – when both A and B false
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
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
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
Slide Note
Embed
Share

Delve into the world of programming with If Statements in Python. Learn the basics, visualizations, and practical applications through examples. Understand the syntax, conditions, and flow control for creating decision-making programs. Explore comparisons, boolean expressions, and more complex If statements to enhance your coding skills.

  • Python Programming
  • If Statements
  • GCSE
  • Visualizations
  • Decision Making

Uploaded on Nov 24, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. TeachingLondon Computing Programming for GCSE Topic 3.1: If Statements in Python William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

  2. Aims If statement basics Visualising If Language Boxes Scratch Comparisons Boolean expressions More complex If statements

  3. SIMPLEIF STATEMENTS

  4. 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

  5. VISUALISINGIF Indentation versus brackets

  6. Boxes An 'if' statement has an inside if condition A: Only enter the box when 'A'

  7. Boxes An 'if' statement has an inside if condition A: Only enter this box when 'A' else: Only enter this box when not 'A'

  8. If in Scratch Scratch blocks make the inside idea obvious Conditio n Inside Conditio n Inside condition true Inside condition false

  9. 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

  10. '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")

  11. COMPARISONS True and false expression used in 'if'

  12. True and False Expressions Comparisons Result can be 'True' or 'False' Operator x > y x < y x == y x != y x <= y x >= y Meaning x is greater than y x is less than y x is equals to y x is NOT equal to y x is less than or equal to y x is greater than or equal to y

  13. Examples What are the values of the following? Expression True or False? 10 > 5 2 + 3 < 10 10 != 11 10 <= 10 10 >= 11

  14. Comparison of Strings Comparison operators work for strings Look at the following examples Using ASCII ordering, letter by letter >>> "David" < "David Cameron" True >>> "Dave" < "David" True >>> "Tom" < "Thomas" False >>> "Bill" < "William" True >>> "AAA" < "AAB" True >>> "AAA" < "aaa" True >>> "aaa" < "AAA" False

  15. '=' and '==' Do not confuse Assignment Operator = Set variable to value (of expression) Equality Operator == Compare two expressions

  16. MORE COMPLEX CONDITIONAL STATEMENTS

  17. Else and Else If 'If' statement with an alternative if condition: statement 1 when condition true else: statement 2 when condition false if condition A: statement 1 when condition A true elif condition B: statement 2 when A false and B true else: statement 3 when both A and B false

  18. 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

  19. 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

  20. 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

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#