0% found this document useful (0 votes)
10 views

4) Python Operators - Jupyter Notebook

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

4) Python Operators - Jupyter Notebook

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

8/6/23, 7:11 PM Python Operators - Jupyter Notebook

Arithmatic Operators
In [1]: a = 5
b = 6

add = a + b
add

Out[1]: 11

In [3]: a = 8
b = 6

subtract = a - b
subtract

Out[3]: 2

In [4]: a = 5
b = 6

mul = a * b
mul

Out[4]: 30

In [15]: a = 12
b = 6

div = a/b
div

Out[15]: 2.0

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 1/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [10]: a = 12
b = 6

floor_div = a//b
floor_div

Out[10]: 2

In [13]: a = 15
b = 6

mod = a%b
mod

Out[13]: 3

In [20]: a = 3
b = 4

exp = a**b
exp

Out[20]: 81

In [ ]: ​

Comparison Operators

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 2/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [23]: a = 6
b = 8

if a == b:
print('Values are equal')
else:
print('Values are not equal')

Values are not equal

In [25]: a = 8
b = 8

if a != b:
print('True')
else:
print('False')

False

In [27]: a = 4
b = 2

if a < b:
print('True')
else:
print('False')

False

In [30]: a = 8
b = 8

if a <= b:
print('True')
else:
print('False')

True

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 3/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [31]: a = 4
b = 25

if a > b:
print('True')
else:
print('False')

False

In [35]: a = 26
b = 25

if a >= b:
print('True')
else:
print('False')

True

In [ ]: ​

Logical Operators

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 4/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [40]: a = 10
b = 10
c = -10

if a > 0 and b > 0:
print('Both a and b are greater than 0')
print('\n')

if a > 0 and b > 0 and c > 0:


print('All variables a, b and c are greater than 0')
else:
print('Atleast one number is not greater than 0')

Both a and b are greater than 0

Atleast one number is not greater than 0

In [ ]: ​

In [44]: a = -1
b = 0
c = 10

if a > 0 or b > 0:
print('Either one of the number is greater than 0')
else:
print('No number is greater than 0 \n')

if b > 0 or c > 0:
print('Either one of the number is greater than 0')
else:
print('No number is greater than 0')

No number is greater than 0

Either one of the number is greater than 0

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 5/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [ ]: ​

In [53]: a = 9

if not a:
print('Boolean value of a is True')

if not (a%3==0 or a%5==0):


print(a, 'is not divisible by either 3 or 5')
else:
print(a, 'is divisible by either 3 or 5')

9 is divisible by either 3 or 5

In [ ]: ​

Assignment Operators
In [54]: x = 8
x

Out[54]: 8

In [57]: x = 8
x += 7 # x = x + 7
x

Out[57]: 15

In [58]: x = 8
x -= 7 # x = x - 7
x

Out[58]: 1

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 6/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [59]: x = 8
x *= 7 # x = x * 7
x

Out[59]: 56

In [61]: x = 8
x /= 2 # x = x / 2
x

Out[61]: 4.0

In [62]: x = 8
x //= 2 # x = x // 2
x

Out[62]: 4

In [64]: x = 9
x %= 2 # x = x % 2
x

Out[64]: 1

In [65]: x = 2
x **= 6 # x = x ** 6
x

Out[65]: 64

In [66]: x = 2
x = x ** 6
x

Out[66]: 64

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 7/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [67]: x = 2
x = x + 6
x

Out[67]: 8

In [ ]: ​

Membership Operators
In [69]: lst1 = [1,2,3,4,5,6]
lst2 = [4,5,6,7,8,9]

for i in lst1:
print(i)
if i in lst2:
print('Items are overlapping')
else:
print('Not Overlapping')

1
Not Overlapping
2
Not Overlapping
3
Not Overlapping
4
Items are overlapping
5
Items are overlapping
6
Items are overlapping

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 8/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [71]: x = 24
y = 20

lst = [10,20,30,40,50]


if x not in lst:
print(x, 'is not present in lst')
else:
print(x, 'is present in the lst')

if y not in lst:
print(y, 'is not present in lst')
else:
print(y, 'is present in the lst')

24 is not present in lst


20 is present in the lst

In [ ]: ​

Identity Operators
In [72]: x = 5
y = 5

print(x is y)

True

In [73]: x = 7
y = 5

print(x is y)

False

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 9/10


8/6/23, 7:11 PM Python Operators - Jupyter Notebook

In [ ]: ​

In [74]: x = 5
y = 5

print(x is not y)

False

In [75]: x = 7
y = 5

print(x is not y)

True

In [ ]: ​

localhost:8888/notebooks/2-PreGrad_Stuffs_August/Python Operators.ipynb 10/10

You might also like