Slides badge

If statements

Data types starter - identify the data types

String

Character

Float

Boolean

Float

Character

value = "wizard123"

value = "0"

value = 94.3

value = True

value = 99.

value = "@"

Reveal

Learning Intentions

  • Understand basic logic in a program
  • Know how to use if statements in Python
  • Know when to use if statements in Python

Success Criteria

  • I can explain what a condition is
  • I can write a condition in Python
  • I can explain what an if statement does in Python
  • I can write an if statement to make a choice in Python

If statements

  • This lesson will be all about conditions. In programming, a condition is something which we must meet for something to happen.
  • Conditions use our Boolean data type!

Say if it’s true or false

 

 

 

 

 

 

 

Is it true or false?

1. 50 is greater than 43

2. 70 is less than 70

3. 50 is equal to 50

4. "Word 1" is equal to "Word 2"

False

True

False

True

False

True

False

True

Operators

  • Up until now we’ve been working with some mathematical operators shown below.

  • What do they all do?

+

/

*

-

Comparators

Comparator Explanation Example Example of explanation
== Equal to if A == B: Checks if A is equal to B
> Greater than if A > B: Checks if A is greater than B
< Less than if A < B: Checks if A is less than B
!= Not equal to if A != B: Checks if A is not equal to B
>= Greater than or equal to if A >= B: Checks if A is greater than or equal to B
<= Less than or equal to if A <= B: Checks if A is less than or equal to B

Say if it’s true or false

 

 

 

 

 

 

 

 

 

Is it true or false?

1. 25 > 13

2. 25 < 13

3. 19 == 90

4. 19 != 90

5. 90 >= 90

False

True

False

True

False

True

False

True

False

True

If statements

  • The if statement is one of the most useful pieces of code you will learn in any language. Almost every program ever written that does something useful will have an if statement.
  • An if statement is all about making a choice.

Structure of an if statement

If something is true then

   do something

Otherwise

   do something else

Structure of an if statement

if bob > 15:
   #do something
else:
   #do something else

If statements in python

user_age = int(input())

if user_age > 60:
  print("You are old enough to claim a Senior’s RailCard")
else:
  print("You are too young to claim a Senior’s RailCard")

Note the indentation!

If statements in python

username = input()

if username == "johnsmith":
  print("Welcome John Smith")
else:
  print("Username not found")

Key things about if statements

  • In Python, if statements will not work without indentation!
  • Do not forget the colon ( : ) at the end of the if statement line and after the else
  • When checking if something is equal with something else we use the double equals sign ( == ). The single equals single is used when we are putting a value into a variable.

Nested if statements

#Users who over 60 and younger than 80 years
user_age = int(input())

if user_age >= 60:
  if user_age <= 80:
    print("You are old enough to attend the ball")
  else:
    print("You are too old to attend the ball")
else:
  print("You are too young to attend the ball")

What is missing from the code below?

print(Please insert a number)
i = int(input())

if i > 10
print(Please insert a number)
x = int(input())

if x > 10
print(Thank you)

What is missing from the code below?

print("Please insert a number")
i = int(input())

if i > 10:
  print("Please insert a number")
  x = int(input())

if x > 10:
  print("Thank you")
Create a program that asks the user for their age. It should store their age in a variable and then check the user’s age to see if they are 17 or over. If they are it should print “You are old enough to drive” otherwise it would say “You are too young to drive”.

Task

COMPLEX IF STATEMENTS

  • Nested if statements get complicated quite quickly.
  • There are different ways of writing them. Consider the following:
username = input()
password = input()
if username == "John":
  if password == "password123":
    print("Welcome, John")
  else:
    print("Incorrect details provided")
else:
  print("Incorrect details provided")

Complex if statements

  • Nested if statements get complicated quite quickly.
  • There are different ways of writing them. Consider the following:
username = input()
password = input()
if username == "John" and password == "password123":
  print("Welcome, John")
else:
  print("Incorrect details provided")
  • Notice the use of the word keyword and

Complex if statements

  • Notice the use of the Python keyword and
  • This makes this a complex condition
username = input()
password = input()
if username == "John" and password == "password123":
  print("Welcome, John")
else:
  print("Incorrect details provided")
A B A and B A or B
T T T T
T F T F
F T T F
F F F F

Complex if statements

  • There are three keywords that turn a condition in to a complex condition:
    • and
    • or
    • not
A B A and B A or B
True True True True
True False False True
False True False True
False False False False

