Python Operators Complete Tutorial

In this section, we will learn what the operators are and how to use them in Python.

Operators in Python

In python, we’re allowed to run different types of operations. For example, we can add, multiply, divide, or subtract two or more values from each other!

Now Python provided a set of symbols and signs to be used for such operations and they are called operators.

For example, one of these operators is called multiplication, and we invoke the operator using the `*` symbol.

Symbol in Python

In Python each operator has a sign AKA symbol and we use those symbols to invoke the target operator etc.) For example, one of these operators is addition operator and its sign (or symbol) is `+`.

So yeah, symbols in Python are basically the signs of operators and we use them in different operations.

Python Operands:

Operators like multiplication or division etc. need two or more values in order to function properly. These values are called operands.

Python LValues and RValues

The value that we put on the left side of an operator like assignment `=` or multiplication `*` etc. is called Lvalue (L stands for Left) and the value we put on the right side of the operators is called RValue (R stands for Right).

Example: using lvalues and rvalues in Python

res = 10 +30

print(res)

Here we have two operators:

The assignment `=` operator and the addition `+` operator.

For the assignment `=` operator, the Lvalue is `res` and the RValue is the sum of `10+30`.

Also, for the addition operator `+`, the LValue is 10 and the RValue is 30.

List of Python Operators

In Python, there are multiple different types of operators for different purposes. In later sections, we will dive deep into each of these operators, but for now, here’s an introductory list of these operators:

Python Arithmetic Operators

These operators are used for arithmetic operations like addition, subtraction, multiplication, etc.

Operator Symbol Description
Addition Operator + Using this operator, we can add two values together.
Subtraction Operator Using this operator, we can subtract two values from each other.
Multiplication Operator * Using this operator, we can multiply two values to each other.
Division Operator / Using this operator, we can subtract two values from each other.
Exponentiation Operator ** This operator is used to raise the value of the left operand to the power of the right operand and then return the result.
Modulus Operator % Using this operator, we can run modulus operation on operands.
Floor Division // Using this operator, we can run floor division on operands.

Python Comparison Operators

These operators are used to compare two or move values to see if they are equal or one is greater than the other.

Operator Symbol Description
Equal == Using this operator, we can check and see if two values are equal or not.
Negate Equal != Using this operator, we can check and see if two values are NOT equal.
Greater than > Using this operator, we can check to see if the left operand is greater than the right operand.
Less than < Using this operator, we can check to see if the right operand is greater than the left operand.
Greater Than or equal to >= Using this operator, we can see if the left operand is either greater or equal compared to the right operand.
Less than or equal to <= Using this operator, we can see if the right operand is either greater or equal compared to the left operand.

Python Logical Operators

These operators are used to combine the results of comparing two or more comparison operators or even negate their results.

Operator Description
and Using this operator, we can combine the result of two expressions and decide whether the final result should be True or False.
or Using this operator, we can combine the result of two boolean expressions and decide whether the final result should be True or False
not Using this operator, we can negate the result of an expression that results either True or False

Python Assignment Operators

We use the assignment operators to assign a value to a variable (making a variable to point to a value in a memory).

Operator Symbol
Assignment operator =
Addition and Assignment operator +=
Subtraction and Assignment operator -=
Multiplication and Assignment operator *=
Division and Assignment operator /=
Exponentiation and Assignment Operator **=
Modulus and Assignment Operator %=
Floor Division and Assignment Operator //=

Identity Operators in Python

The identity operators are used to see if two values are the same or not. (if they live in the same memory space or they have two separate and independent locations in the memory).

Operator Description
is Using this operator, we can see if two values are the same (live in the same memory location).
is not Using this operator, we can see if two values are not the same! (They live in two separate memory location).

Membership Operators in Python

Using these operators, we can check and see if a list or tuple or dictionary (basically those types that can take multiple values) has a specific value on their list or not.

Operator Description
in Using this operator, we can check to see if an object contains a value or not.
no in Using this operator, we can check to see if an object does not contain a value!
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies