Operators#

  • assignment

  • math

  • logic

  • comparison

  • identity

  • membership

Operators are special symbols in Python that carry out arithmetic or logical computation.

Assignment Operator#

Python uses = for assignment.
my_var = 1 

Math Operators#

  • +, -, *, / for addition, subtraction, multiplication, & division

  • ** for exponentiation & % for modulus (remainder)

  • // for floor division (integer division)

Python uses the mathematical operators +, -, *, / for 'sum', 'substract', 'multiply', and 'divide', respectively.
print(2 + 3)
div_result = 4 // 2 
print(div_result)
type(div_result)

Order of Operations#

Mathematical operators follow the rules for order of operations.

  • follow the rules for order of operations.

  • parentheses specify which order you want to occur first

order_operations = 3 + 16 / 2
print(order_operations)

To specify that you want the addition to occur first, you would use parentheses.

specify_operations = (3 + 16) / 2
print(specify_operations)

Clicker Question #1#

What would be the value stored in my_value?

Note: Best to think about it before running the code to ensure you understand.

my_value = (3 + 2) + 16 / (4 / 2) 
my_value
  • A) 7.0

  • B) 10.5

  • C) 13.0

  • D) 20.0

  • E) Produces an error

More Math#

Python also has ** for exponentiation and % for remainder (called modulus). These also return numbers.
# 2 to the power 3
2 ** 3
# remainder of 17 divided by 7
print(17 // 7)
print(17 % 7)

Clicker Question #2#

What would be the value stored in remainder?

remainder = 16 % 5
remainder
  • A) 0

  • B) 1

  • C) 3

  • D) 3.2

  • E) Produces an error

Clicker Question #3#

What would be the value stored in modulo_time?

modulo_time = (4 * 2) % my_value
modulo_time
  • A) 0

  • B) 1

  • C) 8

  • D) 3.2

  • E) Produces an error

Remainder#

How to get Python to tell you the integer and the remainder when dividing?

a = 17 // 7
b = 17 % 7

print(a, 'remainder', b)

// is an operator for floor division (integer division)

Logical (Boolean) operators#

  • use Boolean logic

  • logical operators: and, or, and not

Booleans are named after the British mathematician - George Boole. He first formulated Boolean algebra, which are a set of rules for how to reason with and combine these values. This is the basis of all modern computer logic.

Python has and, or and not for boolean logic. These operators often return booleans, but can return any Python value.
  • and : True if both are true

  • or : True if at least one is true

  • not : True only if false

True and True
True or True
True and not False
not False
# two nots cancel one another out
not (not True)

Capitalization matters#

# this will give you an error
# 'TRUE' is not a boolean
# 'True' is
TRUE and TRUE

Clicker Question #4#

How will the following boolean expression evaluate:

(6 < 10) and (4 == 4)
  • A) True

  • B) False

  • C) None

  • D) This code will fail

Comparison Operators#

Python has comparison operators ==, !=, <, >, <=, and >= for value comparisons. These operators return booleans.
  • == : values are equal

  • != : values are not equal

  • < : value on left is less than value or right

  • > : value on left is greater than value on right

  • <= : value on left is less than or equal to value on right

  • >= : value on left is greater than or equal to value on the right

a = 12
b = 13
a > b
True == True
True != False
'aa' == 'aB'
12 <= 13

Clicker Question #5#

Assume you’re writing a videogame that will only slay the dragon if our magic sword is charged to 90 or higher and the dragon has no more than 25 hit points.

Start with the code in the following cell. Replace --- with operators or values that will evaluate to True when the cell is run (and slay the dragon!).

  • A) I did it!

  • B) I tried but am stuck.

  • C) I’m unsure where to start

## EDIT CODE HERE
sword_charge = ---
hit_points = ---

(sword_charge --- 90) and (hit_points --- 25)

Identity Operators#

Identity operators are used to check if two values (or variables) are located on the same part of the memory.

Python uses is and is not to compare identity. These operators return booleans.
  • is : True if both refer to the same object

  • is not : True if they do not refer to the same object

a = 927
b = a
c = 927
print(a is b)
print(c is a)
print(c == a)
a = 2
print(b)
print(a is b)

Two variables that are equal does not imply that they are identical.

If we wanted that second statement to evaluate as True we could use is not

# make a True statement
print(c is not a) 
# testing for value equality
a == b == c

Clicker Question #6#

Using the variables provided below and identity operators replace --- with code such that true_variable will return True and false_variable will return False.

  • A) I did it!

  • B) I tried but am stuck.

  • C) I’m unsure where to start

z = 5
x = '5'
c = 'Hello!'
d = c
e = [1, 2, 3]
f = [1, 2, 3]

# EDIT CODE HERE
true_variable = e --- f
false_variable = d --- f

print(true_variable, false_variable)

Membership Operators#

Python uses in and not in to compare membership. These operators return booleans.

Membership operators are used to check whether a value or variable is found in a sequence.

Here, we’ll just be checking for value membership in strings. But, we’ll discuss lists, tuples, sets, and dictionaries soon.

  • in : True if value is found in the sequence

  • not in : True if value is not found in the sequence

x = 'I love COGS18!'
'l' in x
print('f' in x)


x
print('L' in x)
print('COGS' in x)
print('CSOG' in x)
print(' ' in x)

String Concatenation#

Operators sometimes do different things on different types of variables. For example, + on strings does concatenation.
'COGS' + ' ' + '18'
'a' + 'b' + 'c'

Chaining Operators#

Operators and variables can also be chained together into arbitrarily complex expressions.
# Note that you can use parentheses to chunk sections
(13 % 7 <= 7) and ('COGS' + '18' == 'COGS18')
(13 % 7 >= 7)
('COGS' + '18' == 'COGS18')

Clicker Question #7#

How will the following expression evaluate:

2**2 >= 4 and 13 % 3 > 1
  • a) True

  • b) False

  • c) None

  • d) This code will fail

Code Style: Operators#

  • Single space around operators

  • No spaces after leading parentheses or before trailing parentheses