Tuesday, May 19, 2020

Operators

Operators

A full list of standard python operators can be found here:



 x % y : returns the remainder from a division of x divided by y


5 % 3 # returns 2
9 % 3 # returns 0
1 % 3 # returns 1

 **: exponentiation

2**4 # returns 16
2**0 # returns 1
2**1 # returns 2

 //: Division to the floor, which just means round down to the nearest integer

8 // 7 # returns 1
8 // 8 # returns 1
8 // 9 # returns 0
8 // 3 # returns 2


The Logical operators are written in lowercase: and, or, not. The not operator is commonly used with a parentheses that would hold some comparison operations.


testx = not(7 > 5 and 100 < 200)
print(testx) # returns False
# because inside the parentheses and operation yields True.

The membership operator is has very unique capabilities, and be careful how you use it.  It is essentially a stricter form of the double equal sign "test for equality" operator == because it requires two objects to not only be exactly identical to each other ... instead of just having the same content in the same order, meaning occupy the same memory address. It best makes sense to use the is membership operator AFTER a call has been made that maps the same memory space to at least two variables.


x = ["chair", "table"]
y = ["chair", "table"]
w = ["chair", "table"]
v = ["Chair", "Table"]
z = x

print(x is z)

# returns True because z is the same object as x, by way of assignment. (Common assignment does not mean exactly the same memory location; see the next post about Lists for more details)

print(x is y)

# returns False because x is not the same object as y, even if they have the same content in the same order.

print(x == y)

# returns True because x is equal to y in content, and order of content is also exactly the same.


print(x == v)

# returns False because x is not equal to v in content because capitalization matters.

print(x is w)

# returns False because although x is equal to w in content, and order of content is also exactly the same, x and w occupy different memory locations.


The is operator can be used in succession


print(x is z is x) # returns True since all three are exactly identical to each other

print(x is z is y) # returns False since only the left and middle variables are exactly identical to each other.


With the membership operators in and not in in general, you're looking to see whether the value or variable in the argument (left of in) is a subset or an element of a larger variable (right of in). The argument may be either a value or a variable. You can use a pair of square braces to cut out portions of a variable against the argument.

f = ["car", "bike", "scooter"]
k = "car"
d= ["bike", "scooter"]

r = k in f     # returns True
s = k in f[2]  # returns False. Shows how to enable location of elements in a list.
t =  d[1] in k # returns False

 The Bitwise operators ^ & | << >>  can be combined with the equal sign, with equal sign placed to the right of the bitwise operator, to all of a sudden become an assignment operator. This saves a line of code if all you want to do is update the variable's value on the fly. 

Here is an easy to use binary converter

# most significant to least significant bit. 8 bits = 1 byte ranging 0 to 255 in decimal

a = 60            # 60 = 0011 1100 
b = 13              # 13 = 0000 1101 
c = 0              # 0 =   0000 0000
d = 2              # 2 =   0000 0010

c = a & b;        
print(c)           # 12 = 0000 1100

c &= d   
print(c)          # 0 = 0000 0000

c = a | b       
print(c)          # 61 = 0011 1101 

c |= d
print(c)          # 63 = 0011 1111

c = a ^ b;       
print(c)          # 49 = 0011 0001

c ^= d
print(c)          # 60 = 0011 1100

c = a << d      
print(c)           # 240 = 1111 0000

b <<= 1
print(b)          # 26 = 0001 1010

c = a >> d       
print(c)           # 15 = 0000 1111

c >>= d          
print(c)           # 3 = 0000 0011












No comments:

Post a Comment