# 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 = "@"

## 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"

## 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

1. 25 > 13

2. 25 < 13

3. 19 == 90

4. 19 != 90

5. 90 >= 90

## 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()

print("Welcome John Smith")
else:
``````

## 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
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:
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”.

## COMPLEX IF STATEMENTS

• Nested if statements get complicated quite quickly.
• There are different ways of writing them. Consider the following:
``````username = input()
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()
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()
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

TRUE

TRUE

TRUE

FALSE

TRUE

TRUE

FALSE

FALSE

TRUE

TRUE

FALSE

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.

• 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

``````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

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

``````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.")

print("Welcome John Smith")
else
print("Welcome")
else:
Presentation Overview
If statements
© 2020 - 2024 J Balfour
16:24 | 13-09-2024
!
Keywords
DragonDocs Management
Random selector
Sections
Binary conversion
Denary to binary conversion
Binary to denary conversion
Feedback 👍
Accessibility

Apply a filter:

×
All slideshow files