0% found this document useful (0 votes)
4 views10 pages

Python Arithmetic Operators Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Python Arithmetic Operators Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] 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 [Link] 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 [Link] 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 [Link] 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 [Link] 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 [Link] 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 [Link] 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 [Link] 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 [Link] 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 [Link] 10/10

Common questions

Powered by AI

Identity operators 'is' and 'is not' optimize data handling by checking objects' memory positions rather than just their values. When 'x is y' evaluates to true, it indicates that both variables not only have the same value but also reference the same memory location, which allows for better memory management by reusing immutable objects like small integers. This efficiency can lead to performance improvements in programs .

The division operator (/) in Python results in a float division where the result is a floating-point number. For example, dividing 12 by 6 using the division operator results in 2.0 . On the other hand, floor division (//) divides and returns the largest integer less than or equal to the division result. For example, 12 // 6 results in 2 .

Membership operators 'in' and 'not in' check for the presence or absence of an element in a collection like a list. For example, given lst1 = [1,2,3,4,5,6] and lst2 = [4,5,6,7,8,9], 'for i in lst1: if i in lst2' checks if elements from lst1 also exist in lst2, printing that items are overlapping if the condition is met . This operation is essential for determining common elements in collections.

The modulo operator (%) returns the remainder of a division operation and is often used in conditional statements to test divisibility. For instance, 'if not (a%3==0 or a%5==0):' checks if 'a' is not divisible by both 3 and 5, executing a block of code if the condition is true . This is used, for example, to determine that a number like 9 is divisible by either 3 or 5 .

Comparison operators such as '==', '!=', '<', '>', '<=', and '>=' control program flow based on comparing variable values. For example, 'if a != b: print('True')' prints 'True' if 'a' does not equal 'b' . This condition allows branching logic where different operations can execute based on variable states, effectively controlling program decisions and responses.

Logical operators in Python, such as 'and', 'or', and 'not', allow for the combination of multiple conditions. For example, 'if a > 0 and b > 0:' checks that both 'a' and 'b' are greater than 0. If both conditions are met, the statement within the block is executed; otherwise, it is not . This powerful feature enables complex conditional logic by combining multiple simple conditions into a single decision-making structure.

The logical operator 'or' evaluates multiple conditions and returns true if at least one condition is satisfied. For example, 'if a > 0 or b > 0:' evaluates as true if either 'a' or 'b' is greater than 0 . This structure is useful for executing code blocks when multiple potential conditions can independently initiate an action, offering flexibility in program behavior decisions.

Arithmetic operators (+, -, *, /, %) combine with variables to produce results based on their mathematics operations. For instance, adding 'a + b' produces a sum, integers result in an integer, and involving a floating-point number with '/' gives a float. Division using '/' of 'a = 12' and 'b = 6' gives 2.0 . Arithmetic operations involving '/' or float numbers usually output floats, while others involving only integers yield integers.

Assignment operator combinations, such as '+=' and '*=', modify variables by performing an operation and reassigning the result back to the variable. For example, 'x += 7' adds 7 to 'x', updating its value to 15 if 'x' was initially 8 . Similarly, 'x *= 7' multiplies 'x' by 7, resulting in 56 if 'x' was initially 8 . These operations provide a shorthand way to update variables.

In Python, the 'is' operator checks whether two variables point to the same object in memory, rather than checking if their values are equal. For instance, x = 5 and y = 5 would return True for 'x is y' because integers between -5 and 256 are cached and reused in Python .

You might also like