Python KPRIDE: Booleans
star
star
star
star
star
Last updated 7 months ago
16 questions
Keywords
- 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
We can compare values with the symbols above so check if something is true or not.
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.
1
What do you think will be the output from the following code:
print ( 10 > 5 )
What do you think will be the output from the following code:
print ( 10 > 5 )
1
What do you think will be the output from the following code:
print ( 10 < 5 )
What do you think will be the output from the following code:
print ( 10 < 5 )
1
What do you think will be the output from the following code:
print ( 10 < 10 )
What do you think will be the output from the following code:
print ( 10 < 10 )
1
What do you think will be the output from the following code:
print ( 10 <= 10 )
What do you think will be the output from the following code:
print ( 10 <= 10 )
1
What do you think will be the output from the following code:
print ( len("Word") < 5 )
What do you think will be the output from the following code:
print ( len("Word") < 5 )
1
What do you think will be the output from the following code:
print ( 5 == 5 )
What do you think will be the output from the following code:
print ( 5 == 5 )
1
What do you think will be the output from the following code:
print ( "Hello" == "Hello" )
What do you think will be the output from the following code:
print ( "Hello" == "Hello" )
1
What do you think will be the output from the following code:
print ( "hello" == "Hello" )
What do you think will be the output from the following code:
print ( "hello" == "Hello" )
1
What do you think will be the output from the following code:
print ( 5 != 5 )
What do you think will be the output from the following code:
print ( 5 != 5 )
1
What do you think will be the output from the following code:
myPassword = 123print ( myPassword != 123 )
What do you think will be the output from the following code:
myPassword = 123
print ( myPassword != 123 )
1
What do you think will be the output from the following code:
myPassword = 123print ( myPassword != "123" )
What do you think will be the output from the following code:
myPassword = 123
print ( myPassword != "123" )
1
What do you think will be the output from the following code:
Compare = "Python" > "Java"print(Compare)
What do you think will be the output from the following code:
Compare = "Python" > "Java"
print(Compare)
Investigate
- Look at the code on the right.
- Run it and see what it does.
- Click on the pencil icon at the top to see the code again.
FIRST EXAMPLE:
- Change the values of firstNumber and secondNumber. What happens?
- Look down at the "Extra" section. Can you understand what AND OR NOT does?
Second Example:
- Run the program. What input do you need to get the output "Access Granted"?
- Change it so the secret password is now a number.
- Change either line 2 or line 4 to turn it into integers, so it can be compared as integers.
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
Create a program that compares two numbers. Compare the numbers if the first number is larger than or equal to the second number.
Create a program that compares two numbers.
Compare the numbers if the first number is larger than or equal to the second number.
1
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 OctagonsOUTPUT False
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
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 55522123Output True
Hint: You will need to use variablename [ : ]
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
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 Johnuser enters wordOutputs True
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