Computing: Introduction To Python: Lesson Six
Computing: Introduction To Python: Lesson Six
Lesson six
Learning Objective
• To use a textual programming language to solve a variety of
computational problems.
Success Criteria
• To recognise and use comparison operators.
• To be able to use if, elif and else in Python programs.
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.
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 True
>>> 63 == 64
>>> 63 == 63
>>> 63 != 64
>>> 63 != 63
Comparative Operators
Answers
:
>>> 63 == 64 False
>>> 63 == 63 True
>>> 63 != 64 True
>>> 63 != 63 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
>>> 6
26 <!=
4 ==
9 >>=766
<=8 3
5 True
False
True
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
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.
• 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:
• 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
• 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?
# Be The Best
age = input("Please enter your age:")
# Be The Best
age = int(input("Please enter your age:"))
These are
comparative operators.
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
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.
Green Light: you feel fully confident with this objective and
you understand it well.
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.
Nailing It Down
• 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.