|||
Accessibility Settings

Y10 AU – CT5 – Boolean Operators

Copy this title into a new OneNote document:

Y10 AU – CT5 – Boolean Operators

Complete this table and copy it into the OneNote document:

⏱️ Do It Now:
Answer the following questions

(1) What symbol do we use for assignment?

(2) Give an example of a relational operator and explain its purpose

(3) Why do we initialise variables?

(4) What is casting and why is it done?

🎯 Objectives:

(1) We will be able to identify the three boolean operators used in Python programming

(2) We will be able to explain the role of each boolean operator

(3) We will be able to apply boolean operators to a Python program

🔎 Investigation Task

Access the mini whiteboard app with the code provided by your teacher

You will answer these questions about the code below
———————————————————————————-
(1) On line 12, other than an input being assigned to the age variable, what else is being done to the it?

(2) For the process in question 1, under what circumstances don’t we do this?

(3) What do you think is happening on line 14?

(4) If I input 17, what will the output be?

(5) If I input 10, what will the output be?

# Y10 AU - CT5 - Activity 1 - Investigating Boolean Operators

#----------------- Global Variables -----------------------

age = 0 # Initialising the age variable as an integer

#----------------- Main Program ---------------------------

print("It's September")
print("Are you the correct age to attend TGGS?")

age = int(input("Enter your age: "))

if age >=11 and age <= 15:
    print(f"Being {age} you can attend secondary")
          
elif age >=16 and age <= 17:
    print(f"Being {age} you can attend sixth form")

else:
    print("You cannot attend TGGS")

📖 The len() function

In Python we can use a special function called len().

This allows us to return the length of a string

myName = "Veronica"        # Assigning the string "Veronica" to the variable myName

myNameLength = len(myName) # This assigns the length of myName to the variable myNameLength

print(myNameLength)        # This will output 8, because there are 8 characters in "veronica"

We can also test the length of a string to see if it matches a desired value

myName = "Veronica"        # Assigning the string "Veronica" to the variable myName

myNameLength = len(myName) # This assigns the length of myName to the variable myNameLength

if myNameLength >= 8:
    print("Name is an acceptable length")
    

⌨️ Coding Activity

(1) Create a folder called Y10 AU – CT5 – Boolean Operators

(2) Create a file called Activity 2 – length check.py

(3) Add the code below

(4) Complete each requirement given in the arrow comment blocks

# Activity 2 - Length Check

#----------------- Global Variables ----------------------------------

password = "" # Initialising the password variable as an empty string

#----------------- Main Program --------------------------------------

#====> Assign an input to the password variable
#====> The prompt should ask the user to input a choice of password
#====> between 8 and 20 characters long



#====> Use a conditional sequence with a boolean and operator
#====> to test if the length of the password is between 8 and 20 characters
#====> If the length meets the criteria, output "Password Accepted"
#====> If the length does not meet the criteria, output "Password Not Accepted"


📖 Types of Boolean Operator

As well as using the and operator to check if both conditions are met we can use two other operators

📖 The OR Operator

The or operator will return a True response if either condition has been met

Note that the code does this with three conditions; we can do the same with the and operator

colour = "red"

if colour == "red" or colour == "yellow" or colour == "blue":
    print("It is a primary colour")
    
else:
    print("It is not a primary colour")
    

📖 The NOT Operator

We place the word not before the whole condition – this reverses the test

colour = "red"

if not colour == "red":
    print("It is not red")
    
else:
    print("It is red")
    

It can be used to conditions along with other boolean operators

Notice how the conditions are within brackets

colour = "red"

if not (colour == "red" or colour == "yellow" or colour == "blue"):
    print("It is not a primary colour")
    
else:
    print("It is a primary colour")
    

📖 Combining Boolean Operators

Boolean Operators can be combined to create complex, multi-part conditions

age = 15       # Age is assinged an integer value

height = 1.6   # Height is assigned a real value


if (age >= 13 and age <=21) or height >= 1.4:
    print("You are allowed on the ride")

⌨️ Coding Activities

(1) Copy the code of each activity into a new file with the name given in the title

(2) Amend the code to meet the requirement in each block of arrow comments

⌨️ Activity 3a.py

# Activity 3a - Online Gaming Account

#----------------- Global Variables ----------------------------------

age = 0                   # Stores the player's age
parentalConsent = False   # Stores whether parental consent has been given

#----------------- Main Program --------------------------------------

#====> Assign an input to the age variable
#====> Ask the user to enter their age



#====> Assign an input to the parentalConsent variable
#====> Ask the user whether parental consent has been given
#====> Store True or False



#====> Use a conditional sequence with boolean operators
#====> A player can access the game if they are over 13
#====> AND they either have parental consent
#====> OR are over 16 years old
#====> Output "Access Granted" if the conditions are met
#====> Otherwise output "Access Denied"




⌨️ Activity 3b.py

# Activity - Cinema Ticket Discount

#----------------- Global Variables ----------------------------------

age = 0              # Stores the customer's age
student = False      # Stores whether the customer is a student
voucher = False      # Stores whether the customer has a voucher

#----------------- Main Program --------------------------------------

#====> Assign an input to the age variable
#====> Ask the user to enter their age



#====> Assign an input to the student variable
#====> Ask the user if they are a student
#====> Store True or False



#====> Assign an input to the voucher variable
#====> Ask the user if they already have a promotional voucher
#====> Store True or False



#====> Use a conditional sequence with boolean operators
#====> A discount should be awarded if the customer is a student
#====> OR they are under the age of 16
#====> AND they do NOT already have a voucher
#====> Output "Discount Applied" if eligible
#====> Otherwise output "No Discount Available"




⌨️ Further Challenges

Now you are able to work with Boolean Operators and Conditional Sequences, it is time to try out one of these yourself

Write the code in a new file, with a suitable name e.g. Activity 3-ext1.py or Activity 3-ext2.py

——————————————————————————————————–

(ETX1) Student Progress Monitor

Student is “on track” if:

attendance > 95% AND homework completion > 85%

OR

assessment score > 90%.

——————————————————————————————————–
(EXT2) Airport Security Screening

Allow passenger through if:

passport is valid

AND

boarding pass exists

AND

security check passed

——————————————————————————————————–
(EXT3) Restaurant Booking System

Accept a booking if:

tables are available

AND

party size is 8 or fewer.

Extension:
————–
Larger groups require manager approval.