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.
print(10 + 20) # Adding integersf1 = 23.25f2 = 1.72print(f1 + f2) # Adding float numbers togehterx_prin = 12y_prin = 5.5print(x_prin + y_prin) # Adding integer with float
Subtraction
Subtraction between two operands can be carried out using the negative (-
) symbol.
print(20 - 10) # Subtracting integersf1 = 23.25f2 = 1.72print(f1 - f2) # Subtracting decimal numbers togehterx_prin = 12y_prin = 5.5print(x_prin - y_prin) # Subtracting integer with float
Multiplication
Operands can be multiplied together using the asterisk (*
) symbol.
print(20 * 10) # Multiplying integersf1 = 23.25f2 = 1.72print(f1 * f2) # Multiplying decimal numbers togehterx_prin = 12y_prin = 5.5print(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.
print(20 ** 10) # Using integer as both the base and exponentf1 = 23.25f2 = 1.72print(f1 ** f2) # Using float as both the base and exponentx_prin = 12y_prin = 5.5print(y_prin ** x_prin) # Using float as the base and integer as the exponentprint(x_prin ** y_prin) # Using integer as the base and float as the exponent
Division
Division can be performed using the slash ...