Skip to content

Operator

1. Arithmetic Operator

  • ex: +, -, *, /, //, % (modulus), ** (exponentiation) List of Arithmetic Operator

  • code:

arithmetic.py

py
# +  -  * / 
print("1 + 1 = ", 1 + 1)
print("2 - 1 = ", 2 - 1)
print("2 * 3 = ", 2 * 3)
print("6 / 2 = ", 6 / 2)

# ** // %
print("2 ** 3 = ", 2 ** 3) # 2 power of 3 = 8
print("5 // 2 = ", 5 // 2) 

"""
5 / 2 = 2.5
5 // 2 = 2

// only get the integer part of the division, and discard the decimal part.
"""

print("5 % 2 = ", 5 % 2) # get the remainder of the division

Results

1 + 1 =  2
2 - 1 =  1
2 * 3 =  6
6 / 2 =  3.0
2 ** 3 =  8
5 // 2 =  2
5 % 2 =  1

2. Assignment operator (=)

  • Use when we want to assign a data to a variable name.

  • code:

assignment-operator.py

py
name = "John"
print(name)  # Output: John

INFO

"=" here is assignment operator

Results

John

3. Compound Assignment Operators

List of Compund Assignment Operator

Equivalent operators format

OperatorsEquivalnet to -
a += ba = a + b

Code:

compound-assignment-operator.py

py
# define a, b, c

a = 10
b = 5
c = 2

# Usage
# 1. a + b = 15

a = a + b
print(a)  # Output: 15

a += c # equivalent to a = a + c
print(a)  # Output: 17

Results:

15
17