Data types starter - identify the data types

String

value = "wizard123"

Character

value = "0"

Float

value = 94.3

Boolean

value = True

Float

value = 99.

Character

value = "@"

If statements

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

 

 

 

 

 

Task

TRUE

1. 50 is greater than 43

2. 70 is less than 70

3. 50 is equal to 50

FALSE

TRUE

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

FALSE

Operators

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

  • What do they all do?

     

+

/

*

-

Comparators

Say if it’s true or false

 

 

 

 

 

 

Task

TRUE

1. 25 > 13

2. 25 < 13

3. 19 == 90

FALSE

FALSE

4. 19 != 90

TRUE

5. 90 >= 90

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

Now work on the tasks on if statements (selection) in the booklet. Open the Python booklet to page 18 and read through it. After that, try the tasks on page 20 and 21 and complete the tasks there.

Task

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")
JB
6 Software Development : 4.1 If statements
© J Balfour
Tools