Python: Beginner’s Notes #02

Operators

There are variables operators that can be used to deal with variables.

Arithmetic Operators

Arithmetic operators are used to perform calculation.

x = 3
y = 2
# Addition
print(x + y) # 5
# Subtraction
print(x - y) # 1
# Multiplication
print(x * y) # 6
# Division
print(x / y) # 1.5
# Modulus
print(x % y) # 1
# Exponent
print(x ** y) # 9
# Floor Division
print(x // y) # 1

Addition, subtraction, multiplication and division are straightforward.

Modulus operator % finds the remainder of x by y.

Exponent operator ** computes the value of x to the power of x (i.e. x^y).

Floor division operator // divides x by y and rounds down the result to the nearest whole number.

Note: Floor division always gives result in integer.

Assignment Operators

We have known the most basic assignment operator, =. It assigns the value of right-hand side to the left-hand side.

There are a few more.

x += y    # Equivalent to x = x + y
x -= y    # Equivalent to x = x - y
x *= y    # Equivalent to x = x * y
x /= y    # Equivalent to x = x / y
x %= y    # Equivalent to x = x % y
x **= y   # Equivalent to x = x ** y
x //= y   # Equivalent to x = x // y

Comparison Operators

Comparison operators are used to compare values.

x = 3
y = 2
# Check if x is equal to y
print(x == y) # False
# Check if x is NOT equal to y
print(x != y) # True
# Check if x is greater than y
print(x > y) # True
# Check if x is smaller than y
print(x < y) # False
# Check if x is greater than or equal to y
print(x >= y) # True
# Check if x is smaller than or equal to y
print(x <= y) # False

In Python, the single-equal sign = is used for value assignment while the double-equal sign == is used to check equality.

!= is used to check inequality.

Python also supports a comparison like x < y < z.

Logical Operators

Logical Operators are used to deal with boolean (True/False).

x = 3
y = 2
print(x == 3 and y == 3)   # False
print(x == 3 or y == 3) # True
print(not x == 3) # False

You can connect more than two statements by and/or operator.

print(x == 3 and y == 3 and x != 5 and y != 3)
print(x == 3 or y == 3 or x != 5 or y != 3)

For an and operator, it returns True when all statements are True.

For an or operator, it returns True when at least one statement is True.

For an not operator, it simply reverses the boolean value.

We can also use and operator and or operator at the same time.

print(x == 3 and y == 3 or x != 5 and y != 3)

When they are used at the same time, and operator will be examined first. In this case, x ==3 and y == 3 and x != 3 and y != 3 will be examined first, followed by the or statement. It is the same as the following lines.

C = x == 3 and y == 3
D = x != 5 and y != 3
print(C or D)

In this case, C and D are both boolean. The last line prints False, if C and D are both False, otherwise True.

However, if we want the or statement to be examined first, we can simply put it in brackets.

print(x == 3 and (y == 3 or x != 5) and y != 3)

Membership Operator

Membership operators, in and not in, help to identify if an instance exists or does not exist in a list/set/tuple or even a string.

print(1 in [1, 2, 3])       # True
print(0 in [1, 2, 3])       # False
print(1 not in (1, 2, 3))   # False
print(5 not in {1, 2, 3})   # True
print('a' in 'abcd')        # True
print('e' in 'abcd')        # False

Exercise

What does each ‘print’ function below produce?

x = 10
y = 3
print(x ** y)
print(x // y)
print(x % y)
print(x == 5*2)
print(x % y != 2)
print(x // y == 3)
print(x ** y > 100 and x // y > 10)
print(x ** By > 100 or x // y> 10)

Answer:

A = 10
B = 3
print(x ** y)
# 1000
print(x // y)
# 3
print(x % y)
# 1
print(x == 5*2)
# True
print(x % y != 2)
# True
print(x // y == 3)
# True
print(x ** y > 100 and x // y > 10)
# False
print(x ** y > 100 or x // y > 10)
# True