1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 2. Operators and Selection(ifs)


Lesson Objective

  • Understand Selection and how to use IF Statements in Python programming.
  • Understand the meaning of the mathematical, comparison and logical operations and know how to use them.

Lesson Notes

If Statements

If statements are used to make decisions in your programs. Different parts of your code will run depending on whether or not a condition is true or false.

Code:

name = input(“Enter your name: ”)
print (“Your name is...”)
print (name)
if name == “Mr Ahmed”:
   print (“Welcome Mr Ahmed. What would you like to do...”)
else:
   print (“Invalid User!! Goodbye.”)

Cmd Output:

Enter your name: Susan
Your name is...
Susan
Invalid User!! Goodbye.
------
Enter your name: Mr Simpson
Your name is...
Mr Simpson
Invalid User!! Goodbye.
------
Enter your name: Mr Ahmed
Your name is...
Mr Ahmed
Welcome Mr Ahmed. What would you like to do...

ELIF Statements

When you need to check multiple conditions, you could use an "elif" statement. This allows you to execute a block of code as soon as a condition is TRUE. The example code now allows more names to be accepted into the program.

Code:

name = input(“Enter your name: ”)
print (“Your name is...”)
print (name)
if name == “Mr Ahmed”:
   print (“Welcome Mr Ahmed. What would you like 
         to do...”)
elif name == “Melon Lord”:
   print (“Welcome Melon Lord!! What would you 
         like to do...”)
elif name == “Daenerys”:
   print (“Daenerys Stormborn of House Targaryen, 
         the First of Her Name, Queen of the Andals 
         and the First Men, Protector of the Seven 
         Kingdoms, the Mother of Dragons, the Khaleesi 
         of the Great Grass Sea, the Unburnt, the 
         Breaker of Chains”)
else:
   print (“Invalid User!! Goodbye.”)

Cmd Output:

Enter your name: Mr Ahmed
Your name is...
Mr Ahmed
Welcome Mr Ahmed. What would you like to do...
------
Enter your name: Melon Lord
Your name is...
Mr Ahmed
Welcome Melon Lord!! What would you like to do...
------
Enter your name: Daenerys
Your name is...
Daenerys
Daenerys Stormborn of House Targaryen, the First of Her Name, Queen of the Andals and the First Men, Protector of the Seven Kingdoms, the Mother of Dragons, the Khaleesi of the Great Grass Sea, the Unburnt, the Breaker of Chains
------
Enter your name: Daisy
Your name is...
Daisy
Invalid User!! Goodbye.

Nested Selection

IF Statements inside another IF Statement structures are referred to as nested IF Statements.

Code:

question = input("Do you like Penguins?: ")
   if question == "Yes":
      subquestion = input("Can Penguins fly?: ")
      if subquestion == "Yes":
         print("Awesome, You know things.You must really like Penguin!!")
      else:
         print("Are you really sure you actually like Penguins?")
   else:
      print("Oh OK... Bye...")

Cmd Output:

Do you like Penguins?: No
oh OK... Bye... 
------
Do you like Penguins?: Yes
Can Penguins fly?: No
Are you really sure you actually like Penguins?
------
Do you like Penguins?: Yes
Can Penguins fly?: Yes
Awesome, You know things.You must really like Penguin!!

Math Operators

These operators are used to carry out arithmetic calculations using variables and other data sets.

Example of + Operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit + vegetable
print(answer)

Cmd Output:

50

Example of - Operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit - vegetable
print(answer)

Cmd Output:

30

Example of ÷ Operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit / vegetable
print(answer)

Cmd Output:

4.0

Example of * Operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print(answer)

Cmd Output:

400

Example of Remainders Division (Modulo) being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit % vegetable
print(answer)

Cmd Output:

0

Example of Whole Number Division (Floor) being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit // vegetable
print(answer)

Cmd Output:

4

Comparison Operators

These operators are used to compare variables and other data types.

Example of < operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
if answer < 500:
   print ("Elephant")
else:
   print ("Mouse")

Cmd Output:

400
Elephant

Example of > operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
if answer > 500:
   print ("Elephant")
else:
   print ("Mouse")

Cmd Output:

400
Mouse

Example of == operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
if answer == 500:
   print ("Elephant")
else:
   print ("Mouse")

Cmd Output:

400
Mouse

Example of != operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
if answer != 500:
   print ("Elephant")
else:
   print ("Mouse")

Cmd Output:

400
Elephant

Example of >= operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
if answer >= 500:
   print ("Elephant")
else:
   print ("Mouse")

Cmd Output:

400
Mouse

Example of <= operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
if answer <= 500:
   print ("Elephant")
else:
   print ("Mouse")

Cmd Output:

400
Elephant

Logical Operators

These operators are used to compare variables and other conditions to determine is a statement is either TRUE or FALSE.

Example of AND operator being used below.

Code:

fruit = 40
vegetable = 10
answer = fruit * vegetable
print (answer)
name = input("Enter your name: ")
if answer == 400 and name == "Mr Ahmed":
    print ("Elephant")
else:
    print ("Mouse")

Cmd Output:

400
Enter your name: Mr Ahmed
Elephant
------
400
Enter your name: Mr Simpson
Mouse

Example of AND operator being used below.

Code:

fruit = 40
vegetable = 5
answer = fruit * vegetable
print (answer)
name = input("Enter your name: ")
if answer == 400 or name == "Mr Ahmed":
    print ("Elephant")
else:
    print ("Mouse")

Cmd Output:

200
Enter your name: Rahaid
Mouse
------
200
Enter your name: Mr Ahmed
Elephant