Practical_python
Practical_python
print('Hello, world!')
Output:
Hello,World!
operators in Python
a=7
b=2
print ('Sum: ', a + b)
print ('Subtraction: ', a - b)
print ('Multiplication: ', a * b)
print ('Division: ', a / b)
print ('Floor Division: ', a // b)
print ('Modulo: ', a % b)
print ('Power: ', a ** b)
Output:
Sum: 9
Subtraction: 5
Multiplication: 14
Division: 3.5
Floor Division: 3
Modulo: 1
Power: 49
Output:
Please enter value for P: 10
Please enter value for Q: 5
The Value of P after swapping: 5
The Value of Q after swapping: 10
Output:
Enter any number to test whether it is odd or even: 75
The number is odd
Enter any number to test whether it is odd or even: 22
The number is even
n = int (input("Enter the number of digits that you want in the Fibonacci sequence:"))
n1, n2 = 0, 1
count = 0
if n <= 0:
print (“The input is invalid. Please enter a positive integer.”)
elif n == 1:
print (“Fibonacci sequence up to n terms will be: “)
print (n1)
else:
print (“Fibonacci sequence up to the given terms will be: “)
while count < n:
print (n1)
nth = n1 + n2
n1 = n2
n2 = nth
count = count + 1
Output:
Enter the number of digits that you want in the Fibonacci sequence:10
Fibonacci sequence up to the given terms will be:
0
1
1
2
3
5
8
13
21
34
Output:
[3, 7, 8, 10, 11, 22, 33, 54, 100]
list1=[20,5,82,71,90,17,65,43]
largest=max(list1)
print(largest)
Output:
90
list1=[20,5,82,71,90,175,5,20]
unique_list=list(set(list1))
print(unique_list)
Output:
[5, 71, 175, 82, 20, 90]
Output:
Enter a number: 5
Factorial of the given number is: 120
Unit-2
1. Write a python function that accepts a string and calculates the numbers of lowercase and
uppercase letters.
Output:
Hello
This is Delhi
This is Paris
This is London
Output:
The division by zero operation is not allowed.
4.Write a python program that uses try, except, else and finally blocks.
def divide(x, y):
try:
result = x // y
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
else:
print("Yeah ! Your answer is :", result)
finally:
print('This is always executed')
Output:
Yeah ! Your answer is : 1.5
This is always executed
Sorry ! You are dividing by zero
This is always executed
x=0
try:
print(5 / x)
except ZeroDivisionError:
print("I am the except clause!")
else:
print("I am the else clause!")
finally:
print("I am the finally clause!")
Output:
I am the except clause!
I am the finally clause!
Output:
Total no of lines=4
s=("Enter a string:")
def is_palindrom(s):
if(s=s[::-1]):
print("This is a Palindrom")
else
print("This is not a Palindrom")
is_palindrom(s)
Output:
Enter a string:level
This is a Palindrom
Chap - 3
class Parent:
def parentMethod(self):
print ("Calling parent method")
class Child(Parent):
def childMethod(self):
print ("Calling child method")
c = Child()
c.childMethod()
c.parentMethod()
Output:
Calling child method
Calling parent method
class A:
def Disp(self):
print("This is Parent class Method")
class B(A):
def Disp(self):
#super().Disp()
print("This is Child class Method")
obj=B()
obj.Disp()
Output:
This is Child class Method
3. Write a python class that represents a rectangle, including methodsfor calculating ite area
and perameter.
class rectangle():
def __init__(self,breadth,length):
self.breadth=breadth
self.length=length
def area(self):
return self.breadth*self.length
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())
Output:
Enter length of rectangle: 2
Enter breadth of rectangle: 3
Area of rectangle: 6
class India():
def capital(self):
print("New Delhi is the capital of India.")
def language(self):
print("Hindi is the most widely spoken language of India.")
def type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.")
def language(self):
print("English is the primary language of USA.")
def type(self):
print("USA is a developed country.")
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()
country.type()
Output:
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
class CustomException(Exception):
pass
def divide(a, b):
try
result = a / b
except ZeroDivisionError:
print("Division by zero!")
except CustomException as e:
print("Custom exception:", e)
else:
print("Result:", result)
finally:
print("Finally block executed.")
divide(10, 2)
divide(10, 0)
divide(10, "a")
Output:
Result: 5.0
Finally block executed.
Division by zero!
Finally block executed.
Finally block executed.
ERROR!
Traceback (most recent call last):
File "<main.py>", line 16, in <module>
File "<main.py>", line 5, in divide
TypeError: unsupported operand type(s) for /: 'int' and 'str'
Output:
Every car has 4 wheels
Speed is 100km/H
Every car has 4 wheels
Speed is 120Km/H
class A:
_a=10
__b=20
def show(self):
print("a=",self._a)
print("b=",self.__b)
obj=A()
obj.show()
print("Outside of the class:",obj._a)
print("Outside of the class:",obj.__b)
Output:
Outside of the class:10
Outside of the class:20
class Calculation1:
def Addition(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Addition(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
Chap - 4
Output:
2. Write a Python program that uses a generator to generate the Fibonacci sequence.
def fibonacci():
x, y = 0, 1
while True:
yield x
x, y = y, x + y
n = int(input("Input the number of Fibonacci numbers you want to generate? "))
print("Number of first ",n,"Fibonacci numbers:")
fib = fibonacci()
for _ in range(n):
print(next(fib),end=" ")
Output:
Input the number of Fibonacci numbers you want to generate? 10
Number of first 10 Fibonacci numbers:
0 1 1 2 3 5 8 13 21 34
3. write a python program that uses regular expression to validate an email address.
import re
email = "[email protected]"
alid = re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email)
if alid:
print("Valid email address")
else:
print("Invlid email address")
Output:
Valid email address
4. Write a Python program that uses list comprehension to create a new list based on an
existing one.
numbers = [1, 2, 3, 4]
doubled_numbers = [num * 2 for num in numbers]
print(doubled_numbers)
Output:
[2, 4, 6, 8]