top of page

OPERATORS IN PYTHON

Operator is a character or a symbol that represents a specific mathematical or logical action or process. Operators are used to perform operations on variables and values. For example: * returns product when applied on two numbers​ and > tells us which value is greater. In Python, we have different types of operators. Let us discuss them in detail.

Types of Operators

1. Arithmetic Operators

Arithmetical Operators are use to perform mathematical operations and returns calculated result. These operators work with numeric data type and  math's rule BODMAS is also applied here.

operators.JPG

Code 

Output

2. Relational Operators

Relational Operators are used to determine the relation between the two values. They are also called comparison operators and returns True if the comparison is true otherwise returns False if the comparison is false.  Relational Operators will always return a boolean value i.e. True or False.

3. Augmented Assignment Operators

Augmented assignment operators are the combination of assignment (=) operator with arithmetic operators (+ - * / % // **).

For example: x=x+5, can be written as x+=5.

Let us read all examples assuming value of variable x as 5.

4. Logical Operators

Logical Operators are basically used to connect two or more expressions with relational operators. These operators are used to create complex Boolean expressions. and, or, not are three logical operators offered by Python.

Before, we start with the examples here, we need to understand Truth Value Testing. It is a common way to show logical relationships between the expressions. Assuming x and y as expressions, see how logical operators work with them. 

and.JPG
x and y
or.JPG
x or y
not.JPG
not x

Operator Precedence

Since, you have learnt about so many operator by now, little confusion might arise while using multiple operators together which one should be evaluated first. As BODMAS, covers only few arithmetic operators, but here we have so many other as well. 

For example:

Try to evaluate this expression -   

>>>17>8+2**4 and 9//2>=4

Solution -

>>>17>8+16 and 4>=4 #Here ** got the highest precedence

>>>False and True

>>> False

Let us know the order of precedence for all operators in Highest to Lowest order:

Data Type Conversions

When Python is dealing with an expression of mixed data types, then it internally converts the data type to another data type, usually higher to avoid data loss. This is known as implicit conversion.

For example:

>>> 8.5+2 #adding a float and int

>>> 10.5  # returns float, means internally 2 was considered as 2.0

This process do not need any user involvement.

Explicit type conversion or Type Casting

An explicit conversion means a user defined conversion, where user uses any of the given conversion function to convert the value in a desired format.

  • int() function is used convert the string input into integer so that arithmetic operations can be performed. 

  • float() method is used convert the object input into float.

  • str() method is used to convert the object into string type.

  • The chr() method converts an integer to its Unicode character and returns it.

  • bool() method coverts a given value into a boolean value i.e. True or False

  • The type() method displays the data type of the object passed.

Code :

Output :

area.JPG
Try It Yourself

Try It Yourself

Problem Statement 1 - Input height in feet and convert into cm. Formula : cm= value in feet*30.48

Problem Statement 2 -  Input a two digit number and display sum of its digits.

For example: 25 should return 2+5=7

Assignment

Assignment

bottom of page