Uploaded by sahilshakya622

Ujju Python

advertisement
BCA 3 SEMESTER
BY
Ujjwal Azad
2021001334
Under Supervision
of Mr. Amit Kumar
Application Based Programming
In Python And Machine
Learning
SCHOOL OF ENGINEERING & TECHNOLOGY DEPARTMENT
OF COMPUTER SCIENCE AND ENGINEERING
Course Coordinator
Lab. Coordinator
Mr. Amit Kumar
Mr. Amit Kumar
SCHOOL OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LIST OF EXPERIMENTS
S.NO.
1.
2.
3.
4.
EXPERIMENT NAME
Program to implement all condition statement
Program to implement exception handling
Program to find the factorial of a given number using function
Write a program to print Fibonacci series using functions
5.
Write a program to demonstrate working of classes and objects
6.
Write a program to demonstrate class method
7.
Write a Program to demonstrate constructors
Program to implement searching and sorting.
8.
9.
10.
Program to implement lists
Program to implement dictionaries
EXPERIMENT 1:
Program to implement all condition statement
price=100
quantity=10
amount = price*quantity
if amount > 200:
if amount >1000:
print("The amount is greater than 1000")
else:
if amount 800:
print("The amount is between 800 and 1000")
elif amount 600:
print("The amount is between 600 and 1000")
else:
print("The amount is between 400 and 1000")
elif amount == 200:
print("Amount is 200")
else:
print("Amount is less than 200")
output : “The amount is between 400 and 1000.”
EXPERIMENT 2:
Program to implement exception handling
# import module sys to get the type of exception
import sys
randomList = ['a', 0, 2]
for entry in randomList:
try:
print("The entry is", entry)
r = 1/int(entry)
break
except:
print("Oops!", sys.exc_info()[0], "occurred.")
print("Next entry.")
print()
print("The reciprocal of", entry, "is", r)
output:
The entry is a
Oops! <class 'ValueError'> occurred.
Next entry.
The entry is 0
Oops! <class 'ZeroDivisionError'> occured.
Next entry.
The entry is 2
The reciprocal of 2 is 0.5
EXPERIMENT 3:
Program to find the factorial of a given number using function
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
output:
Factorial of 5 is 120
EXPERIMENT 4:
Write a program to print Fibonacci series using functions
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
output:
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
EXPERIMENT 5:
Write a program to demonstrate working of classes and objects
class Person:
def init (self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession
# Behavior (instance methods)
def show(self):
print('Name:', self.name, 'Sex:', self.sex,
'Profession:', self.profession)
# Behavior (instance methods)
def work(self):
print(self.name, 'working as a', self.profession)
# create object of a class
jessa = Person('Jessa', 'Female', 'Software
Engineer')
# call methods
jessa.show()
jessa.work()
output:
Name: Jessa Sex: Female Profession: Software Engineer
Jessa working as a Software Engineer
EXPERIMENT 6:
Write a program to demonstrate class method
class Person:
def init (self, name, age):
self.name = name
self.age = age
# a class method to create a
# Person object by birth year.
@classmethod
def fromBirthYear(cls, name, year):
return cls(name, date.today().year - year)
def display(self):
print("Name : ", self.name, "Age : ", self.age)
person = Person('mayank', 21)
person.display()
output:
Name : mayank Age :
21
EXPERIMENT 07:
Write a Program to demonstrate constructors
class Student:
# Constructor - parameterized
def init (self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("John")
student.show()
output:
This is parametrized constructor
Hello John
EXPERIMENT 08:
Program to implement searching and sorting
def bubblesort(list):
# Swap the elements to arrange in order
for iter_num in range(len(list)-1,0,-1):
for idx in range(iter_num):
if list[idx]>list[idx+1]:
temp = list[idx]
list[idx] = list[idx+1]
list[idx+1] = temp
list = [19,2,31,45,6,11,121,27]
bubblesort(list)
print(list)
output:[2, 6, 11, 19, 27, 31, 45, 121]
EXPERIMENT 09:
Program to implement lists
List = []
print("Blank List: ")
print(List)
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
# Creating a List of strings and accessing
# using index
List = ["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])
output:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
EXPERIMENT 10:
Program to implement dictionaries
# Initializing an empty Dictionary
Dictionary = {}
print("The empty Dictionary: ")
print(Dictionary)
# Inserting key:value pairs one at a time
Dictionary[0] = 'Javatpoint'
Dictionary[2] = 'Python'
Dictionary.update({ 3 : 'Dictionary'})
print("\nDictionary after addition of these elements: ")
print(Dictionary)
# Adding a list of values to a single key
Dictionary['list_values'] = 3, 4, 6
print("\nDictionary after addition of the list: ")
print(Dictionary)
# Updating values of an already existing Key
Dictionary[2] = 'Tutorial'
print("\nUpdated dictionary: ")
print(Dict)
# Adding a nested Key to our dictionary
Dictionary[5] = {'Nested_key' :{1 : 'Nested', 2 : 'Key'}}
print("\nAfter addtion of a Nested Key: ")
print(Dictionary)
output:The empty Dictionary:
{}
Dictionary after addition of these elements:
{0: 'Javatpoint', 2: 'Python', 3: 'Dictionary'}
Dictionary after addition of the list:
{0: 'Javatpoint', 2: 'Python', 3: 'Dictionary',
'list_values': (3, 4, 6)}
Updated dictionary:
{1: 'Javatpoint', 2: 'Python'}
After addtion of a Nested Key:
{0: 'Javatpoint', 2: 'Tutorial', 3: 'Dictionary',
'list_values': (3, 4, 6), 5: {'Nested_key': {1: 'Nested',
2: 'Key'}}}
Download