Complex if statements - Truth Tables

  • This table is called a truth table. It defines what happens with a complex condition:
    • And: true when both parts of a condition are true
    • Or: true when one or more parts of a condition are true
    • Not: flips a true to a false and a false to a true
A B A and B A or B
True True True True
True False False True
False True False True
False False False False

Boolean Heroes

RED OR GREEN

TRUE

sUIT OF METAL AND Gold

TRUE

TRUE

ORANGE OR YELLOW

FALSE

TRUE

RED AND NOT GREEN

TRUE

FALSE

BLUE AND NOT GREEN

FALSE

TRUE

Asgardian AND NOT GREEN

TRUE

FALSE

Wears a cape AND green

FALSE

TRUE

Wears a cape OR green

TRUE

FALSE

  • Write a statement that uses and in every day life. 
    • For example, you are expected to have a Chromebook and be in school uniform in school
  • Write a statement that uses or in every day life.
    • For example, you are expected to be learning history or geography in social subjects
  • Work through the tasks on page 24 and 25 in the Python booklet.

Task

  • Write a statement that uses and in every day life. 
    • For example, you are expected to have a pen and be in school uniform in school
  • Write a statement that uses or in every day life.
    • For example, you are expected to be learning history or geography in social subjects
  • Work through the tasks on page 24 and 25 in the Python booklet.

FALSE

Multiple selection

  • Sometimes an if statement becomes overly complicated:
age = int(input())

if age < 21:
  print("You are under 21")
else:
  if age < 31:
    print("You are under 31")
  else:
    if age < 41:
      print("You are under 41")
    else:
      print("You are much older")

Multiple selection - Else If

  • Introducing the else if statement.
  • We can use an else if statement (in Python this is written as elif) to combine two or more if statements into one and prevent us needing to have nested if statements.
  • Else if is more efficient than having many if statements:
    • it takes fewer lines of code
    • it is easier to read
    • it runs faster

Multiple selection - Else if

  • We can use an else if statement (in Python elif) to write that same program from before:
age = int(input())

if age < 21:
  print("You are under 21")
elif age < 31:
  print("You are under 31")
elif age < 41:
  print("You are under 41")
else:
  print("You are much older")

Combining complex conditions and else if

print("What age are you?")
age = int(input())

if age <= 11 and age >= 5:
  print("You should be in primary school")
elif age <= 17 and age >= 12:
  print("You should be in primary school")
elif age < 5:
  print("You should be in nursery!")
else:
  print("You are much older")
  • Write a program, using complex conditions and else if statements, to:
    • Take in a user's percentage
    • If the percentage is above 70%
      • Tell them they got an A
    • If the percentage is above 60%
      • Tell them they got a B
    • If the percentage is above 50%
      • Tell them they got a C
    • Otherwise tell them they did not pass

Task

print("What percentage did you get?")
percentage = int(input())

if percentage > 70:
  print("You got an A")
elif percentage > 60:
  print("You got a B")
elif percentage > 50:
  print("You got a C")
else:
  print("You didn't pass.")
  • Write a program, using complex conditions and else if statements, to:
    • Take in a user's percentage
    • If the percentage is above 70%
      • Tell them they got an A
    • If the percentage is above 60%
      • Tell them they got a B
    • If the percentage is above 50%
      • Tell them they got a C
    • Otherwise tell them they did not pass

Task - Solution

print("What percentage did you get?")
percentage = int(input())

if percentage > 70:
  print("You got an A")
elif percentage > 60:
  print("You got a B")
elif percentage > 50:
  print("You got a C")
else:
  print("You didn't pass.")

Work through the tasks in the if statements worksheet.

Work through the tasks in the if statements worksheet.

Task

print("What percentage did you get?")
percentage = int(input())

if percentage > 70:
  print("You got an A")
elif percentage > 60:
  print("You got a B")
elif percentage > 50:
  print("You got a C")
else:
  print("You didn't pass.")

Plenary

  1. Identify the errors in the code below:

 

 

 

 

 

 

 

 

 

 

 

print("Please input your username.")
username = input()

if username == "John Smith":
  print("Welcome John Smith")
else
  print("Your password is incorrect")
print("Please input your password.")
password = input()
if password == "Halo":
  print("Welcome")
else:
  print("Your password is incorrect")
Presentation Overview
Close
JB
If statements
© 2020 - 2024 J Balfour
23:33 | 06-12-2024
Join Live Session
Go Live
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Generate Quiz
Generate Lesson Plan
Widget Screen
Canvas Controls
Fullscreen
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      Loading
      All slideshow files