...

/

Basic Python Operators

Basic Python Operators

Let's look at the different types of Python operators.

Introduction to operators

Operators are tokens that can be used to perform operations such as addition or subtraction. There are several different types of operators in Python, a few of which will be looked at in this lesson.

Over here, the numbers on which the mathematical operation is being applied are called operands and the symbol representing the operation is known as an operator.

It is easier to grasp the figure above using numbers along with an operator. An example of one of the arithmetic operators, the addition operator, can be seen below:

Arithmetic operators

Arithmetic operators are used for carrying out mathematical operations like addition or subtraction.

Except in the case of division (which always yields a float), the result of an operation will be an integer if both the operands are integers. Otherwise, it will be a float.

Addition

In Python, addition can be carried out using the plus (+) symbol.

Press + to interact
print(10 + 20) # Adding integers
f1 = 23.25
f2 = 1.72
print(f1 + f2) # Adding float numbers togehter
x_prin = 12
y_prin = 5.5
print(x_prin + y_prin) # Adding integer with float

Subtraction

Subtraction between two operands can be carried out using the negative (-) symbol.

Press + to interact
print(20 - 10) # Subtracting integers
f1 = 23.25
f2 = 1.72
print(f1 - f2) # Subtracting decimal numbers togehter
x_prin = 12
y_prin = 5.5
print(x_prin - y_prin) # Subtracting integer with float

Multiplication

Operands can be multiplied together using the asterisk (*) symbol.

Press + to interact
print(20 * 10) # Multiplying integers
f1 = 23.25
f2 = 1.72
print(f1 * f2) # Multiplying decimal numbers togehter
x_prin = 12
y_prin = 5.5
print(x_prin * y_prin) # Multiplying integer with float

Exponentiation

Exponentiation is an operation that involves raising a number to a power. It is represented by the double asterisk (**) symbol.

Press + to interact
print(20 ** 10) # Using integer as both the base and exponent
f1 = 23.25
f2 = 1.72
print(f1 ** f2) # Using float as both the base and exponent
x_prin = 12
y_prin = 5.5
print(y_prin ** x_prin) # Using float as the base and integer as the exponent
print(x_prin ** y_prin) # Using integer as the base and float as the exponent

Division

Division can be performed using the slash ...