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

PYT100 M2 1 Slides Operators Comparisons

The document outlines the use of comparisons and logical operators in programming, focusing on conditional statements and their evaluations. It explains various comparison operators, logical operators, and arithmetic operations, providing examples for clarity. The content emphasizes the importance of completeness in comparisons and the use of parentheses for clarity in logical expressions.

Uploaded by

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

PYT100 M2 1 Slides Operators Comparisons

The document outlines the use of comparisons and logical operators in programming, focusing on conditional statements and their evaluations. It explains various comparison operators, logical operators, and arithmetic operations, providing examples for clarity. The content emphasizes the importance of completeness in comparisons and the use of parentheses for clarity in logical expressions.

Uploaded by

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

PYT100

Comparisons and Logical Operators

1
LEARNING OBJECTIVES

At the completion of this lecture, students should be able to:


LO1: Utilize conditional operators for branching
LO2: Combine multiple conditions using Logical operators
LO3: Describe common arithmetic operators

2
CONDITIONS

Statements like if-elif-else require a condition


Conditionals return True or False values
Conditionals can be
Math based, 4 > 3 which returns True.
Strings, "dog" in "Do androids dream of electric sheep?" which returns False.

Additional comparisons exist in advanced data structures

3
LOGICAL COMPARISON OPERATORS

Assume:
Allows for comparisons
var1 = 5 var2 =20
Returns Boolean userinput = "admin" guess = 1
(true/false) Operator Meaning Example Results
> Greater than var1 > 10 False
Can act unexpectedly if
< Less than var2 < 25 True
the comparisons are of Greater than
two different types >=
or equal to
var1 >= var2 False
Less than or
"2" == 2 is False <=
equal to
var1 <= var2 True
userinput !=
!= Not equal to True
"admin"
== Equal to guess == 3 False

Note that <>, meaning not equal, is deprecated starting in Python 3.

4
COMPARISON COMPLETENESS

When using comparison operators, ensure all options are account for
properly
Consider:
if (value > 10):
print(“Greater than 10”)
else:
print(“Less than 10”) # Is this statement true in all cases?

What happens if value == 10?

5
COMPARISON COMPLETENESS

if (value > 10): # value > 10


print(“Greater than 10”)
else: # value <= 10
print(“Less than or equal to 10”)

This works if we are not trying to capture value == 10 specifically


Since the else condition is not always obvious, a best practice is to
add a comment with the implied comparison

6
COMPARISON COMPLETENESS: ELIF

if (value > 10): # value > 10


print(“Greater than 10”)
elif (value == 10): # value == 10
print(“Equal to 10”)
else: # value < 10
print(“Less than 10”)

This can be used to account for all three cases

7
LOGICAL OPERATORS: NOT

Negation, not keyword

The negation of True is false

3 > 5 returns False, so the


negation, not(3>5), is True

8
LOGICAL OPERATORS: AND

Connects two
conditionals

Both must be True to


get a True returned

9
LOGICAL OPERATORS: OR

Connects two
conditionals

Either (or both) must


be True to get a True
returned

10
LOGICAL OPERATORS: PARENTHESIS

Open (
Close or closed )
Chain many operators together
Clarity or to remove ambiguity
Order of operations (PEMDAS)
Use parentheses to remove
ambiguity
Example:
(user=='admin' or user=='root' ) and
authenticated == True

Parenthesis = the singular operator: open ( , close or closed )


Parentheses = two or more parenthesis

Order of Operations (PEMDAS) (2+1)/3 = 1 vs 2 + 1 / 3 = 2.3333....

Can be nested:
>>> True or ((True or False) and False)
True
>>> False or ((True or False) and False)
False
>>> False and ((True or False) and False)
False
>>> True and ((True or False) and False)
False

11
OR: FIRST TRUE IS TRUE

In an or condition, the first True


makes the statement True
Notice that a is defined, but x is
not
The or statement evaluates True,
but the x == "something" will
throw an error
When (a < 10) evaluates to false,
then the second condition is
evaluated

12
AND: FIRST FALSE IS FALSE

In an and condition, the first


False makes the statement False
Notice that a is defined, but x is
not
The and statement evaluates
False, but the x == "something"
will throw an error

13
ARITHMETIC OPERATORS: COMMON MATH OPERATIONS

Addition, + Examples:
Subtraction, - 6+2=8
15 – 7 = 8
Multiplication, *
25 * 3 = 75
Division, / 55 / 5 = 11.0

14
ARITHMETIC OPERATORS: SPECIAL MATH OPERATIONS

Modulus, % Returns the remainder


6%2=0 Note: 6 / 2 = 3 Remainder 0
75 % 10 = 5 Note: 75 / 10 = 7 Remainder 5

Integer Division, //
Equivalent of int(num1/num2)

Also called Floor Division

55//10 = 5 Note: 55/10 = 5.5, but the // returns only the integer part, 5
9//4 = 2 Note: 9/4 = 2.25, but the // returns only the integer part, 4

15
ARITHMETIC OPERATORS: EXPONENTS

Exponent, **
5**2 = 25 Note: This is the equivalent of 5*5
2**4 = 16 Note: This is the equivalent of 2*2*2*2

16

You might also like