Photo by DATAIDEA

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
# Let 
x = 10 
y = 5
# Addition
summation = x + y
print(summation)
15
# Subraction
difference = x - y
print(difference)
5
# Division
quotient = x / y
print(quotient)
2.0
# Multiplication
product = x * y
print(product)
50
# Exponentiation 
exponent = x ** y
print(exponent)
100000
# Remainder
remainder = x % y
print(remainder)
0
# Floor Division
floor = 10 / 4
print(floor)
2.5
# Perform in a sequence
ans = 10 * 3 / 2 + 1
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
x = 10
# Addition Ass
x += 5 # x = x + 5 => x = 10 + 5 => x = 15
print(x)
15
# Subraction Ass
x = 10
x -= 5 # x = x - 5 => x = 10 - 5 => x = 5
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: '))

weight = 56
height = 1.5

bmi = weight/(height**2)

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
x = 5
y = 4
z = x # x = 5 => z = 5

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
x = 4
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
y = 7
expression_2 = 10 > y or 4 > y
#                   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
z = 8
expression_3 = not(10 == z)
#               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
name = 'Tinye Robert'
print('Robert' not in name)
False

A few ads maybe displayed for income as resources are now offered freely. 🤝🤝🤝

Back to top