Python Programming (Basics)- Operators in Python

Operators in Python

Operator is a symbol used in various computations performed on operands.
For example:
>>>20+3
here 20 and 3 are operands and ‘+’ is operator.

Types of Operators in Python

Depending upon the number of operands and required computations ,operators are divided in following major types:
1. Arithmetic Operator
2. Assignment Operator
3. Comparison/Relational Operator
4. Bit-wise Operator
5. Logical Operator
6. Membership Operator
7. Identity Operator

Arithmetic Operator

Arithmetic operators are used for mathematical calculations like addition, subtraction, modulus, division etc.

OperatorDescriptionExamples
+It adds value on either sides of operator or can be used as unary operatotra=8
b=2
a+b=10
-It subtracts value of right operand from left operand or it can be used as unary minusa=8
b=2
a-b=6
*Multiplication of two operands a=8
b=2
a*b=16
/Divides left operand with right operanda=8
b=2
a/b=4.0
// (Floor operator)This is used to carry out floor division ie. removes decimal valuesa=8
b=2
a//b=4
%(modulus)It is used to find remainder of divisiona=8
b=2
a%b=0
**(exponent)It is used to perform exponential calculation on operators(ie, left operand raised to the power of right)a=8
b=2
a**b=64

Assignment Operator

‘=’ sign is the basic assignment operator used to assign values to variables.
In addition ,Python has a set of short hand assignment operators as follows:

OperatorDescriptionExampleEquivalent to
=It is used to assign values of right side operands to left side>>>a=5
>>>a
5
>>>b='itvoyagers"
>>>b
itvoyagers
a=5
b="itvoyagers"
+=It is used to add values of right side operands to left side operand and the result is assigned to left operandx+=4x=x+4
-=It is used to subtract values of right side operands from left side operand and the result is assigned to left operandx-=4x=x-4
*=It is used to multiply values of both operands and the result is assigned to left operandx*=4x=x*4
/=It is used to divide values of left side operands by right side operand and the result is assigned to left operandx/=4x=x/5
%=It is used to divide values of left side operands by right side operand and the resulting reminder is assigned to left operandx%=4x=x%4
**=It performs Exponential calculation and result is assigned to left operandx**=4x=x**4
//=It is used to divide values of left side operands by right side operand and the result is assigned to left operand by removing decimal placesx//=4x=x//4

Note : Other operator like logical , bit-wise etc can also be used as short hand assignment operator.

Comparison/Relational Operator

 It is used to compare operands.It is also called as Relational operator.
It returns Boolean values as output (True or False).

OperatorDescriptionExample

== (equal to)
This returns true if both operands are true otherwise false>>> a=8
>>> b=2
>>> c=8
>>> a==b
False
>>> a==c
True
> (greater than)This returns True if left operand is greater than right operand otherwise false>>> a=8
>>> b=2
>>> c=1
>>> a>b
True
>>> c>b
False
< (less than)This returns True if left operand is less than right operand otherwise false>>> a=8
>>> b=2
>>> c=10
>>> a False
>>> a True
>= (greater than or equal to)This returns True if left operand is greater than or equal to right operand otherwise false>>> a=5
>>> b=3
>>> c=5
>>> a>=c
True
>>> b>=a
False
>>> a>=b
True
<= (less than or equal to)This returns True if left operand is less than or equal to right operand otherwise false>>> a=8
>>> b=2
>>> c=2
>>> a<=b
False
>>> b<=c
True
>>> c<=a
True
!= (not equal to)This returns True when operands are not equal otherwise false>>> a=8
>>> b=2
>>> c=8
>>> a!=b
True
>>> a!=c
False

Bitwise Operator

 It is used to perform operation on bit.
It performs bit by bit operations.

OperatorNameDescriptionExample
&Binary ANDOperator copies a bit to the result if it exists in both the operands.
i.e. returns 1 when both operands are 1
and 0 otherwise
>>> w=5
>>> a=6
>>> w & a
4
>>> bin(a)
'0b110'
>>> bin(w)
'0b101'
>>> bin(4)
'0b100'
>>>
|Binary OROperator copies a bit to the result if it exists in any of the operands.
i.e. returns 1 when either of operand is 1
and 0 otherwise
>>> w=5
>>> a=6
>>> w | a
7
>>> bin(a)
'0b110'
>>> bin(w)
'0b101'
>>> bin(7)
'0b111'
^Binary XORIt copies the bit if it is set in one operand but not in both.
i.e.returns 1 if one operand is 1 and other is 0
and 0 otherwise
>>> w=5
>>> a=6
>>> w ^ a
3
>>> bin(a)
'0b110'
>>> bin(w)
'0b101'
>>> bin(3)
'0b11'
~Binary Ones ComplementIt is unary operator and has effect of flipping bits>>> w=5
>>> bin(w)
'0b101'
>>> ~w
-6
>>> bin(-6)
'-0b110'
<<Binary Left ShiftThe left operand value is moved to left by the number of bits specified in the right operand>>> w=5
>>> w << 2
20
>>> bin(20)
'0b10100'
>>Binary Right ShiftThe left operand value is moved to right by the number of bits specified in the right operand>>> w=5
>>> w >> 2
1
>>> bin(1)
'0b1'

Logical Operator

 This operator is used to perform logical operations used logical and, or and not.These operators are case sensitive.
It returns Boolean values as output (True or False).

Operator DescriptionExample
andLogical AND returns True if both operands are True.>>> p=100
>>> q=500
>>> r=700
>>> q>p and q>r
False
>>> q>p and r>q
True
orLogical OR returns True if either of the operands is True.>>> p=100
>>> q=500
>>> r=700
>>> p>q or q>r
False
>>> q>p or q>r
True
notLogical NOT is used to reverse the state of operand.>>> a=''
>>> not a
True
>>> b=10
>>> not b
False
>>> not q>p
False

Membership Operator

 This operator is used to check if given variable is a part of specified sequence or not.
This operator (in and not in) can be used with strings, lists etc
It returns Boolean values as output (True or False).

OperatorsDescriptionExample
inIt returns True if it finds variable in specified sequence , False otherwise>>> 'I' in 'INDIA'
True
>>> 'i' in 'INDIA'
False
not inIt returns True if it does not find variable in specified sequence, False otherwise>>> 'V' not in 'ITVoyagers'
False
>>> 'v' not in 'ITVoyagers'
True

Identity Operator

 This operator is used to compare memory location of two objects.
id() is used to show memory location of any object.
This operator (is and is not) can be used compare memory location of two objects.
It returns Boolean values as output (True or False).

OperatorsDescriptionExample
isIt returns True if the variable on both sides of operator are pointing to same object ,False otherwise>>> f=88
>>> o=100
>>> g=88
>>> id(f)
1872877376
>>> id(g)
1872877376
>>> id(o)
1872877568
>>> f is o
False
>>> f is g
True
is notIt returns True if the variable on both sides of operator are pointing to different object ,False otherwise>>> f=88
>>> o=100
>>> g=88
>>> id(f)
1872877376
>>> id(g)
1872877376
>>> id(o)
1872877568
>>> f is not o
True
>>> f is not g
False

Loader Loading…
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download

Note:

Order of operations plays important role in python programming.

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......

For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment