Lesson 1 of 0
In Progress

Lesson #7: Operators

Python Operators are used to perform operations on values and variables. These are symbols used for the purpose of logical and arithmetic operations.

Arithmetic Operators

Arithmetic operators are used with numeric values to perform mathematical operations.

OperatorExampleMeaning
+num_1 + num_2Addition: adds two operands
num_1 – num_2Subtraction: subtracts two operands
*num_1 * num_2Multiplication: multiplies two operands
/num_1 / num_2Division: divides the first operand by the second
%num_1 % num_2Modulus: returns the remainder
**num_1 ** num_2Exponentiation: Returns first raised to power second
//num_1 // num_2Floor Division: divides the first operand by the second

Example

num_1 = 20
num_2 = 10

print(num_1 + num_2)
# 30

print(num_1 – num_2)
# 10

print(num_1 * num_2)
# 200

print(num_1 / num_2)
# 2.0

print(num_1 % num_2)
# 0

print(num_1 ** num_2)
# 10240000000000

print(num_1 // num_2)
# 2

Comparison Operators

Comparison operators are used to compare two values.

OperatorExampleMeaning
==num_1 == num_2Equal to: returns True if both operands are equal
!=num_1 != num_2Not equal: returns True if operands are not equal
>num_1 > num_2Greater than: returns True if the num_1
is greater than the num_2
<num_1 < num_2Less than: returns True if the num_1
is less than the num_2
>=num_1 >= num_2Greater than or equal to: returns True if the num_1
is greater than or equal to the num_2
<=num_1 <= num_2Less than or equal to: returns True if the num_1
is less than or equal to the num_2

Example

num_1 = 20
num_2 = 10

print(num_1 == num_2)
# False

print(num_1 != num_2)
# True

print(num_1 > num_2)
# True

print(num_1 < num_2)
# False

print(num_1 >= num_2)
# True

print(num_1 <= num_2)
# False

Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExampleSame asMeaning
=num = 2num = 2Assign value of right side of expression
to left side operand
+=num += 2num = num + 2Add
-=num -= 2num = num – 2Subtract
*=num *= 2num = num * 2Multiply
/=num /= 2num = num / 2Divide
%=num %= 2num = num % 2Modulus
//=num //= 2num = num // 2Divide(floor)
**=num **= 2num = num ** 2Exponent
&=num &= 2num = num & 2Performs Bitwise AND on operands
|=num |= 2num = num | 2Performs Bitwise OR on operands
^=num ^= 2num = num ^ 2Performs Bitwise xOR on operands
>>=num >>= 2num = num >> 2Performs Bitwise right shift on operands
<<=num <<= 2num = num << 2Performs Bitwise left shift on operands

Example

num = 0
num += 5
print(num) # 5

num = 10
num -= 5
print(num) # 5

num = 1
num *= 5
print(num) # 5

num = 5
num /= 5
print(num) # 1.0

num = 5
num %= 20
print(num) # 5

num = 100
num //= 10
print(num) # 10

num = 3
num **= 2
print(num) # 9

Identity Operators

Identity Operators are used to check if two values are located on the same part of the memory.

OperatorExampleMeaning
isnum_1 is num_2Returns True if both variables are identical
is notnum_1 is not num_2Returns True if both variables are not identical

Example

num_1 = 2
num_2 = 5
num_3 = num_1

print(num_1 is num_2) # False
print(num_1 is not num_2) # True
print(num_1 is num_3) # True

Logical Operators

Logical Operators are used to combine conditional statements.

OperatorExampleMeaning
andnum_1 and num_2Logical AND: Returns True if
both statements are true
ornum_1 or num_2Logical OR: Returns True if
one of the statements is true
notnum_1 not num_2Logical NOT: Returns False if the result is true.
Returns True if the result is false

Example

num_1 = 2
num_2 = 5
num_3 = 10

print(num_1 < num_2 and num_3 > num_2) # True
print(num_1 > num_2 or num_3 > num_2) # True
print(not(num_1 > num_2 or num_3 > num_2)) # False

Membership Operators

Membership Operators are used to test whether a value is in a sequence.

OperatorExampleMeaning
innum_1 in num_2Returns True if value is found in the sequence
not innum_1 not in num_2Returns True if value is not found in the sequence