Categories: Operators

Operator in Python

Operators in Python programming language

In this tutorial, we will discuss Operator in Python programming language

Operators in Python

Python programming language provides a rich set of operators to manipulate variables. We can divide all the Python operators into the following groups:

  • Arithmetic Operators

  • Relational Operators

  • Assignment Operators

  • Unary Operator

  • Logical Operators

  • Bitwise Operators

     

    Arithmetic Operators

Arithmetic operators are used to calculating mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:

Example 1
>>> //#example for Arithmetic operator in Python
>>> //#we can use Python Shell like a calculator
>>> 56+46
102
>>> 58-35
23
>>> 21*20
420
>>> 24/6
4
>>> 34%4
2
>>> 54//6
9
>>> 4**5
1024
>>>

 

Example 2

>>> x=12
>>> y=5
>>> print("x+y =",x+y)
('x+y =', 17)
>>> print("x-y =",x-y)
('x-y =', 7)
>>> print("x*y =",x*y)
('x*y =', 60)
>>> print("x/y=",x/y)
('x/y=', 2)
>>> print("x%y=",x%y)
('x%y=', 2)
>>> print("x//y=",x//y)
('x//y=', 2)
>>> print("x**y=",x**y)
('x**y=', 248832)
>>>

 

Example

Assignment Operator

In the Python programming language, assignment operators are used to assigning a value to a particular variable

for example – x=10 is a simple assignment operator that assigns the value 10 to the variable x

Python assignment operators and examples are shown below.

Example

>>> 
>>> x=16
>>> y=4
>>> z=x+y
>>> z
20
>>> x+=y
>>> x
20
>>> x+=y
>>> x
24
>>> x/=y
>>> x
6
>>> x*=y
>>> x
24
>>> x&=y
>>> 
>>> x
0
>>>
>>> 
>>> x=5
>>> y=3
>>> 
>>> x**y
125
>>> x=5
>>> y=3
>>> x**=y
>>> 
>>> x
125
>>> x//=y
>>> x
41
>>> x^=y
>>> x
42
>>> x>>=y
>>> x
5
>>> x<<=y
>>> x
40
>>>

 

Relational Operators- (comparison operator)

Relational operators are used to finding the relationship(compare) of values. It returns either true or false according to the given condition

 

Example

>>> 
>>> 15>12
True
>>> 13>56
False
>>> 23<45
True
>>> 35<28
False
>>> 15==15
True
>>> 15==16
False
>>> 34!=23
True
>>> 37!=37
False
>>> 12<=23
True
>>> 34<=23
False
>>> 25>=25
True
>>> 34>=35
False
>>>

 

Logical Operator

Three logical operators are available in the Python language. like  and, not, or

Example
>>> 
>>> 
>>> x=True
>>> y=False
>>> 
>>> x and y
False
>>> x and x
True
>>> y and y
False
>>> 
>>> x or y
True
>>> x or x
True
>>> y or y
False
>>> 
>>> not x
False
>>> not y
True
>>> 
>>>

 

 

Special operators

Some type of special operators available in Python language for the special purpose

Identify Operators

Python language provides some identity operators for identification purpose

They are used to check when two values are located in the same memory location. It returns either true or false

Example
>>> a=5
>>> b=5
>>> c='Hello world'
>>> d='hello World'
>>> e=[34,56,78,43]
>>> f=[34,65,78,43]
>>> print(a is b)
True
>>> print(a is not b)
False
>>> print(c is d)
False
>>> print(c is not d)
True
>>> print(e is f)
False
>>> print(e is not f)
True
>>>

 

Membership Operators

Python language provides some membership operators for a value or variable is found in a given sequence

Example
>>> 
>>> x='Python language'    //string 
>>> y={1,'H',2,'e',3,'l'}  //tuple
>>> z=[34,67,85,43,25]     //list
>>> 
>>> print('h' in x)
True
>>> print('b' in x)
False
>>> print(3 in y)
True
>>> print(5 in y)
False
>>> print(85 in z)
True
>>> print(75 in z)
False
>>>

 

Bitwise operators

learn more about Python bitwise operator

 

Suggested for you

Pyton program to calculate electicity bill

All type of the operators in Java with example
Operator in C programming language with example
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago