Computing: Introduction to Python
Lesson 3
• To know how selection is used to make decisions in a computer
program.
• To recognise and use comparison operators.
• To be able to use if, elif and else in Python programs.
• To use a textual programming language to solve a variety of
computational problems.
Learning Objective
Success Criteria
Pyphone Passcode
• Start Python IDLE and type in the following code in the script mode window.
• Save and run your program.
# Screen Lock
print("Pyphone 360")
passcode = input("Enter your passcode:")
if passcode == "1234":
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
Pyphone 360
Enter your
passcode:
1234
Welcome –
Pyphone unlocked
Pyphone Passcode
# Screen Lock
print("Pyphone 360")
Pyphone 360
What do these
lines of code
do?
Answer: it prints
the text “Pyphone
360” on the screen.
Pyphone Passcode
passcode = input("Enter your passcode:")
Pyphone 360
Enter your
passcode:
What do these
lines of code
do?
Answer: it prints
the text “Enter your
passcode:” on the
screen and waits
for user input.
Pyphone Passcode
if passcode == "1234":
print("Welcome - Pyphone unlocked")
Pyphone 360
Enter passcode:
1234
Welcome –
Pyphone unlocked
What do these
lines of code
do?
Answer: if the user
enters the code
1234 then the
Pyphone will be
unlocked.
Pyphone Passcode
else:
print("Incorrect passcode")
Pyphone 360
Enter passcode:
5678
Incorrect passcode
What do these
lines of code
do?
Answer: if the
user enters the
wrong code then
the Pyphone will
remain locked.
Selection
• This Python program is an example of selection.
• Selection is how computer programs make decisions.
• In Python, as with many other programming languages, we use the
keywords if and else.
• It works like this:
IF something is true, THEN
do this.
ELSE (if the something is NOT true) THEN
do that.
if passcode == "1234":
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
Selection
if passcode == "1234":
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
There are several things to remember when using selection in Python.
1.The keywords if and else must be in lower case (not UPPER CASE).
2.We always end each if line of code with a colon :
3.We always end each else line of code with a colon :
4.Notice that we use == instead of just a single =
(because == is a comparison operator, more about this later on…)
5.Notice how the lines of code after if and else are indented.
Indentation is very important in Python.
6.Python IDLE will automatically create indentation for you on the next
line when you use a colon correctly.
Selection
Our last program only had two choices: the passcode was either correct
or incorrect. What if we wanted to have more than two choices?
•Solution: in Python we can use the elif keyword, which means else-if.
•Type in the following code in the script mode window, then save and
run.
# Grades Calculator
score = int(input("Enter your score:"))
if score >= 80:
print("Great score, well done!")
elif score >= 50:
print("Not bad, could do better.")
else:
print("Oh dear!")
>= means
greater than
or equal to.
Selection
• In Python, for every if, we can have one or more elif, and one else.
• else is optional - we only use it if we need to.
• We can improve our last program by using more elifs. Try this
yourself:
score = int(input("Enter your score:"))
if score >= 80:
print("Grade A")
elif score >= 70:
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Oh dear!")
Now try adding some more
elif lines for grades D
(>=50),
E (>=40) and F (>=30).
title
# Grades Calculator Solution
score = int(input("Enter your score:"))
if score >= 80:
print("Grade A")
elif score >= 70:
print("Grade B")
elif score >= 60:
print("Grade C")
elif score >= 50:
print("Grade D")
elif score >= 40:
print("Grade E")
elif score >= 30:
print("Grade F")
else:
print("Oh dear!")
Run your program several
times and test that it works
for scores between 1 and
100.
Selection
Key Terms
Selection is when computers make decisions.
A computer can be programmed to make a selection between
two or more choices, depending on the condition (the question).
Key Terms
To program selection in Python we use the key words if, elif and else.
elif means ‘else if’. For every if statement, we can include any
number of elif statements. else can be thought of as meaning
‘none of the above’. For every if statement, we can include a single
else statement, or no else statements (else is optional).
Selection
if something == True:
# stuff that happens if something is true
elif something else == True:
# stuff that happens if something else is true
else:
# stuff that happens if none are true
Key Terms
Indentation is very important in Python. Each block of code after
an if, elif or else statement must be indented. There should always
be a colon : at the end of every line of code containing if, elif or
else statements. Python IDLE will then automatically indent the
next line of code when we use a colon correctly.
When Programs Go Wrong
Pause for Thought
What would happen if your forgot to include a colon at the end
of an if statement? Answer: it would produce a syntax error.
Try it now and see for yourself.
Do some research to find out what a syntax error is.
How does Python IDLE help you find and fix syntax errors?
>>> if something == True
SyntaxError: invalid syntax
Key Terms
An error in a computer program is known as a bug. Try and
find out why. The process of finding and then fixing errors
in computer programs is often called debugging. Many IDEs
such as Python IDLE have inbuilt debugging tools.
Comparative Operators
• Type this line of Python code into the interactive mode window and
then press the enter key. What happens?
>>> 6 > 7
>>> 6 > 7
False
• 6 > 7 means ‘6 greater than 7’. When we type this into Python IDLE
we are asking the question: is 6 greater than 7?
The answer of course is no, or False.
(Don’t type the characters >>> as these appear automatically on
each new line in the interactive mode window.)
•You should see something like this:
Comparative Operators
Now try the following examples.
Do they give you the answers you’d expect?
>>> 50 > 49
>>> 34 < 36
>>> 8 >= 5
>>> 27 <= 14
Comparative Operators
Answers:
>>> 50 > 49
>>> 34 < 36
>>> 8 >= 5
>>> 27 <= 14
True
True
True
False
Comparative Operators
Now try these examples.
Do they give you the answers you’d expect?
>>> 63 == 64
>>> 63 == 63
>>> 63 != 64
>>> 63 != 63
Comparative Operators
>>> 63 == 64
>>> 63 == 63
>>> 63 != 64
>>> 63 != 63
Answers:
False
True
True
False
Comparative Operators
• Comparative operators are used in Python and other programming
languages to compare two values.
They are also known as relational operators.
• This table shows the comparative operators available in Python:
>>> 4 < 7 True
>>> 4 <= 3 False
>>> 2 > 8 False
>>> 9 >= 5 True
>>> 6 == 6 True
>>> 6 != 6 False
Operator Description Example Result
< Less than 4 < 7 True
<= Less than or equal to 4 <= 3 False
> Greater than 2 > 8 False
>= Greater than or equal
to
9 >= 5 True
== Equal to 6 == 6 True
!= Not equal to 6 != 6 False
Comparative Operators
Pause for Thought
What would happen if you tried the following line of code in
Python IDLE? What happens? Why do you think this happens?
>>> 63 = 63
SyntaxError: can't assign to literal
Key Terms
In Python, a single = is called an assignment operator. We use = to give
(assign) a value to a variable. For example: myAge = 15 means we
assign the value 15 to a variable called myAge.
A double == is called an equality operator. We use == to compare one
value or condition with another value or condition to see if they are the
same (equivalent) or not. The == operator is often used with if
statements.
The != is called an inequality operator.
Boolean Operators
• Quick Question 1
What are the four data-types that we use in Python?
Answer: String, Integer, Float, Boolean
• Quick Question 2:
Which of the following is a Boolean value?
23.76, “Hello World”, True, -95
Answer: True
• Remember: Boolean values can only be True or False.
Pause for Thought
Do you think that Boolean is a strange name for a data type?
It is named after George Boole, an English mathematician
(1815 – 1864) who wrote the book “The Laws of Thought” in 1854.
His work on logic laid the foundation for modern digital electronics,
essential to the design of all computer systems.
We have a lot to thank him for!
Boolean Operators
• OK, we know that a Boolean value can only be True or False.
So what exactly is a Boolean Operator?
• Well, if a numerical operator (such as +) operates on two or more
numbers, then a Boolean operator will operate on two or more
Booleans!
Confused? Let’s find out a bit more…
• There are three different Boolean operators that we need to know
about:
AND, OR, NOT.
• The best way to understand how these work is to try them out in
Python.
• Type the following into the Interactive mode window and press
Enter:
>>> True and True
Boolean ‘And’ Operator
If you got a syntax error you may have typed the code incorrectly.
For example, this is incorrect:
Python code is case-sensitive, so make sure you
type this EXACTLY as you can see it here (see
below). After you press the Enter key, Python
gives you the following output:
This is also incorrect. Why?
>>> true and true
>>> True AND True
>>> True and True
True
Boolean ‘And’ Operator
Now try the following. What outputs will Python give?
>>> True and False
>>> False and True
>>> False and False
Boolean ‘And’ Operator
Answers:
>>> True and False
False
>>> False and True
False
>>> False and False
False
Boolean ‘And’ Operator
We could put all of our results together like this:
First Value Second Value Result
False False False
False True False
True False False
True True True
• This table has all four possible combinations of True and False for
the two values. Look at the Result column. Can you see a pattern?
• The Result is only True when both the First Value and the Second
Value are True.
• This is why we call this operator the Boolean ‘and’ operator.
Boolean ‘Or’ Operator
What about the Boolean ‘or’ operator? What Results would we get
below?
First Value Second Value Result
False False
False True
True False
True True
Use Python IDLE to help you find the answers!
>>> False or False
Boolean ‘Or’ Operator
Answers:
First Value Second Value Result
False False False
False True True
True False True
True True True
• The Result is True if the First Value is True, or if the Second Value is
True, or if both values are True.
• This is why we call this operator the Boolean ‘or’ operator.
Boolean ‘Not’ Operator
• What about the Boolean ‘not’ operator?
• Try the following in Python IDLE. What will the outputs be?
>>> not True
>>> not False
Boolean ‘Not’ Operator
• What about the Boolean ‘not’ operator?
• Try the following in Python IDLE. What will the outputs be?
>>> not True
False
>>> not False
True
The Boolean ‘not’ operator returns the logical opposite, so that
not True is False, and not False is True.
Boolean Bonanza
Test your knowledge of Boolean Operators!
What results would the following statements return?
>>> True and False
>>> True or False
>>> not True
>>> not(True and False)
>>> not(True or False) and True
Boolean Bonanza
Answers:
>>> True and False
False
>>> True or False
True
>>> not True
False
>>> not(True and False)
True
>>> not(True or False) and True
False
Be the Best
• To join the British Army, you have to be 16 years or older, but no
older than 32 years old.
• Using your knowledge of selection,
comparative operators and Boolean operators,
write a Python program that will do the following:
1. Ask the user to enter their age.
2. Output whether or not they are eligible to
join the British Army.
• When you have finished:
Test your program several times to make sure that it works.
Then test someone else’s program.
I was born in 1989.
Can I join the army?
Be the Best
The code below is Jamie’s solution, but it doesn’t work.
Can you see why?
# Be The Best
age = input("Please enter your age:")
if age >= 16 and age <= 32:
print("Yes, you can join the Army.")
else:
print("Sorry, you can't join the Army.")
Be the Best
Here’s the reason: we must use int() to
convert text into an integer.
# Be The Best
age = int(input("Please enter your age:"))
if age >= 16 and age <= 32:
print("Yes, you can join the Army.")
else:
print("Sorry, you can't join the Army.")
Don’t forget to add
the extra brackets!
Be the Best
Can you spot any comparative or Boolean operators in this code?
# Be The Best
age = int(input("Please enter your age:"))
if age >= 16 and age <= 32:
print("Yes, you can join the Army.")
else:
print("Sorry, you can't join the Army.")
This is a
Boolean operator.
These are
comparative
operators.
Be The Best
Alternative solution without using a Boolean operator.
# Be The Best
age = int(input("Please enter your age:"))
if age >= 16:
if age <= 32:
print("Yes, you can join the Army.")
else:
print("Sorry, you can't join the Army.")
else:
print("Sorry, you can't join the Army.")
Which version do you
prefer?
Cinema Challenge
According to the British Board of Film Classification, movies shown at
the cinema should have one of the following age certificates:
•U (suitable for all, ages 4 and over)
•PG (parental guidance)
•12A (suitable only for 12 years and over)
•12 (suitable only for 12 years and over)
•15 (suitable only for 15 years and over)
•18 (suitable for adults only)
Using your knowledge of selection, comparative operators and
Boolean operators, write a Python program that will do the following:
1. Ask the user to enter their age.
2. Output a list of film age certificates which the user can watch
(i.e. U, PG, 12A, 12, 15 or 18).
When you have finished:
Test your program several times to make sure that it works.
Then test someone else’s program.
Cinema Challenge
Possible solution:
# Cinema Selection
# Get age of user
age = int(input("How old are you?"))
# Selection
if age >= 18:
print("U, PG, 12, 12A, 15, 18")
elif age >= 15 and age < 18:
print("U, PG, 12, 12A, 15")
elif age >= 12 and age < 15:
print("U, PG, 12, 12A")
elif age >= 4:
print("U, PG")
else:
print("Sorry, not old enough!")
Let’s Bring It All Together
Key Terms
Selection is when computers make decisions.
A computer can be programmed to make a selection between
two or more choices, depending on the condition (the question).
Key Terms
To program selection in Python we use the key words if, elif and else.
elif means ‘else if’. For every if statement, we can include any
number of elif statements. else can be thought of as meaning
‘none of the above’. For every if statement, we can include a single
else statement, or no else statements (else is optional).
Let’s Bring It All Together
Key Terms
An error in a computer program is known as a bug.
The process of finding and then fixing errors in computer
programs is often called debugging. Many IDEs such as
Python IDLE have inbuilt debugging tools.
Key Terms
In Python, a single = is called an assignment operator. We use = to give
(assign) a value to a variable. For example: myAge = 15 means we
assign the value 15 to a variable called myAge.
A double == is called an equality operator. We use == to compare one
value
or condition with another value or condition to see if they are the same
(equivalent) or not. The == operator is often used with if statements.
The != is called an inequality operator.
Let’s Bring It All Together
Key Terms
Comparative operators are used in Python and other programming
languages to compare two values. They are also known as
relational operators. The following comparative operators can be
used in Python: <, <=, >, >=, ==, !=.
Key Terms
Boolean operators are sometimes used in programs to make
decisions. If a numerical operator (such as +) operates on two
or more numbers, then a Boolean operator will operate on two
or more Boolean values. AND, OR, NOT are all Boolean operators.
Rate Your Progress
Green Light: you feel fully confident with this objective
and you understand it well.
Red Light: you have understood some of the
objective and you will think about it some more later
on, perhaps asking a friend or teacher for help.
Amber Light: you have understood most of the
objective and you are happy with your progress.
Success Criteria:
•To know how selection is used to make decisions in a computer
program.
•To understand why indentation is important in Python.
•To be able to use if, elif and else in Python programs.
• We have learned a lot today about selection and comparative
and Boolean operators in Python.
• Most computer programs make decisions based on two or more
conditions. The keywords IF and ELSE are used in selection and
are used in most programming languages.
• Knowledge-Based Systems (or ‘Expert’ systems) are sometimes used
to model real-life situations such as weather forecasting. These
systems are often very complex, and need a different way of
processing than using IF and ELSE to make simple comparisons.
Nailing It Down
Check out the following interesting
links to try out an expert system for
yourself. Can you find out how these
are coded?
http://20q.net/
https://www.nhsdirect.wales.nhs.uk/SelfAssessment
s/
Introduction to Python Lesson Three_041313.ppt

