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:

[wp_table id=6983/]
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.

[wp_table id=6985/]

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

[wp_table id=6923/]

 

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

[wp_table id=6988/]
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

[wp_table id=6992/]
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

[wp_table id=6989/]
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

[wp_table id=6945/]

learn more about Python bitwise operator

 

 

Suggested for you

Math function in C

Operator in Java language

Operator in C++ language

Operator in Python language

Data type and variable in Java

Data type and variable in C

Data type and variable in C++

Data type and variable in Python

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago