# Let
= 10
x = 5 y
Operators
Operators are symbols that perform operations on operands. Operands can be variables, strings, numbers, booleans etc
Arithmetic
Arithemators are symbols that perform mathematical operations on operands
Arithmetic Operator | Description |
---|---|
+ |
Addition |
- |
Subraction |
/ |
Division |
* |
Multiplication |
** |
Exponentiates |
% |
Remainder |
# Addition
= x + y
summation print(summation)
15
# Subraction
= x - y
difference print(difference)
5
# Division
= x / y
quotient print(quotient)
2.0
# Multiplication
= x * y
product print(product)
50
# Exponentiation
= x ** y
exponent print(exponent)
100000
# Remainder
= x % y
remainder print(remainder)
0
# Floor Division
= 10 / 4
floor print(floor)
2.5
# Perform in a sequence
= 10 * 3 / 2 + 1
ans print(ans)
16.0
Assignment
Assignment operators are used to assign values to variables.
Name | Operation | Same As |
---|---|---|
Assignment | x = y |
x = y |
Addition Ass | x += y |
x = x + y |
Subtraction Ass | x -= y |
x = x - y |
Mult Ass | x *= y |
x = x * y |
Division Ass | x /= y |
x = x / y |
Expo Ass | x **= y |
x = x ** y |
Remainder Ass | x %= y |
x = x % y |
Floor Div Ass | x //= y |
x = x // y |
# Examples
# Assignment
= 10
x # Addition Ass
+= 5 # x = x + 5 => x = 10 + 5 => x = 15
x print(x)
15
# Subraction Ass
= 10
x -= 5 # x = x - 5 => x = 10 - 5 => x = 5
x print(x)
5
Comparison
A comparison operator compares its operands and returns a Boolean value based on whether the comparison is True of False
Name | Operation |
---|---|
Equality | == |
Inequality | != |
Greater than | > |
Less than | < |
Greater or equal | >= |
Less or equal | <= |
# Examples
# Equality
'Voila' == 'Viola'
False
# Inequality
'Voila' != 'Viola'
True
# Greater or Equal
34 >= 43
False
# Tip
print('Voila' == 'Viola' == 'Voila')
# False == True => False
False
# weight = int(input("Enter your weight: "))
# height = int(input('Enter your height: '))
= 56
weight = 1.5
height
= weight/(height**2)
bmi
if bmi > 28:
print('You are over weight')
elif bmi > 18:
print('You are normal weight')
else:
print('You are under weight')
You are normal weight
Identity
Identity operators are used to compare two values to determine if they point to the same object
Operator | Name |
---|---|
is |
The is operator |
is not |
The is not operator |
# Example
# is
= 5
x = 4
y = x # x = 5 => z = 5
z
print(x is not z)
False
Logical
Logical operators are commonly used with Booleans. In Python, there are 3 logical operators
Operator | Description |
---|---|
and |
Logical and operator |
or |
Logical or |
not |
Logical not |
Logical and
The logical and
operator returns True
if both operands are True
# Example
# Logical and
= 4
x print(x > 3 and 8 < x)
# True and False => False
False
Logical or
The logical or
operator returns True
if one of the operands is True
# Logical or
= 7
y = 10 > y or 4 > y
expression_2 # True or False => True
print(expression_2)
True
Logical not
The logical not
operator returns True
if the operand is False
, otherwise returns False
if the operand is True
# Logical not
= 8
z = not(10 == z)
expression_3 # not False => True
print(expression_3)
True
Membership
Membership operators are used to check if a sequence is present in an object like a string, list etc
Operator | Name |
---|---|
in |
The in operator |
not in |
The not in operator |
# Example
= 'Tinye Robert'
name print('Robert' not in name)
False