Introduction to Python Lesson Three_041313.ppt

  • 1.
  • 2.
    • To knowhow selection is used to make decisions in a computer program. • To recognise and use comparison operators. • To be able to use if, elif and else in Python programs. • To use a textual programming language to solve a variety of computational problems. Learning Objective Success Criteria
  • 3.
    Pyphone Passcode • StartPython IDLE and type in the following code in the script mode window. • Save and run your program. # Screen Lock print("Pyphone 360") passcode = input("Enter your passcode:") if passcode == "1234": print("Welcome - Pyphone unlocked") else: print("Incorrect passcode") Pyphone 360 Enter your passcode: 1234 Welcome – Pyphone unlocked
  • 4.
    Pyphone Passcode # ScreenLock print("Pyphone 360") Pyphone 360 What do these lines of code do? Answer: it prints the text “Pyphone 360” on the screen.
  • 5.
    Pyphone Passcode passcode =input("Enter your passcode:") Pyphone 360 Enter your passcode: What do these lines of code do? Answer: it prints the text “Enter your passcode:” on the screen and waits for user input.
  • 6.
    Pyphone Passcode if passcode== "1234": print("Welcome - Pyphone unlocked") Pyphone 360 Enter passcode: 1234 Welcome – Pyphone unlocked What do these lines of code do? Answer: if the user enters the code 1234 then the Pyphone will be unlocked.
  • 7.
    Pyphone Passcode else: print("Incorrect passcode") Pyphone360 Enter passcode: 5678 Incorrect passcode What do these lines of code do? Answer: if the user enters the wrong code then the Pyphone will remain locked.
  • 8.
    Selection • This Pythonprogram is an example of selection. • Selection is how computer programs make decisions. • In Python, as with many other programming languages, we use the keywords if and else. • It works like this: IF something is true, THEN do this. ELSE (if the something is NOT true) THEN do that. if passcode == "1234": print("Welcome - Pyphone unlocked") else: print("Incorrect passcode")
  • 9.
    Selection if passcode =="1234": print("Welcome - Pyphone unlocked") else: print("Incorrect passcode") There are several things to remember when using selection in Python. 1.The keywords if and else must be in lower case (not UPPER CASE). 2.We always end each if line of code with a colon : 3.We always end each else line of code with a colon : 4.Notice that we use == instead of just a single = (because == is a comparison operator, more about this later on…) 5.Notice how the lines of code after if and else are indented. Indentation is very important in Python. 6.Python IDLE will automatically create indentation for you on the next line when you use a colon correctly.
  • 10.
    Selection Our last programonly had two choices: the passcode was either correct or incorrect. What if we wanted to have more than two choices? •Solution: in Python we can use the elif keyword, which means else-if. •Type in the following code in the script mode window, then save and run. # Grades Calculator score = int(input("Enter your score:")) if score >= 80: print("Great score, well done!") elif score >= 50: print("Not bad, could do better.") else: print("Oh dear!") >= means greater than or equal to.
  • 11.
    Selection • In Python,for every if, we can have one or more elif, and one else. • else is optional - we only use it if we need to. • We can improve our last program by using more elifs. Try this yourself: score = int(input("Enter your score:")) if score >= 80: print("Grade A") elif score >= 70: print("Grade B") elif score >= 60: print("Grade C") else: print("Oh dear!") Now try adding some more elif lines for grades D (>=50), E (>=40) and F (>=30).
  • 12.
    title # Grades CalculatorSolution score = int(input("Enter your score:")) if score >= 80: print("Grade A") elif score >= 70: print("Grade B") elif score >= 60: print("Grade C") elif score >= 50: print("Grade D") elif score >= 40: print("Grade E") elif score >= 30: print("Grade F") else: print("Oh dear!") Run your program several times and test that it works for scores between 1 and 100.
  • 13.
    Selection Key Terms Selection iswhen computers make decisions. A computer can be programmed to make a selection between two or more choices, depending on the condition (the question). Key Terms To program selection in Python we use the key words if, elif and else. elif means ‘else if’. For every if statement, we can include any number of elif statements. else can be thought of as meaning ‘none of the above’. For every if statement, we can include a single else statement, or no else statements (else is optional).
  • 14.
    Selection if something ==True: # stuff that happens if something is true elif something else == True: # stuff that happens if something else is true else: # stuff that happens if none are true Key Terms Indentation is very important in Python. Each block of code after an if, elif or else statement must be indented. There should always be a colon : at the end of every line of code containing if, elif or else statements. Python IDLE will then automatically indent the next line of code when we use a colon correctly.
  • 15.
    When Programs GoWrong Pause for Thought What would happen if your forgot to include a colon at the end of an if statement? Answer: it would produce a syntax error. Try it now and see for yourself. Do some research to find out what a syntax error is. How does Python IDLE help you find and fix syntax errors? >>> if something == True SyntaxError: invalid syntax Key Terms An error in a computer program is known as a bug. Try and find out why. The process of finding and then fixing errors in computer programs is often called debugging. Many IDEs such as Python IDLE have inbuilt debugging tools.
  • 16.
    Comparative Operators • Typethis line of Python code into the interactive mode window and then press the enter key. What happens? >>> 6 > 7 >>> 6 > 7 False • 6 > 7 means ‘6 greater than 7’. When we type this into Python IDLE we are asking the question: is 6 greater than 7? The answer of course is no, or False. (Don’t type the characters >>> as these appear automatically on each new line in the interactive mode window.) •You should see something like this:
  • 17.
    Comparative Operators Now trythe following examples. Do they give you the answers you’d expect? >>> 50 > 49 >>> 34 < 36 >>> 8 >= 5 >>> 27 <= 14
  • 18.
    Comparative Operators Answers: >>> 50> 49 >>> 34 < 36 >>> 8 >= 5 >>> 27 <= 14 True True True False
  • 19.
    Comparative Operators Now trythese examples. Do they give you the answers you’d expect? >>> 63 == 64 >>> 63 == 63 >>> 63 != 64 >>> 63 != 63
  • 20.
    Comparative Operators >>> 63== 64 >>> 63 == 63 >>> 63 != 64 >>> 63 != 63 Answers: False True True False
  • 21.
    Comparative Operators • Comparativeoperators are used in Python and other programming languages to compare two values. They are also known as relational operators. • This table shows the comparative operators available in Python: >>> 4 < 7 True >>> 4 <= 3 False >>> 2 > 8 False >>> 9 >= 5 True >>> 6 == 6 True >>> 6 != 6 False Operator Description Example Result < Less than 4 < 7 True <= Less than or equal to 4 <= 3 False > Greater than 2 > 8 False >= Greater than or equal to 9 >= 5 True == Equal to 6 == 6 True != Not equal to 6 != 6 False
  • 22.
    Comparative Operators Pause forThought What would happen if you tried the following line of code in Python IDLE? What happens? Why do you think this happens? >>> 63 = 63 SyntaxError: can't assign to literal Key Terms In Python, a single = is called an assignment operator. We use = to give (assign) a value to a variable. For example: myAge = 15 means we assign the value 15 to a variable called myAge. A double == is called an equality operator. We use == to compare one value or condition with another value or condition to see if they are the same (equivalent) or not. The == operator is often used with if statements. The != is called an inequality operator.
  • 23.
    Boolean Operators • QuickQuestion 1 What are the four data-types that we use in Python? Answer: String, Integer, Float, Boolean • Quick Question 2: Which of the following is a Boolean value? 23.76, “Hello World”, True, -95 Answer: True • Remember: Boolean values can only be True or False. Pause for Thought Do you think that Boolean is a strange name for a data type? It is named after George Boole, an English mathematician (1815 – 1864) who wrote the book “The Laws of Thought” in 1854. His work on logic laid the foundation for modern digital electronics, essential to the design of all computer systems. We have a lot to thank him for!
  • 24.
    Boolean Operators • OK,we know that a Boolean value can only be True or False. So what exactly is a Boolean Operator? • Well, if a numerical operator (such as +) operates on two or more numbers, then a Boolean operator will operate on two or more Booleans! Confused? Let’s find out a bit more… • There are three different Boolean operators that we need to know about: AND, OR, NOT. • The best way to understand how these work is to try them out in Python. • Type the following into the Interactive mode window and press Enter: >>> True and True
  • 25.
    Boolean ‘And’ Operator Ifyou got a syntax error you may have typed the code incorrectly. For example, this is incorrect: Python code is case-sensitive, so make sure you type this EXACTLY as you can see it here (see below). After you press the Enter key, Python gives you the following output: This is also incorrect. Why? >>> true and true >>> True AND True >>> True and True True
  • 26.
    Boolean ‘And’ Operator Nowtry the following. What outputs will Python give? >>> True and False >>> False and True >>> False and False
  • 27.
    Boolean ‘And’ Operator Answers: >>>True and False False >>> False and True False >>> False and False False
  • 28.
    Boolean ‘And’ Operator Wecould put all of our results together like this: First Value Second Value Result False False False False True False True False False True True True • This table has all four possible combinations of True and False for the two values. Look at the Result column. Can you see a pattern? • The Result is only True when both the First Value and the Second Value are True. • This is why we call this operator the Boolean ‘and’ operator.
  • 29.
    Boolean ‘Or’ Operator Whatabout the Boolean ‘or’ operator? What Results would we get below? First Value Second Value Result False False False True True False True True Use Python IDLE to help you find the answers! >>> False or False
  • 30.
    Boolean ‘Or’ Operator Answers: FirstValue Second Value Result False False False False True True True False True True True True • The Result is True if the First Value is True, or if the Second Value is True, or if both values are True. • This is why we call this operator the Boolean ‘or’ operator.
  • 31.
    Boolean ‘Not’ Operator •What about the Boolean ‘not’ operator? • Try the following in Python IDLE. What will the outputs be? >>> not True >>> not False
  • 32.
    Boolean ‘Not’ Operator •What about the Boolean ‘not’ operator? • Try the following in Python IDLE. What will the outputs be? >>> not True False >>> not False True The Boolean ‘not’ operator returns the logical opposite, so that not True is False, and not False is True.
  • 33.
    Boolean Bonanza Test yourknowledge of Boolean Operators! What results would the following statements return? >>> True and False >>> True or False >>> not True >>> not(True and False) >>> not(True or False) and True
  • 34.
    Boolean Bonanza Answers: >>> Trueand False False >>> True or False True >>> not True False >>> not(True and False) True >>> not(True or False) and True False
  • 35.
    Be the Best •To join the British Army, you have to be 16 years or older, but no older than 32 years old. • Using your knowledge of selection, comparative operators and Boolean operators, write a Python program that will do the following: 1. Ask the user to enter their age. 2. Output whether or not they are eligible to join the British Army. • When you have finished: Test your program several times to make sure that it works. Then test someone else’s program. I was born in 1989. Can I join the army?
  • 36.
    Be the Best Thecode below is Jamie’s solution, but it doesn’t work. Can you see why? # Be The Best age = input("Please enter your age:") if age >= 16 and age <= 32: print("Yes, you can join the Army.") else: print("Sorry, you can't join the Army.")
  • 37.
    Be the Best Here’sthe reason: we must use int() to convert text into an integer. # Be The Best age = int(input("Please enter your age:")) if age >= 16 and age <= 32: print("Yes, you can join the Army.") else: print("Sorry, you can't join the Army.") Don’t forget to add the extra brackets!
  • 38.
    Be the Best Canyou spot any comparative or Boolean operators in this code? # Be The Best age = int(input("Please enter your age:")) if age >= 16 and age <= 32: print("Yes, you can join the Army.") else: print("Sorry, you can't join the Army.") This is a Boolean operator. These are comparative operators.
  • 39.
    Be The Best Alternativesolution without using a Boolean operator. # Be The Best age = int(input("Please enter your age:")) if age >= 16: if age <= 32: print("Yes, you can join the Army.") else: print("Sorry, you can't join the Army.") else: print("Sorry, you can't join the Army.") Which version do you prefer?
  • 40.
    Cinema Challenge According tothe British Board of Film Classification, movies shown at the cinema should have one of the following age certificates: •U (suitable for all, ages 4 and over) •PG (parental guidance) •12A (suitable only for 12 years and over) •12 (suitable only for 12 years and over) •15 (suitable only for 15 years and over) •18 (suitable for adults only) Using your knowledge of selection, comparative operators and Boolean operators, write a Python program that will do the following: 1. Ask the user to enter their age. 2. Output a list of film age certificates which the user can watch (i.e. U, PG, 12A, 12, 15 or 18). When you have finished: Test your program several times to make sure that it works. Then test someone else’s program.
  • 41.
    Cinema Challenge Possible solution: #Cinema Selection # Get age of user age = int(input("How old are you?")) # Selection if age >= 18: print("U, PG, 12, 12A, 15, 18") elif age >= 15 and age < 18: print("U, PG, 12, 12A, 15") elif age >= 12 and age < 15: print("U, PG, 12, 12A") elif age >= 4: print("U, PG") else: print("Sorry, not old enough!")
  • 42.
    Let’s Bring ItAll Together Key Terms Selection is when computers make decisions. A computer can be programmed to make a selection between two or more choices, depending on the condition (the question). Key Terms To program selection in Python we use the key words if, elif and else. elif means ‘else if’. For every if statement, we can include any number of elif statements. else can be thought of as meaning ‘none of the above’. For every if statement, we can include a single else statement, or no else statements (else is optional).
  • 43.
    Let’s Bring ItAll Together Key Terms An error in a computer program is known as a bug. The process of finding and then fixing errors in computer programs is often called debugging. Many IDEs such as Python IDLE have inbuilt debugging tools. Key Terms In Python, a single = is called an assignment operator. We use = to give (assign) a value to a variable. For example: myAge = 15 means we assign the value 15 to a variable called myAge. A double == is called an equality operator. We use == to compare one value or condition with another value or condition to see if they are the same (equivalent) or not. The == operator is often used with if statements. The != is called an inequality operator.
  • 44.
    Let’s Bring ItAll Together Key Terms Comparative operators are used in Python and other programming languages to compare two values. They are also known as relational operators. The following comparative operators can be used in Python: <, <=, >, >=, ==, !=. Key Terms Boolean operators are sometimes used in programs to make decisions. If a numerical operator (such as +) operates on two or more numbers, then a Boolean operator will operate on two or more Boolean values. AND, OR, NOT are all Boolean operators.
  • 45.
    Rate Your Progress GreenLight: you feel fully confident with this objective and you understand it well. Red Light: you have understood some of the objective and you will think about it some more later on, perhaps asking a friend or teacher for help. Amber Light: you have understood most of the objective and you are happy with your progress. Success Criteria: •To know how selection is used to make decisions in a computer program. •To understand why indentation is important in Python. •To be able to use if, elif and else in Python programs.
  • 46.
    • We havelearned a lot today about selection and comparative and Boolean operators in Python. • Most computer programs make decisions based on two or more conditions. The keywords IF and ELSE are used in selection and are used in most programming languages. • Knowledge-Based Systems (or ‘Expert’ systems) are sometimes used to model real-life situations such as weather forecasting. These systems are often very complex, and need a different way of processing than using IF and ELSE to make simple comparisons. Nailing It Down Check out the following interesting links to try out an expert system for yourself. Can you find out how these are coded? http://20q.net/ https://www.nhsdirect.wales.nhs.uk/SelfAssessment s/