You are on page 1of 20

10/26/2020 operators

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

Operator Name Example

+ 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 [4]: "iet "+"lucknow"

Out[4]: 'iet lucknow'

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

In [8]: 12*"BSNL "

Out[8]: 'BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL BSNL '

In [11]: "BSNL "*12*4

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"

TypeError: can't multiply sequence by non-int of type 'str'

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

Operator Example Same As

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to 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

Operator Example Same As

& AND Sets each bit to 1 if both bits are 1

Sets each bit to 1 if one of


OR
two bits is 1

^ XOR Sets each bit to 1 if only one of two bits is 1

~ NOT Inverts all the bits

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)

Out[37]: ('0b1100', '0b1110')

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

Operator Example Same As

= 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

^= 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

Out[46]: 'iet lucknow'

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

Out[50]: (10, 20, 30)

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

ValueError: too many values to unpack (expected 2)

In [55]: a,*b=1,10,30,40
a,b

Out[55]: (1, [10, 30, 40])

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

Out[59]: (10, 20)

In [60]: d,e

Out[60]: (88, 99)

In [61]: c

Out[61]: [30, 56, 66, 78]

5 ) Logical operators
Logical operators are used to combine conditional statements:

Operator Example Same As

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is true x < 5 or x < 4

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

In [63]: m1>80 and m1<83

Out[63]: False

In [64]: m1>80 or m1<83

Out[64]: True

In [65]: not True

Out[65]: False

file:///D:/operators.html 11/20
10/26/2020 operators

In [66]: not False

Out[66]: True

In [67]: a=100
a!=90

Out[67]: True

In [68]: not a!=90

Out[68]: False

6 ) Python Identity Operators


Identity operators are used to compare the objects, not if they are equal, but if they are actually
the same object, with the same memory location:

Operator Example Same As

is Returns True if both variables are the same object x is y

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

In [70]: a is b#&a==&b id(a)==id(b)

Out[70]: True

In [71]: id(a),id(b)

Out[71]: (2031018688, 2031018688)

In [87]: a,b=300,300
a is b,a==b

Out[87]: (False, True)

In [85]: a="iet"
b="iet"
a is b

Out[85]: True

file:///D:/operators.html 12/20
10/26/2020 operators

In [86]: a="iet luck"


b="iet luck"
a==b,a is b

Out[86]: (True, False)

In [73]: id(a),id(b)

Out[73]: (62371936, 62370592)

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).

Operator Example Same As

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

In [77]: s1="iet lucknow"


'x' in s1

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

In [81]: "tei" not in s1

Out[81]: True

In [82]: "iet" not in s1

Out[82]: False

In [84]: (2+4)/3,2+(4/3)

Out[84]: (2.0, 3.333333333333333)

file:///D:/operators.html 14/20
10/26/2020 operators

In [89]: import math


dir(math)

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

In [92]: import sys


sys.version

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

You might also like