Session 19 – Python: Operators

19.1 Many types of operators

We will discuss many different operators including: Arithmetic, Assignment, Comparison, Bitwise, Logical, Identity and Membership operators. You can think of them as abbreviated commands that facilitate advanced operations. This is why we could not use special characters in our identifiers, they are reserved for another function.

Operators work in conjunction with operands, which are any variable used as input towards an operator.

19.2 Arithmetic operators

#addition
1+2

#subtraction
1-2

#multiplication
1*2

#float division
1/2

#floor division (exclude fraction)
1//2

#modulus
1%2

#exponent
1**2

19.3 Comparison operators

a=1
b=2

#greater than
print('a > b is', a>b)

#less than
print('a < b is', a<b)

#equal to
print('a == b is', a==b)

#not equal to
print('a != b is', a!=b)

#greater than or equal to
print('a >= b is', a>=b)

#less than or equal to
print('a <= b is', a<=b)

19.4 Logical operators

Logical operators allow us to make a choice based on set conditions.

a=1
b=2

#and
print('a and b is',a and b)
1 == 1 and 2 == 3
1 == 1 and 2 == 2


#or
print('a or b is',a or b)
1 == 1 or 2 == 3
1 == 1 or 2 == 2

#not
print('not a is',not a)

19.5 Bitwise operators

Bitwise operators process the bits of integer values. We will not be using bitwise operators. I will still list them below:

& Bitwise AND | Bitwise OR ~ Bitwise NOT ^ Bitwise XOR >> Bitwise right shift << Bitwise left shift

##Assignment operators

We use assignment operators to set values to indentifiers.

Simple Assignment:

=    a=2        a=2

Arithmetic Assignments:

+=    a+=2    a=a+2
-=    a-=2    a=a-2
*=    a*=2    a=a*2
/=    a/=2    a=a/2
%=    a%=2    a=a%2
**=    a**=2    a=a**2

Bitwise Assignments:

&=    a&=2    a=a&2
|=    a|=2    a=a|2
^=    a^=2    a=a^2
>>=    a>>=2    a=a>>2
<<=    a<<=2    a=<<2

19.6 Identity Operators

Identity operators allow the comparison of memory locations of two Python variables.

a = 2

#is
if (type(a) is int):
    print("true")
else:
    print("false")

#is not
if (type(a) is not int):
    print("true")
else:
    print("false")

19.7 Membership operators

Membership operators can tell us if our value is listed in a string, list or tuple.

#in
evidence = 'jack and jill were at the crime scene'
print('jill' in evidence)
print('james' in evidence)
print('j' in evidence)
print('randy' not in evidence)

19.8 What is the order of operations with operators?

Multiplication occurs before addtion, even if the addition is stated first.

#multiplication preceeds addition even below
2 + 6 * 5

#you can use parentheses to force addition first
(2 + 6) * 5

#some operators share the same precedence, so the leftmost will be performed first
print(4 + 7 - 1)

print(4 - 7 + 1)

print(4 * 7 / 1)

print(4 / 7 * 1)

#make sure to test order of operations throughly to make sure your formula is working as preferred
#the best way is to start small and add more as you go

#EXCEPTION!!!
#exponents have right-to-left associativity, which is atypical
print(4 ** 2 ** 2)

print((4 ** 2) ** 2)