Python Practical
Python Practical
Practical 2
• # output: x + y = 50
• print ( ‘x + y =’, x + y )
• When you execute the above program, you will get the below output.
• Run
• x + y = 50
y = 30
# output: x - y = - 10
Run
x - y = - 10
y = 10
# output: x * y = 150
Run
x * y = 150
y = 50
# output: x / y = 0.8
Run
x / y = 0.8
y = 10
# output: x % y = 0.
Run
x % y = 0
y = 40
# Output: x // y = 2
print('x // y =',x//y)
Run
x // y = 2
y = 95
# Output: x == y is False
print('x == y is',x==y)
Run:
x == y is False
y = 85
# Output: x != y is True
print('x != y is',x!=y)
Run:
x != y is true
equal operator is exclusively used to return the True statement if the values are not
equal.
3) Python (>) Greater Than Operator
This operator is mainly used in Python to return true if the Left hand value is greater
than right one. If left hand operand is greater than right hand operand, it returns True
as the output.
Ex: 12 is > 8, then the answer becomes True
07 is > 12, then the answer becomes False
Program:
ex :
x = 50
y = 25
Run:
x > y is True
y = 75
Run:
x < y is True
Run:
x >= y. Hence it is False
y = 100
Run:
x <=y is True
4) Python (*=) Multiply AND Operator
It multiplies the left hand operand with the right hand one and returns the value as
output.
Example;
x = 20, x*=5, the answer is x = 100.
y = 20
Output:
Ans: A and B is True
y = 20
# Output: x or y is True
print('x or y is',x or y)
Output:
Ans: a or b is True
y = 20
print('not x is',not x)
Output:
Ans: Not (a and b) is false.
Python Bitwise operators
1) Python & Binary AND Bitwise Operator
Description: This operator performs bit by bit AND operations on the values on either
side of the operator.
Example Program
a = 15
b = 3
print(a & b)
Output: 3
2) Python | Binary OR Bitwise Operator
Description: This operator performs bit by bit OR operations on the values on either
side of the operator.
Example Program
a = 08
b = 5
print(a | b)
Output: 13
3) Python ^ Binary XOR Bitwise Operator
Description: This operator performs bit by bit exclusive OR operations on the values
on either side of the operator.
Example Program
a = 15
b = 6
print(a ^ b)
Output: 09
4) Python ~ Binary Ones Complement Bitwise Operator
Description: The one’s complement of a number’s binary is returned.
Example Program:
a = 10
b = 4
print(~a)
Output: -11
b = 4
print(a << 2)
Output: 40
6) Python >> Binary Right shift Bitwise Operator
Description: The value to the left of the operator is shifted to the right as many times as
the value on the right of the operator.
a = 10
b = 4
print(a >> 2)
Output: 2
Practical : 3
Aim : Write a program to study conditional statements in python.
Program 3 :
num1=10
num2=20
num3=30
if(num1>num2):
if(num1>num3):
print("num1 is greater")
else:
print("num3 is greater")
elif(num2>num3):
print("num2 is greater")
else:
print("num3 is greater")
Program 4.2 :
Output :
The current number is 1
The current number is 2
The current number is 3
The current number is 4
The current number is 5
Program 4.3 :
for number in range(1,5):
print ("The current number is ",number)
print("---------------------------")
for number in range(1,7,2):
print ("The current number is ",number)
print("---------------------------")
for number in range(5,0,-1):
print ("The current number is ",number)
Output :
The current number is 1
The current number is 2
The current number is 3
The current number is 4
---------------------------
The current number is 1
The current number is 3
The current number is 5
---------------------------
The current number is 5
The current number is 4
The current number is 3
The current number is 2
The current number is 1
Practical :5
Aim : Write a program to understand the concept of functions in python.
Program 5.1 :
def my_function():
print("Its a Python function")
Program 5.2 :
def my_function(fname):
print(fname + " MBA")
Output :
Trisha MBA
Himanshi MBA
Mukul MBA
Practical :6
Aim : Write a program to perform different operations on strings in python.
Program 6.1 :
txt = "The best things in life are free!"
if "expensive" not in txt: // if operator
print("No, 'expensive' is NOT present.")
Program 6.2 :
b = "Himanshi"
print(b[2:5]) //slicing
Output :
man
Program 6.3 :
a = "Pyhton"
b = "2.0"
c=a+b
print(c)
Output : Python2.0
Practical : 7
Aim : Write a program to create a list and apply different operations to that list.
Program 7 :
list_of_airlines=["AL1","AL2","AL3"]
print("Iterating the list using range()")
for index in range(0,len(list_of_airlines)):
print(list_of_airlines[index])
print("Iterating the list using keyword in")
for airline in list_of_airlines:
print(airline)
Practical : 8
Aim : Write a program to create a tuple and apply different operations to that tuple. Also, show
that tuples are immutable.
Program 8.1 :
thistuple = ("apple", "banana", "cherry") // creating a tuple
print(thistuple)
Output : 3
Program 8.3 :
tuple1 = (1, 2, 33, 44, 6)
tuple1[4] = 10
Output :
TypeError: 'tuple' object does not support item assignment