Operators#
assignment
math
logic
comparison
membership
Assignment Operator#
my_var = 1
Math Operators#
+,-,*,/for addition, subtraction, multiplication, & division**for exponentiation &%for modulus (remainder)//for floor division (integer division)
Logical (Boolean) operators#
use Boolean logic
logical operators:
and,or, andnot
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.
and: True if both are trueor: True if at least one is truenot: True only if false
Comparison Operators#
==: 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:
Talk with your neighbor
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#
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 sequencenot 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#
'COGS' + ' 18'
'a' + 'b' + 'c'
Code Style: Operators#
Single space around operators
No spaces after leading parentheses or before trailing parentheses