1/25/22, 12:57 PM 2.
Operations in Python
Operations in Python
### Good Morning
# Experiment2 Date : 25 Jan 2022
# Aim:- Perform different mathematical operation
in Python
## Name:-
### Roll No:-
Arithmatic Operations
In [39]: 13+5 #addition
Out[39]: 18
In [40]: 13-5 # subtraction
Out[40]: 8
In [41]: 13*5 # Multiplication
Out[41]: 65
In [17]: 13/5 #division gives quotient
Out[17]: 2.6
In [27]: 13 % 5 # Modulus operator gives remaindaer
Out[27]: 3
In [19]: 13**5 # Exponent operator
Out[19]: 371293
In [20]: # Integer division ( Floor division). Here we will quotient in integer
13 //5
Out[20]: 2
# several arithmatic operation: -
When an expression conyains several mathamatical operations, the sequence of
operation is as follows
localhost:8888/notebooks/Skill Lab Sem-IV Python/2. Operations in Python.ipynb#Operations-in-Python 1/6
1/25/22, 12:57 PM 2. Operations in Python
## parentheses
## exponential
## Multication, division,modulus, floor division
## addition and subtraction
## assignment operator
In [42]: x=1; y=2; z=3; a=2; b=2 ; c=2;
In [31]: d = (x+y)*z**a//b+c
In [32]: d
Out[32]: 15
# Assignment operator
In [38]: x=4; z=4
In [37]: z+=x
z
Out[37]: 8
In [32]: z+=x # z=z+x
In [33]: z
Out[33]: 8
In [34]: z-=x # z=z-x
In [35]: z
Out[35]: 4
In [36]: z*=x # z=z*x
In [37]: z
Out[37]: 16
In [38]: z/=x # z=z/x
localhost:8888/notebooks/Skill Lab Sem-IV Python/2. Operations in Python.ipynb#Operations-in-Python 2/6
1/25/22, 12:57 PM 2. Operations in Python
In [39]: z
Out[39]: 4.0
In [40]: z%=x # z=z%x store remainder
In [41]: z
Out[41]: 0.0
In [42]: z**=x # z=z**x # z power x
In [43]: z
Out[43]: 0.0
In [44]: z//=x # z=z//x
In [45]: z
Out[45]: 0.0
In [51]: # Unary minus operator ( it negate the value of variable)
x=10
print(-x)
-10
In [52]: p = 10
print(-p)
-10
In [53]: p=10
print(-p)
-10
In [50]: p= -10
print(-p)
10
In [ ]: # Note:- Python does not have increment (++)or decrement operator (--) like in C++
# Relational Operatos :
localhost:8888/notebooks/Skill Lab Sem-IV Python/2. Operations in Python.ipynb#Operations-in-Python 3/6
1/25/22, 12:57 PM 2. Operations in Python
Used to compare two quantities i.e whether the two values are same or which one
is bigger or ehich one is lesser.
these operatioes will result in True or False
a>b
a<b
a>=b
a>=b
a==b
a!=b
In [54]: 5>3
Out[54]: True
In [55]: 3>7
Out[55]: False
In [56]: 4>=4
Out[56]: True
In [57]: 4<=2
Out[57]: False
In [58]: 5!=5
Out[58]: False
In [59]: 5 == 5
Out[59]: True
# Logical operators and Boolean Operators
Logical operators are used to combine more than one simple condition i.e
compound condition. each of the condition is evaluated True or False then
dicision is taken whether the total condition is true or False.
eg if (x>y or y>z): print('yes')
## 3 logical operatora are:- 'and' , 'or' , 'not'
In [2]: a = True
b = False
a and b
Out[2]: False
localhost:8888/notebooks/Skill Lab Sem-IV Python/2. Operations in Python.ipynb#Operations-in-Python 4/6
1/25/22, 12:57 PM 2. Operations in Python
In [3]: a or b
Out[3]: True
In [4]: a and a
Out[4]: True
In [5]: not a
Out[5]: False
Bitwise Operator
these operators act on individual binary bit of operands. we can use use it directly on binary
numbers or on integers.
In case of integer operands, it first converted to binary then does bitwise
operation and gives result in interger number.
In [6]: x =10
y=11
In [7]: ~x
Out[7]: -11
In [8]: x & y # and operation
Out[8]: 10
In [9]: x | y # or
Out[9]: 11
In [10]: x^y # XOR
Out[10]: 1
In [11]: x<<2 #bitwise left shift operator
Out[11]: 40
In [12]: x>>2 # bitwise right shift
Out[12]: 2
localhost:8888/notebooks/Skill Lab Sem-IV Python/2. Operations in Python.ipynb#Operations-in-Python 5/6
1/25/22, 12:57 PM 2. Operations in Python
In [ ]:
localhost:8888/notebooks/Skill Lab Sem-IV Python/2. Operations in Python.ipynb#Operations-in-Python 6/6