Professional Documents
Culture Documents
Python Operators ¶
The operator can be defined as a symbol which is responsible for a particular operation
between two operands.Operators are used to perform operations on variables and values.
Operators are the pillars of a program on which the logic is built in a specific
programming language
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
1 ) Arithmetic operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
file:///D:/operators.html 1/20
10/26/2020 operators
file:///D:/operators.html 2/20
10/26/2020 operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
In [1]: 2+34
Out[1]: 36
In [2]: "2"+"34"
Out[2]: '234'
In [5]: [1,2,3]+[4,5,6]
Out[5]: [1, 2, 3, 4, 5, 6]
In [6]: 23-10
Out[6]: 13
In [7]: 12*13
Out[7]: 156
Out[8]: 'BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL '
Out[11]: 'BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL B
SNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSN
L BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL
BSNL BSNL '
In [ ]:
file:///D:/operators.html 3/20
10/26/2020 operators
In [10]: "A"*"B"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-4dc308e1ad79> in <module>
----> 1 "A"*"B"
In [12]: 12/5
Out[12]: 2.4
In [13]: 12.2/3.1
Out[13]: 3.9354838709677415
In [14]: (1+4j)/(3+6j)
Out[14]: (0.6+0.13333333333333333j)
In [15]: 100%8
Out[15]: 4
In [16]: 20%7
Out[16]: 6
In [17]: 5**2
Out[17]: 25
In [18]: 1.2**2
Out[18]: 1.44
In [19]: 5/2,5//2
Out[19]: (2.5, 2)
In [ ]:
2 ) Comparision operators
Comparison operators are used to compare values. It returns either True or False according to
the condition.
file:///D:/operators.html 4/20
10/26/2020 operators
== Equal x == y
!= Not equal x != y
In [20]: a=10
b=20
c=10
In [21]: a>b
Out[21]: False
file:///D:/operators.html 5/20
10/26/2020 operators
In [22]: a<b
Out[22]: True
In [23]: a==b
Out[23]: False
In [24]: a==c
Out[24]: True
In [25]: a!=b
Out[25]: True
In [26]: a>=b
Out[26]: False
In [27]: a>=c
Out[27]: True
In [28]: "hyd"=="hyd"
Out[28]: True
In [29]: [1,2]==[3,4]
Out[29]: False
3 ) Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit,
hence the name.
file:///D:/operators.html 6/20
10/26/2020 operators
Zero fill left Shift left by pushing zeros in from the right and let the leftmost bits fall
<<
shift off
Signed right Shift right by pushing copies of the leftmost bit in from the left, and let
>>
shift the rightmost bits fall off
file:///D:/operators.html 7/20
10/26/2020 operators
In [42]: 100>>5
Out[42]: 3
In [33]: bin(100)
Out[33]: '0b1100100'
In [34]: res=0b0110010
res
Out[34]: 50
In [35]: 100<<2
Out[35]: 400
In [36]: a=12
b=14
a&b
Out[36]: 12
In [37]: bin(a),bin(b)
In [38]: a|b
Out[38]: 14
In [39]: a^b
Out[39]: 2
In [41]: ~a+1
Out[41]: -12
In [44]: "iet"=='iet1'
Out[44]: False
4 ) Assignment operators
Assignment operators are used in Python to assign values to variables.
file:///D:/operators.html 8/20
10/26/2020 operators
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
= x =3 x=x 3
^= x ^= 3 x=x^3
In [45]: a=4
a+=5#a=a+5
a
Out[45]: 9
file:///D:/operators.html 9/20
10/26/2020 operators
In [46]: s1="iet"
s1+=" lucknow"
s1
In [47]: a=[1,2]
a+=[3,4]#a=a+[3,4]
a
Out[47]: [1, 2, 3, 4]
In [48]: a=10
b=20
c=30
In [49]: a,b,c=10,20,30
In [50]: a,b,c
In [51]: x=1,2,3
x
Out[51]: (1, 2, 3)
In [52]: p,q,r=x
q
Out[52]: 2
In [53]: a,b=1,10,30,40
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-53-fb029b44cf7e> in <module>
----> 1 a,b=1,10,30,40
In [55]: a,*b=1,10,30,40
a,b
In [56]: a,*b,c=1,2,3,4,5,6,7
b
Out[56]: [2, 3, 4, 5, 6]
file:///D:/operators.html 10/20
10/26/2020 operators
In [57]: a,c
Out[57]: (1, 7)
In [58]: a,b,*c,d,e=10,20,30,56,66,78,88,99
In [59]: a,b
In [60]: d,e
In [61]: c
5 ) Logical operators
Logical operators are used to combine conditional statements:
and Returns True if both statements are true x < 5 and x < 10
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
In [62]: m1=85
m1>80 and m1<90
Out[62]: True
Out[63]: False
Out[64]: True
Out[65]: False
file:///D:/operators.html 11/20
10/26/2020 operators
Out[66]: True
In [67]: a=100
a!=90
Out[67]: True
Out[68]: False
is not Returns True if both variables are not the same object x is not y
In [69]: a=100
b=100
a==b
Out[69]: True
Out[70]: True
In [71]: id(a),id(b)
In [87]: a,b=300,300
a is b,a==b
In [85]: a="iet"
b="iet"
a is b
Out[85]: True
file:///D:/operators.html 12/20
10/26/2020 operators
In [73]: id(a),id(b)
In [74]: a is not b
Out[74]: True
7 ) Membership operators
in and not in are the membership operators in Python. They are used to test whether a value or
variable is found in a sequence (string, list, tuple, set and dictionary).
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y
In [75]: a=[1,2,3,4,5]
2 in a
Out[75]: True
In [76]: 10 in a
Out[76]: False
Out[77]: False
In [78]: 'e' in s1
Out[78]: True
In [79]: "iet" in s1
Out[79]: True
file:///D:/operators.html 13/20
10/26/2020 operators
In [80]: "tei" in s1
Out[80]: False
Out[81]: True
Out[82]: False
In [84]: (2+4)/3,2+(4/3)
file:///D:/operators.html 14/20
10/26/2020 operators
file:///D:/operators.html 15/20
10/26/2020 operators
Out[89]: ['__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'acos',
'acosh',
'asin',
'asinh',
'atan',
'atan2',
'atanh',
'ceil',
'copysign',
'cos',
'cosh',
'degrees',
'e',
'erf',
'erfc',
'exp',
'expm1',
'fabs',
'factorial',
'floor',
'fmod',
'frexp',
'fsum',
'gamma',
'gcd',
'hypot',
'inf',
'isclose',
'isfinite',
'isinf',
'isnan',
'ldexp',
'lgamma',
'log',
'log10',
'log1p',
'log2',
'modf',
'nan',
'pi',
'pow',
'radians',
'remainder',
'sin',
'sinh',
'sqrt',
'tan',
'tanh',
'tau',
'trunc']
file:///D:/operators.html 16/20
10/26/2020 operators
Out[92]: '3.7.1 (default, Dec 10 2018, 22:09:34) [MSC v.1915 32 bit (Intel)]'
file:///D:/operators.html 17/20
10/26/2020 operators
In [91]: dir(sys)
file:///D:/operators.html 18/20
10/26/2020 operators
Out[91]: ['__breakpointhook__',
'__displayhook__',
'__doc__',
'__excepthook__',
'__interactivehook__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'__stderr__',
'__stdin__',
'__stdout__',
'_clear_type_cache',
'_current_frames',
'_debugmallocstats',
'_enablelegacywindowsfsencoding',
'_framework',
'_getframe',
'_git',
'_home',
'_xoptions',
'api_version',
'argv',
'base_exec_prefix',
'base_prefix',
'breakpointhook',
'builtin_module_names',
'byteorder',
'call_tracing',
'callstats',
'copyright',
'displayhook',
'dllhandle',
'dont_write_bytecode',
'exc_info',
'excepthook',
'exec_prefix',
'executable',
'exit',
'flags',
'float_info',
'float_repr_style',
'get_asyncgen_hooks',
'get_coroutine_origin_tracking_depth',
'get_coroutine_wrapper',
'getallocatedblocks',
'getcheckinterval',
'getdefaultencoding',
'getfilesystemencodeerrors',
'getfilesystemencoding',
'getprofile',
'getrecursionlimit',
'getrefcount',
'getsizeof',
'getswitchinterval',
'gettrace',
'getwindowsversion',
file:///D:/operators.html 19/20
10/26/2020 operators
'hash_info',
'hexversion',
'implementation',
'int_info',
'intern',
'is_finalizing',
'last_traceback',
'last_type',
'last_value',
'maxsize',
'maxunicode',
'meta_path',
'modules',
'path',
'path_hooks',
'path_importer_cache',
'platform',
'prefix',
'ps1',
'ps2',
'ps3',
'set_asyncgen_hooks',
'set_coroutine_origin_tracking_depth',
'set_coroutine_wrapper',
'setcheckinterval',
'setprofile',
'setrecursionlimit',
'setswitchinterval',
'settrace',
'stderr',
'stdin',
'stdout',
'thread_info',
'version',
'version_info',
'warnoptions',
'winver']
file:///D:/operators.html 20/20