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.
Operator | Example | Meaning |
---|---|---|
+ | num_1 + num_2 | Addition: adds two operands |
– | num_1 – num_2 | Subtraction: subtracts two operands |
* | num_1 * num_2 | Multiplication: multiplies two operands |
/ | num_1 / num_2 | Division: divides the first operand by the second |
% | num_1 % num_2 | Modulus: returns the remainder |
** | num_1 ** num_2 | Exponentiation: Returns first raised to power second |
// | num_1 // num_2 | Floor 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
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.
Operator | Example | Meaning |
---|---|---|
== | num_1 == num_2 | Equal to: returns True if both operands are equal |
!= | num_1 != num_2 | Not equal: returns True if operands are not equal |
> | num_1 > num_2 | Greater than: returns True if the num_1 is greater than the num_2 |
< | num_1 < num_2 | Less than: returns True if the num_1 is less than the num_2 |
>= | num_1 >= num_2 | Greater than or equal to: returns True if the num_1 is greater than or equal to the num_2 |
<= | num_1 <= num_2 | Less 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
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.
Operator | Example | Same as | Meaning |
---|---|---|---|
= | num = 2 | num = 2 | Assign value of right side of expression to left side operand |
+= | num += 2 | num = num + 2 | Add |
-= | num -= 2 | num = num – 2 | Subtract |
*= | num *= 2 | num = num * 2 | Multiply |
/= | num /= 2 | num = num / 2 | Divide |
%= | num %= 2 | num = num % 2 | Modulus |
//= | num //= 2 | num = num // 2 | Divide(floor) |
**= | num **= 2 | num = num ** 2 | Exponent |
&= | num &= 2 | num = num & 2 | Performs Bitwise AND on operands |
|= | num |= 2 | num = num | 2 | Performs Bitwise OR on operands |
^= | num ^= 2 | num = num ^ 2 | Performs Bitwise xOR on operands |
>>= | num >>= 2 | num = num >> 2 | Performs Bitwise right shift on operands |
<<= | num <<= 2 | num = num << 2 | Performs 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
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.
Operator | Example | Meaning |
---|---|---|
is | num_1 is num_2 | Returns True if both variables are identical |
is not | num_1 is not num_2 | Returns 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
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.
Operator | Example | Meaning |
---|---|---|
and | num_1 and num_2 | Logical AND: Returns True if both statements are true |
or | num_1 or num_2 | Logical OR: Returns True if one of the statements is true |
not | num_1 not num_2 | Logical 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
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.
Operator | Example | Meaning |
---|---|---|
in | num_1 in num_2 | Returns True if value is found in the sequence |
not in | num_1 not in num_2 | Returns True if value is not found in the sequence |