Python 1 To 13
Python 1 To 13
Program to demonstrate simple statements like printing the names (“Hello World”),
numbers, mathematical calculations, etc.
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
print("Hello World!")
#Output Hello
World!
#Mathematical Calculations
a = 20 b = 10
print("Addition:",a+b)
print("Subtraction:",a-b)
print("Division:",a/b)
print("Multiplication:",a*b)
print("Modulus:",a%b)
print("Power:",a**2)
#Output
Addition: 30
Subtraction: 10
Division: 2.0
Multiplication: 200
Modulus: 0
Power: 400
1
2. Program to find all prime numbers within a given range.
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
start=int(input("Enter number"))
end=int(input("Enter number"))
for n in range(start,end+1):
for i in range(2,n): if(n
%i)==0: break else:
print(n)
#Output
Enter number2
Enter number50
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
2
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
#Output
Enter the value of number10
Fibbonacci series is
0
1
1
2
3
5
8
13
21
34
3
4. Program to demonstrate the use of slicing in string.
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
#Output
4
5. Programs related to functions & modules
def fun_hello(name):
message="Hello "+name
return(message)
#Output
Enter your name: Student
Hello Student
#Keyword argument
def sum(a,b,c):
return(a+b+c)
print("Sum is",sum(b=3,a=7,c=10))
#Output
Sum is 20
#Default argument
welcome(name="student")
#Output
I am a student of Dhanaji Nana Mahavidyalaya,Faizpur
5
#Variable length argument
def vararg(*names):
print("Type of argument passed is",type(names))
print("The arguments passed are") for name in
names: print(name)
vararg("C","C++","Python")
#Output
Type of arguments passed is <class 'tuple'>
The arguments passed are
C
C++
Python
def getdimensions():
length=float(input("Enter The Length :-"))
width=float(input("Enter The Width :-"))
return length,width l,w=getdimensions()
print(l,w)
#Output
Enter The Length :-10
Enter The Width :-20
10.0 20.0
#Recursive function
def product(a,b):
if(a<b):
return product(b,a) elif(b!
=0):
return(a+product(a,b-1)) else:
return 0 a=int(input("Enter first
6
number: ")) b=int(input("Enter second
number: "))
print("Product is: ",product(a,b))
#Output
Enter first number: 12
Enter second number: 4
Product is: 48
def sq(n):
print("Square is", n*n)
import square
square.sq(5)
#Output
Square is 25
#Output
1.0
8.0
10.0
7
6. Write a program that demonstrate concept of Functional Programming.
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
# Lambda Function
area_square=lambda x: x*x #area of square is a^2
side=int(input('Enter a side value of square: '))
print(area_square(side))
#Output
Enter a side value of square: 5
25
#Mapping function
#Output
[1, 4, 9, 16]
l=[1,2,6,9,4,12,7,8] a=list(filter(lambda
p:(p%2==0),l))
print(a)
#Output
[2, 6, 4, 12, 8]
8
print(sub)
#Output
13
9
7. Program to demonstrate the use of list & related functions.
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B5
# Creating a list
l= ["apple", "banana", "cherry", "date"]
print(l)
#Output
[‘apple’, ‘banana’, ‘cherry’, ‘date’]
Second Fruit: banana
10
Updated List: ['apple', 'grape', 'cherry', 'date']
List After Adding Kiwi: ['apple', 'grape', 'cherry', 'date', 'kiwi']
List After Removing Cherry: ['apple', 'grape', 'date', 'kiwi']
Date is in List
Number of fruits in the List: 4
Sorted List: ['apple', 'date', 'grape', 'kiwi']
Reversed List: ['kiwi', 'grape', 'date', 'apple']
11
8. Program to demonstrate the use of Dictionary & related functions.
#Creating Dictionary
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
D=dict({1:'odd',2:'even'}) print(D)
#Output
{1: 'odd', 2: 'even'}
#Output
D['Name']: ABC
D['Age']: 22
#Traversing A Dictionary
D={'Name':'ABC','Age':22,'Subject':'Python'}
for i in D: print (i,":",D[i])
#Output
Name : ABC
Age : 22
Subject : 'Python'
#Update Dictionary
D={'Name':'ABC','Age':22,'Subject':'Python'}
D['Class']='SY BCA' print(D)
#Output
{'Name': 'ABC', 'Age': 22, 'Subject': 'Python', 'Class': 'SY BCA'}
12
print(D.popitem()) print(D) D.clear()
print(D) del D print(D)
#Output
22
{'Name': 'ABC', 'Subject': 'Python'}
('Subject', 'Python')
{'Name': 'ABC'}
{}
Traceback (most recent call last):
File "E:/Dictionary/remove.py", line 10, in <module>
print(D)
NameError: name 'D' is not defined
D={'Name':'ABC','Age':22,'Subject': 'Python'}
print(len(D)) #length() print(all(D)) #all()
print(any(D)) #any()
D1={'Name':'ABCD','Age':21,'Subject': 'Python'}
#print(cmp(D,D1)) #Compare x=D.get('Subject')
print(x)
#Output
3
True
True
Python
#Dictionary Methods
D={'Name':'ABC','Age':22,'Subject': 'Python'}
print(D.get('Age')) #get()
D1=D.copy() #copy()
print(D1) print(D.values())
#values print(D.keys())
#keys print(D.items())
#items
dict=dict.fromkeys(D) #Create New dictionary with keys
13
print(str(dict)) dict=dict.fromkeys(D,5)
print(str(dict))
print(sorted(D.keys())) #sort functions
print(sorted(D.items())) D2={1:'one',2:'two',3:'three'}
print(sorted(D2.values()))
D.clear() #delete
print(D)
#Output
22
{'Name': 'ABC', 'Age': 22, 'Subject': 'Python'}
dict_values(['ABC', 22, 'Computer Science']) dict_keys(['Name',
'Age', 'Subject'])
dict_items([('Name', 'ABC'), ('Age', 22), ('Subject', 'Python')])
{'Name': None, 'Age': None, 'Subject': None}
{'Name': 5, 'Age': 5, 'Subject': 5}
['Age', 'Name', 'Subject']
[('Age', 22), ('Name', 'ABC'), ('Subject', 'Python')]
['one', 'three', 'two']
{}
14
9. Program to demonstrate the use of Tuple.
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
#Creating tuple
t=("DNM",'p',4,8,12) print(t)
#Output
(‘DNM’, 'p', 4, 8, 12)
#Deleting Tuple
t=("DNM",'p',4,8,12)
print(t) del t print(t)
#Output
(‘DNM’, 'p', 4, 8, 12)
Traceback (most recent call last):
File "<string>", line 5, in <module>
NameError: name 't' is not defined
#Output p
8 12
15
#Output
('ABC', 'PQR', 'XYZ', 'GHI', 'SHUBH', 'LABH', 'NMU')
('ABC', 'PQR', 'XYZ', 'GHI', 'ABC', 'PQR', 'XYZ', 'GHI',) yes
ABC
PQR
XYZ
GHI
#Output
7
Any element are present in a tuple: True
Smallest element is: 1
Largest element is: 7
Sorted elements are: [1, 2, 3, 4, 5, 6, 7]
Sum of all elements is: 28
#Output
The count of i is: 3
The count of d is: 0
vowels=('a','e','i','o','u')
print("The index of i is:",vowels.index('i'))
#Output
The index of i is: 2
16
10. Program to demonstrate the working of Class and Objects.
Name:-Kundan Kishor Patil Div:- B
17
Roll no:- Date:- Batch no:- B4
class customer:
def __init__(self):
self.id='C0121'
self.name="Priya"
self.city="Faizpur"
def __del__(self):
print('Object destroyed')
c=customer(); c.putdata()
del c
#Output
Customer's id is : C0121
Customer's name is : Priya
Customer's city is : Faizpur
Object destroyed
18
11. Program to demonstrate the working of Inheritance
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
class college: def detail(self):
print('College: '+'Dhanaji Nana Mahavidyalaya, Faizpur')
class stud(college):
def __init__(self):
self.rno=121
self.name="Riya"
self.course="BCA"
def putdata(self):
print("Roll No:",self.rno);
print("Name:",self.name);
print("Course:",self.course);
s=stud();
s.putdata()
s.detail()
#Output
Roll No: 121
Name: Riya
Course: BCA
College: Dhanaji Nana Mahavidyalaya, Faizpur
19
12. Program to demonstrate the working of Overloading Methods
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
20
Name:-Kundan Kishor Patil Div:- B
Roll no:- Date:- Batch no:- B4
try:
a=int(input('Enter first no:'))
b=int(input('Enter second no:'))
try: print(a/b) except
ZeroDivisionError as e:
print("You are trying to divide by zero") except
BaseException as obj:
print('Type of exception:',type(obj)) print('Type
of exception:',obj.__class__) print('Type of
exception:',obj.__class__.__name__) else:
print('You have entered correct input') finally:
print("Always executed")
#Output
Enter first no:12
Enter second no:0
You are trying to divide by zero
You have entered correct input
Always executed
21
sal=int(input('Enter salary:')) mobileno=input('Enter Mobile
Number:') if sal<5000 or sal>70000 or len(mobileno)>10 or
len(mobileno)<10: raise Emp("Invalid salary or mobile number")
else:
print("Salary is:",sal)
print("Mobile number is:",mobileno)
#Output
Enter salary:56000
Enter Mobile Number:9987652436
Salary is: 56000
Mobile number is: 9987652436
22