7 Types of Python Operators that will ease your programming

Python has 7 types of operators. In this Python Operators article, we will discuss all of them in detail with examples.

Python, the fastest-growing major programming language, has risen in the ranks of programming languages, edging out Java this year and standing as the second most loved language (behind Rust) – Stack Overflow

First, let’s discuss what are operators.

What are Operators?

An operator is a symbol that will perform mathematical operations on variablesor on values. Operators operate on operands (values) and return a result. Python has 7 types of operators that you can use:

Arithmetic Operators

Relational Operators

Assignment Operators

Logical Operators

Membership Operators

Identity Operators

Bitwise Operators

Let’s take an example:

2+3

Here, + is an operator for addition. It adds 2 and 3 and prints 5 in the interpreter. This is an arithmetic operator.

Types of Python Operators

1. Python Arithmetic Operators

Let’s discuss arithmetic operators- they are the most common. You have done this earlier.

Python has seven arithmetic operators for different mathematical operations. They are:

+ (Addition)

– (Subtraction)

* (Multiplication)

/ (Division)

** (Exponentiation)

// (Floor division)

% (Modulus)

1.1. Addition Operator

The addition operator [+] adds two values and gives their sum.

Example:

>>> num1=7
>>> num2=4
>>> num1+num2

Output:

11

>>> 7+4.1

Output:

11.1

In this example, we added integers to integers and integers to floats.

1.2. Subtraction Operator

The subtraction operator [-] subtracts second value from first and gives their difference.

Example:

>>> num1=7
>>> num2=4
>>> num1-num2

Output:

3

>>> 7-4.1

Output:

2.9000000000000004

In this example, we subtracted integers from integers and floats from integers.

1.3. Multiplication Operator

The multiplication operator [*] multiplies two values and gives their product.

Example:

>>> num1=7
>>> num2=4
>>> num1*num2

Output:

28

>>> 7*4.1

Output:

28.699999999999996

In this example, we multiplied integers by integers and floats by integers.

1.4. Division operator

The division operator [/] divides one value by second and gives their quotient.

Example:

>>> num1=7
>>> num2=4
>>> num1/num2

Output:

1.75

>>> 7/4.1

Output:

1.707317073170732

In this example, we divided integers by integers and integers by floats. Dividing in Python 3 always gives a float result.

1.5. Exponentiation

The exponentiation operator [**] raises one value to power of second.

Example:

>>> num1=7
>>> num2=4
>>> num1**num2

Output:

2401

>>> 7**4.1

Output:

2916.7685197377978

In this example, we raise integers to the power of integers and integers to the power of floats.

1.6. Floor Division

The floor division operator [//] divides one value by second and gives their quotient rounded to the next smallest whole number.

Example:

>>> num1=7
>>> num2=4
>>> num1//num2

Output:

1

>>> 7**4.1

Output:

1.0

In this example, we floor-divided integers by integers and integers by floats.

1.7. Modulus

The modulus operator [%] divides one value by second and gives their remainder.

Example:

>>> num1=7
>>> num2=4
>>> num1%num2

Output:

3

>>> 7%4.1

Output:

2.9000000000000004

In this example, we floor-divided integers by integers and integers by floats.

Any doubts in TechVidvan’s Python Operators article till now? Mention them in the comment section.

2. Python Relational Operators

Now, let’s talk about relational operators. They are also called comparison operators and they compare values. Python has 6 relational operators:

> (Greater than)

< (Less than)

== (Equal to)

!= (Not equal to)

>= (Greater than or equal to)

<= (Less than or equal to)

2.1. Greater than

The greater than operator [>] returns True if the first value is greater than the second.

Example:

>>> num1=7
>>> num2=4
>>> num1>num2

Output:

True

>>> 7>4.1

Output:

True

In this example, we compared integers to integers and integers to floats.

2.2. Less than

The less than operator [<] returns True if the first value is less than the second.

Example:

>>> num1=7
>>> num2=4
>>> num1<num2

Output:

False

>>> 7<4.1

Output:

False

In this example, we compared integers to integers and integers to floats.

2.3. Equal to

The equal to operator [==] returns True if the first value is equal to the second.

Example:

>>> num1=7
>>> num2=4
>>> num1==num2

Output:

False

>>> 7==4.1

Output:

False

In this example, we compared integers to integers and integers to floats.

2.4. Not equal to

The not equal to operator [!=] returns True if the first value is not equal to the second.

Example:

>>> num1=7
>>> num2=4
>>> num1!=num2

Output:

True

>>> 7!=4.1

Output:

True

In this example, we compared integers to integers and integers to floats.

2.5. Greater than or equal to

The greater than or equal to operator [>=] returns True if the first value is greater than or equal to the second.

Example:

>>> num1=7
>>> num2=4
>>> num1>=num2

Output:

True

>>> 7>=4.1

Output:

True

In this Python operator example, we compared integers to integers and integers to floats.

2.6. Less than or equal to

The less than or equal to operator [<=] returns True if the first value is smaller than or equal to the second.

Example:

>>> num1=7
>>> num2=4
>>> num1<=num2

Output:

False

>>> 7<=4.1

Output:

False

In this example, we compared integers to integers and integers to floats.

Python Operator Precedence

Which operator evaluates first can be confusing. So we have some rules for this too. This is the precedence table that denotes which operator evaluates first:

Higher priority operators evaluate first. We can use this table to get the result of this expression:

>>> 4**2*4/3+4-2%4

Output:

23.333333333333332

Summary

So, this was all about TechVidvan’s Python operators article. Today, we learned about 7 types of operators in Python and their subtypes. These are arithmetic, relational, assignment, logical, membership, identity and bitwise. We also saw some examples of Python operators. And last, we studied operator precedence.