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. These follow the order of operations.

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

Comparison Operators#

Python has comparison operators 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

Activity Time: Operators#

Complete all questions in this Google Form (https://forms.gle/AXS7sHYohzi2kkjH8) and then click submit.

You are encouraged to:

  1. Talk with your neighbor

  2. Try the questions out in your notebok to check your understanding

If you have questions, I’m happy to come over as we work on this!

# cell included to test code out for the activity!

# logical/boolean and comparison operators that will store the value False in the variable false_var
false_var = 4 < 2 and 100 > 200
false_var

# only slay the dragon only if our magic lightsaber sword is charged to 90 or higher and we have 100 or more energy units in our protective shield.

sword_charge = 95
shield_energy = 101

(sword_charge >= 90) and (shield_energy >= 100)
True

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'

Code Style: Operators#

  • Single space around operators

  • No spaces after leading parentheses or before trailing parentheses