Course Announcements

Due Date:

  • Complete both pre-assesment surveys by tonight 11:59PM (#finaid)

  • CL1 due Friday 11:59 PM

Notes:

  • A1 now available on datahub

  • Office Hours posted on Canvas

  • To have lecture notes on datahub, have to click on link on Canvas homepage

  • When logging into datahub, choose “stable” environment

Operators#

  • assignment

  • math

  • logic

  • comparison

  • 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', repsectively.
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
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 % 5
modulo_time
  • A) 0

  • B) 1

  • C) 3

  • 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' == 'aa'
12 <= 13

Clicker Question #5#

Assume you’re writing a videogame that will only slay the dragon only if our magic lightsabre sword is charged to 90 or higher and we have 100 or more energy units in our protective shield.

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

(sword_charge ---) and (shield_energy ---)

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!'
print('l' in 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 #6#

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