Twa kɔ nsɛm atitiriw so
Log in
Sign up for FREE
arrow_back
Laabri

Python KPRIDE: Booleans

star
star
star
star
star
Last updated 3 months ago
16 Nsɛmmisa

For GCSE, you must know the following for programming:

  • Outputs and Inputs

  • Variables

  • Maths Operators

  • Random numbers

  • Strings and Casting

  • Booleans

  • If, elif, else

  • Nested Ifs

  • For loops

  • While loops

  • Nested loops

  • Arrays/Lists

  • 2D Lists

  • File handling

  • Subprograms(functions/procedures)

Warm-up Practice:

https://type.withcode.uk/python/Conditional_logic

Learning Objectives:

  • I know what a boolean is

  • I can compare with booleans

  • I know that booleans return True or False

Keywords

  1. Boolean: a data type that can hold one of two values (true or false)

  2. > Symbol: Greater than

  3. < Symbol: Less than

  4. == Symbol: Compares if both values are the same

  5. != Symbol: Not equal to

  6. >= Symbol: Greater than or equal to

  7. <= Symbol: Less than or equal to

We can compare values with the symbols above so check if something is true or not.

Debug

Click on each link and try dragging and dropping code into the correct order

https://7strikelink7.github.io/NormalPythonParsons/parsons/Booleans.html

1
1
1
1
1
1
1
1
1
1
1
1

Investigate

  1. Look at the code on the right.

  2. Run it and see what it does.

  3. Click on the pencil icon at the top to see the code again.

FIRST EXAMPLE:

  1. Change the values of firstNumber and secondNumber. What happens?

  2. Look down at the "Extra" section. Can you understand what AND OR NOT does?

Second Example:

  1. Run the program. What input do you need to get the output "Access Granted"?

  2. Change it so the secret password is now a number.

  3. Change either line 2 or line 4 to turn it into integers, so it can be compared as integers.

Code:

firstNumber = 5

secondNumber = 10

print("Is the first number greater?", firstNumber > secondNumber)

print("Is the first number greater or the same?", firstNumber >= secondNumber)

print("Is the first number smaller?", firstNumber < secondNumber)

print("Is the first number greater or the same?", firstNumber <= secondNumber)

print("Are both numbers the same?", firstNumber == secondNumber)

print("Are the numbers different?", firstNumber != secondNumber)

#----------EXTRA----------------------

thirdNumber = 1

forthNumber = 2

# AND checks both comparisons. If BOTH are True, it returns "True"

print(firstNumber < secondNumber and thirdNumber < forthNumber )

#OR Checks both Comparisons. If at least one comparison is true, it returns "True".

print(firstNumber < secondNumber and thirdNumber < forthNumber )

#NOT reverses the answer. If it would be "True", it outputs "False"

print(not firstNumber < secondNumber)

Code 2:

secretPassword = "Password!"

myGuess = input("What is the password? ")

if secretPassword == myGuess:

print("Access Granted. Your input is exactly the same as the password")

else:

print("Access Denied. Your input is not exactly the same as the secret password.")

Extend

Help:

print ( ) is used to output text

input( ) is used to get the user to type something

Variables store values that can change.

Name = Value here

https://www.w3schools.com/python/python_booleans.asp

RECAP

Boolean: a data type that can hold one of two values (true or false)

> Symbol: Greater than

< Symbol: Less than

== Symbol: Compares if both values are the same

!= Symbol: Not equal to

>= Symbol: Greater than or equal to

<= Symbol: Less than or equal to

1
Asemmisa {{asɛmmisaAhyɛnsode}}
13.

Create a program that compares two numbers.

Compare the numbers if the first number is larger than or equal to the second number.

1
Asemmisa {{asɛmmisaAhyɛnsode}}
14.

Create a program that asks the user to enter a word.

Compare the length of that word to the number 8.

Output True or False if that word length is Greater or not.

E.g.

User enters Octagons

OUTPUT False

1
Asemmisa {{asɛmmisaAhyɛnsode}}
15.

Challenge:

Create a Credit Card number checker.

Get the user to enter an 8 digit number and store this into a variable.

Output True if the last 3 digits are "123".

e.g.

User enters 55522123

Output True

Hint: You will need to use

variablename [ : ]

1
Asemmisa {{asɛmmisaAhyɛnsode}}
16.

Challenge:

Create a program with two variables called name and password.

Set it to any value you want.

Then ask the user to input their name and password. Store both of these answers into variables.

Compare both names and passwords to see if they are the same. It must output "True" if BOTH the names and the passwords are exactly the same.

e.g.

name = "John"

password = "word"

user enters John

user enters word

Outputs True

Predict and Run

Look at the code for each question.

What do you think will be the output?

What is the actual output?

Variables that have a value in Speech marks or Quotation marks have a data type called Strings.

Booleans only Return TRUE or FALSE

It is like asking a question.

For example:

Is 10 > 5 ?

Yes. So this is True.

Asemmisa {{asɛmmisaAhyɛnsode}}
1.

What do you think will be the output from the following code:

print ( 10 > 5 )

Asemmisa {{asɛmmisaAhyɛnsode}}
2.

What do you think will be the output from the following code:

print ( 10 < 5 )

Asemmisa {{asɛmmisaAhyɛnsode}}
3.

What do you think will be the output from the following code:

print ( 10 < 10 )

Asemmisa {{asɛmmisaAhyɛnsode}}
4.

What do you think will be the output from the following code:

print ( 10 <= 10 )

Asemmisa {{asɛmmisaAhyɛnsode}}
5.

What do you think will be the output from the following code:

print ( len("Word") < 5 )

Asemmisa {{asɛmmisaAhyɛnsode}}
6.

What do you think will be the output from the following code:

print ( 5 == 5 )

Asemmisa {{asɛmmisaAhyɛnsode}}
7.

What do you think will be the output from the following code:

print ( "Hello" == "Hello" )

Asemmisa {{asɛmmisaAhyɛnsode}}
8.

What do you think will be the output from the following code:

print ( "hello" == "Hello" )

Asemmisa {{asɛmmisaAhyɛnsode}}
9.

What do you think will be the output from the following code:

print ( 5 != 5 )

Asemmisa {{asɛmmisaAhyɛnsode}}
10.

What do you think will be the output from the following code:

myPassword = 123

print ( myPassword != 123 )

Asemmisa {{asɛmmisaAhyɛnsode}}
11.

What do you think will be the output from the following code:

myPassword = 123

print ( myPassword != "123" )

Asemmisa {{asɛmmisaAhyɛnsode}}
12.

What do you think will be the output from the following code:

Compare = "Python" > "Java"

print(Compare)