0% found this document useful (0 votes)
8 views28 pages

Computing: Introduction To Python: Lesson Six

Uploaded by

ali.wessam4444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views28 pages

Computing: Introduction To Python: Lesson Six

Uploaded by

ali.wessam4444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Computing: Introduction to Python

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

(Don’t type the characters >>> as these appear automatically on


each new line in the interactive mode window.)

•You should see something like this:

>>> 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

>>> 34 < 36 True

>>> 8 >= 5 True

>>> 27 <= 14 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
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:

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

>>> 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

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:

>>> true and true

This is also incorrect. Why?


>>> True AND True

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:

>>> 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
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:"))
This is a
Boolean operator.
if age >= 16 and age <= 32:
print("Yes, you can join the Army.")
else:
print("Sorry, you can't join the Army.")

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.

Amber Light: you have understood most of the objective


and you are happy with your progress.

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.

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/SelfAssessments/

You might also like