Bhanushali Python
Bhanushali Python
Practical no 1
A. Create a program that asks the user to enter their name and their age. Print out a message
addressed to them that tells them the year that they will turn 100 years old.
import datetime
name = input("Hello! Please enter your name: ")
print("Hello " + name)
age = int(input("Enter your age: "))
year_now = datetime.datetime.now()
# print(year_now.year) print("You will turn 100 in " + str(int(100-age) +int(year_now.year)))
Output:
B. Enter the number from the user and depending on whether the number is even or odd, print
out an appropriate message to the user.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output
nterms = 10
n1 = 0
n2 = 1
Page | 1
print("Fibonacci sequence upto",nterms,":")
count = 2
if nterms<= 0:
elif nterms == 1:
print(n1)
else:
print(n1,",",n2,end=', ')
print(nth,end=' , ')
n1 = n2
n2 = nth
count += 1
Output:
def reverse_number(number):
reverse = 0:
reminder = number % 10
number = number
reverse_number(1546)
Page | 2
Name:-Samruddhi Mali Roll no:-3131 Python Programming
E. Write a function to check the input value is Armstrong and also write the function for
Palindrome.
def armstrong(num):
sum=0
temp = num
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
else:
Page | 3
Name:- Samruddhi Mali Roll no:-3131 Python Programming
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))
Output:
print(f"Result: {result}")
Output:-
H) Write a program in python using if-else statement. Ask user to enter his age, if age<18 print “minor”
if age<65 print “Adult”
if age<100 print “senior citizen”.
-> CODE:
age = int(input("Enter your age: ")) if age < 18:
print("You are a minor.") elif age < 65:
print("You are an adult.") elif age < 100:
print("You are a senior citizen.") else:
print("You have entered an invalid age.")
OUTPUT:
I) Write a program in python to generate a table. Take input from the user by using For loop.
-> CODE:
num = int(input("Enter a number for the multiplication table: ")) terms =
int(input("Enter the number of terms for the table: "))
print(f"Multiplication table for {num} (up to {terms} terms):")
for i in range(1, terms + 1): result = num * i print(f"{num} x {i} = {result}")
OUTPUT:
Page | 5
Name:-Samruddhi Mali Roll no:-3131 Python Programming
J) Write a program in python to generate a table. Take input from the user by using While loop.
-> CODE:
num = int(input("Enter a number for the multiplication table: ")) terms =
int(input("Enter the number of terms for the table: "))
i=1
print(f"Multiplication table for {num} (up to {terms} terms):") while i <= terms:
result = num * i print(f"{num} x {i} = {result}") i += 1
OUTPUT:
H) Write a program to count even and odd numbers using for loop.
-> CODE:
start = int(input("Enter the start of the range: ")) end = int(input("Enter the
end of the range: ")) even_count = 0
odd_count = 0
for num in range(start, end + 1): if num % 2 == 0:
even_count += 1 else:
odd_count += 1
print(f"Number of even numbers: {even_count}") print(f"Number of odd
numbers: {odd_count}")
OUTPUT:
Page | 6
Name:- Samruddhi Mali Roll no:-3131 Python Programming
Practical no 2
A. Write a function that takes a character (i.e. a string of length 1) and returns True if it is a
vowel, False otherwise.
def find_vowel(s):
for i in s:
if i in l:
print("True")
else:
print("False")
s = "God is Great"
find_vowel(s)
Output:
def len_s(s):
count = 0
for i in s:
if i != "":
count += 1
s = "God is great"
len_s(s)
Output:
C. Define a procedure histogram() that takes a list of integers and prints a histogram to the
screen. For example, histogram([4, 9, 7]) should print the following:
****
*******
***
******
def histogram(l1):
for i in l1:
print("*" * i)
l1 = [4, 7, 3, 5]
histogram(l1)
Output:
E) Create a function which displays result based on the below conditions: If percentage <35-Failed
else:
return "Distinction"
user_percentage = float(input("Enter your percentage: ")) result =
calculate_result(user_percentage)
print(f"Result: {result}")
OUTPUT:
Page | 10
Name:- Samruddhi Mali Roll no:-3131 Python Programming
Practical no 3
A. A pangram is a sentence that contains all the letters of the English alphabet at least once, for
example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to
check a sentence to see if it is a pangram or not.
if sys.version_info[0] < 3:
alphaset = set(alphabet)
Output:
B. Take a list, say for example this one: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] and write a program
that prints out all the elements of the list that are less than 5.
l1=[1,1,2,3,5,8,13,21,34,55,89]
l2=[]
for i in l1:
if i < 5:
l2.append(i)
print(l2)
Output:
Page | 11
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Practical no 4
A. Write a program that takes two lists and returns True if they have at least one common
member
l1 = [1,2,3,4,5,6,]
for i in l1:
for j in l2:
if i == j:
Output:
B. Write a Python program to print a specified list after removing the 0th, 2nd, 4 th and 5th
elements.
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
l1.remove(l1[0])
l1.remove(l1[1])
print("After Removal of 1st element of New List (original 2nd index element) is", l1)
l1.remove(l1[2])
print("After Removal of 3rd element of New List (original 4th index element) is", l1)
l1.remove(l1[2])
print(l1)
Output:
Page | 12
Name:-Samruddhi Mali Roll no:-3131 Python Programming
l1=[2, 4, 7, 8, 9, 0]
l2=l1
Output:
Class Codes
1. l1=[10,20,30,[50,60,[70,80]]]
print(l1[-1][-1][-1])
print(l1[3][2][1])
Output:
print(l1[-1][-1][0], (l1[-1][-1][1]))
Output:
3.
py = t1[3][2][-1]
print(py[::2])
Page | 13
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Output:
4.
l1 = list(t1)
l1.append(50)
t1 = tuple(l1)
print(t1)
Output:
5.
t1[3].insert(2, 90)
print(t1)
Output:
Page | 14
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Practical no 5
A. Write a Python script to sort (ascending and descending) a dictionary by value.
released = {"Python 3.6": 2017, "Python 1.0": 2002, "Python 2.3": 2010}
print(key, value)
Output:
print (sorted(released))
Output:
B. Write a Python script to concatenate following dictionaries to create a new one. Sample
Dictionary : dic1={1:10, 2:20} dic2={3:30, 4:40} dic3={5:50,6:60}
dic1.update(dic2)
print(dic1)
dic1.update(dic3)
print(dic1)
Output:
Page | 15
Name:-Samruddhi Mali Roll no:-3131 Python Programming
print(sum(d.values()))
Output:
Class Codes
1.
Galaxy_M35 = {
"Brand": "Samsung",
"CPU_speed": "2.4hz",
Output:
2.
bookmyShow = {
["pvr", "cinemax"],
),
Page | 16
Name:-Samruddhi Mali Roll no:-3131 Python Programming
"Gold:K-7,K-8",
47.20,
447.20,
],
print(bookmyShow)
Output:
Page | 17
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Practical no 7
A. Design a class that store the information of student and display the same
class Student:
def __init__(self, name, sex, course, result):
self.name = name
self.sex = sex
self.course = course
self.result = result
def display(self):
print('Name:', self.name)
print('Sex:', self.sex)
print('Course:', self.course)
print('Result:', self.result)
student1 = Student("Alice", "Female", "Computer Science", "A")
student1.display()
Output:
Page | 18
Name:-Samruddhi Mali Roll no:-3131 Python Programming
square.area()
Output:
C. Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a
constructor which takes the parameters x and y (these should all be numbers). i. Write a
method called add which returns the sum of the attributes x and y. ii. Write a class method
called multiply, which takes a single number parameter a and returns the product of a and
MULTIPLIER. iii. Write a static method called subtract, which takes two number parameters, b
and c, and returns b - c. iv. Write a method called value which returns a tuple containing the
values of x and y. Make this method into a property, and write a setter and a deleter for
manipulating the values of x and y.
class Numbers:
def __init__(self, x=0, y=0): # Corrected the constructor method name
self.x = x
self.y = y
def add(self):
return self.x + self.y # No need for parameters; use the instance variables
# Example usage
n3 = Numbers()
result_multiply = n3.multiply(4.78)
print("Multiplication Result:", result_multiply)
Page | 19
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Practical no 9
A. Try to configure the widget with various options like: bg=”red”, family=”times”, size=18
import tkinter
root = Tk()
O.pack()
n.pack()
root.mainloop()
Output:
B. Try to change the widget type and configuration options to experiment with other widget
types like Message, Button, Entry, Checkbutton, Radiobutton, Scale etc.
root = Tk()
var = StringVar()
label.pack()
root.mainloop()
Page | 20
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Output:
Button.py
import tkinter
top = tkinter.Tk()
def helloCallBack():
B.pack()
top.mainloop()
Output:
Entry.py
import tkinter as tk
top = tk.Tk()
L1.pack(side=tk.LEFT)
E1 = tk.Entry(top, bd=5)
E1.pack(side=tk.RIGHT)
top.mainloop()
Output:
RadioButton.py
import tkinter as tk
def sel():
label.config(text=selection)
root = tk.Tk()
var = tk.IntVar()
R1.pack(anchor=tk.W)
R2.pack(anchor=tk.W)
R3.pack(anchor=tk.W)
label = tk.Label(root)
label.pack()
root.mainloop()
Outpt:
Page | 22
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Page | 23
Name:-Samruddhi Mali Roll no:-3131 Python Programming
Extra Practical
1) Write a program to display result based on below condition:
If percentage 75-Distinction ,Percentage between35-50-Pass Class, Percentage
between50-60 -Second Class, Percentage between60-75 -First Class, Percentage >75-
Distinction
print("Enter the marks of five subjects: ")
total = C_programing+cpp+dbms+Java+python
average = total / 5.0
percentage = (total / 500.0) * 100
2) simple calculator
Page | 24
Name:-Samruddhi Mali Roll no:-3131 Python Programming
CODE:
print("Enter any two number:")
num1 = float(input())
num2 = float(input())
print("value of",num1,"+",num2,"is",num1+num2)
print("value of",num1,"-",num2,"is",num1-num2)
print("value of",num1,"*",num2,"is",num1*num2)
print("value of",num1,"/",num2,"is",num1/num2)
Output:
Page | 25
Name:- Samruddhi Mali Roll no:-3131 Python Programming
1) if else statement
age = int(input("Enter your age:"))
if(age>18):print("Eligble for job") else:print("not
eligble for job")
Output:
OUTPUT:
Page | 26
Name:-Samruddhi Mali Roll no:-3131 Python Programming
5)ticketboookingdiscountnestifelseaskrailwayemp30%else(ifelse{ask
for age (<18 25%|| senior citizen 65yrs 25% )} rest 5% dis)
CODE:
railway_emp = str(input("Are you railway employee or not [yes/no]:"))
if(railway_emp == "yes" ):
print("youget30%discount " :
●
-")
elif(railway_emp == "no"):
age = int(input("Enter ur age:"))
if(17<age & age<65):print("you get 5% dicount")
elif(0<age<18):print("you get 25% dicount")
elif(age<65):print("you get 25% dicount")
else:print("wrong input")
else:print("wrong input")
OUTPUT:
5) Function
CODE:
def Add(a,b):
sum= a+ b print(f"{a}+{b}={a+b}")
Add(10,20)
Output:
Page | 27
Name:-Samruddhi Mali Roll no:-3131 Python Programming
def show(self):
print('Name:', self.name, '\nSex:', self.sex, '\nProfession:',
self.profession)
def work(self):
print(self.name, 'working as a', self.profession)
Pankaj= Person('Pankaj','Male','Full Stack Developer') Pankaj.show()
Pankaj.work()
OUTPUT:
Page | 28
Name:-Samruddhi Mali Roll no:-3131 Python Programming
11)
class Student:
def _init_(self,name,sex,course,result): self.name=name
self.sex=sex
self.course=course
self.result=result
def display(self,name,sex,course,result): self.name=name
self.sex=sex self.course=course
self.result=result print('name:' ,
name)
print('Sex:', sex) print('Course:', course)
print('Result:',result)
s1 =Student() s1.display('Pankaj','male','Bsc. IT','99.99%')
Output:
Page | 29
Python Programming
12) calculator
op = input("Enter operator:")
a = float(input("Enter 1st number:"))
b = float(input("Enter 2nd number:")) if(op == "+"):
print(a,'+',b,'=',a+b)
elif(op == "-"): print(a,'-',b,'=',a-b)
elif(op == "*"): print(a,'*',b,'=',a*b)
elif(op == "/"): print(a,'/',b,'=',a/b) else:print("Opps! Wrong Input!")
Output:
Output:
Page | 30
Name:-Samruddhi Mali Roll no:-3131 Python Programming
],
[
],
[
"Who is best course of the year?",
"BSCIT", "Bms", "Baf","Bmm","A"
PR_Piont = [ 40000,30000,20000,10000]
PR_Piont.sort()
Page | 31
Name:-Samruddhi Mali Roll no:-3131 Python Programming
if(reply== question[i][-1]):
OUTPUT:
CODE:
(len(),any(),all(),sorted())
dic1 = { 1:"A", 3:"B",2:"C" }
print(len(dic1)) print(any(dic1))
print(all(dic1))
print(sorted(dic1))
Output:
bg=’red’,size=18,family=’item’
CODE:
create_widget():
root = tk.Tk()
Output:
12) Try to change the widget type and configuration options to experiment
with other widget types like Message, Button, Entry, Checkbutton,
Page | 33
Name:-Samruddhi Mali Roll no:-3131 Python Programming
CODE:
import tkinter as tk
def create_widgets():
root = tk.Tk()
# Message Widget
message = tk.Message(root, text="This is a Message
Widget", width=200)
message.pack()
# Button Widget
button = tk.Button(root, text="Click Me",
command=lambda: print("Button clicked"))
button.pack()
# Entry Widget
entry = tk.Entry(root)
entry.pack()
# Checkbutton Widget
checkbutton = tk.Checkbutton(root, text="Check me")
checkbutton.pack()
# Radiobutton Widget
radio_var = tk.StringVar(value="Option 1")
radio1 = tk.Radiobutton(root, text="Option 1",
variable=radio_var, value="Option 1")
radio2 = tk.Radiobutton(root, text="Option 2",
variable=radio_var, value="Option 2")
radio1.pack()
radio2.pack()
# Scale Widget
scale = tk.Scale(root, from_=0, to=100,
orient=tk.HORIZONTAL)
scale.pack()
Page | 34
Name:-Samruddhi Mali Roll no:-3131 Python Programming
root.mainloop()
create_widgets()
Output:
Page | 35