python multiple basic codes
python multiple basic codes
CERTIFICATE
Place: Satara
Date:
Miss.R.N.Gaikwad Dr.R.D.Kumbhar
Teacher in charge Head of Department
Examiner Dr.B.S.Sawant
(Director)
INDEX
Sr. Page.
Particular Date Sign
No No
1 Program to display name and address. 16/1/25 1
Program to Accept two number and display
2 addition, subtraction, multiplication,division 17/1/25 2
and modules.
#Method 2:
def display():
name = input("Plz Enter Your Name Sir/Madam: ")
address = input("Plz Enter Your Address Sir/Madam: ")
age = int(input("Plz Enter Your Age Sir/Madam: "))
print("Name: ",name,"\n","Address: ",address,"\n","Age: ",age)
display()
Output:
Assignment 2
Name: saurabh manohar sawant
Class: BCA III (SEM-VI) Roll.No: 189
Q.2: Program to accept two numbers and display addition, subtraction, multiplication,
division and modules.
#Method 1:
def add():
a = 10
b = 20
c=a+b
return c
sum = add()
print("Addition= ",sum)"""
#Method 2:
def add():
a = 10
b = 20
return a + b
sum = add()
print("Addition= ",sum)"""
#Method 3:
def add(a,b):
return a + b
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
sum = add(num1,num2)
print("Addition= ",sum)"""
#Method 4:
def calculations(a,b):
return a + b,a - b,a * b,a / b,a % b,a // b
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
add,sub,mul,div,remain,floor = calculations(num1,num2)
print("\n","Addition= ",add,"\n","Subtraction= ",sub,"\n","Multiplication=
",mul,"\n","Division= ",div,"\n","Modulus= ",remain,"\n","Floor= ",floor)
Output:
Assignment 3
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.3: Program to calculate factorial of given number.
#Using loop
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print("Factorial of number is not possible")
elif num == 0:
print("Factorial of 0 is 1")
else:
for i in range(1,num + 1):
fact = fact * i
print("Factorial of",num,"is",fact)
#Using function
def fact(n):
if n == 1:
return 1
else:
return n*fact(n-1)
Output:
Assignment 4
Name: saurabh manohar sawant
Class: BCA III (SEM-VI) Roll.No: 189
Q.4: Program to create a list of 100 numbers and separate those numbers in two
different list one includes odd number other even.
Output:
Assignment 5
Name: saurabh manohar sawant
Class: BCA III (SEM-VI) Roll.No:189
Q.5: Program to display maximum number and minimum number from given list.
#Maximum Number
#using loop
number = [99, 67, 44, 54, 27, 9]
max_num=number[0]
for num in number:
if num>max_num:
max_num=num
print("Maximun num: ",max_num)
#using function
def max_num(lst):
maximum=lst[0]
for num in lst:
if num>maximum:
maximum=num
return maximum
number=[99, 67, 44, 54, 27, 9]
res=max_num(number)
print(f"Maximum number: {res}")
#using reduce
from functools import reduce
number=[99, 67, 44, 54, 27, 9]
max_num=reduce(lambda a,b:a if a>b else b, number)
print(max_num)
Output:
#Minimum Number
#using loop
number = [2, 99, 67, 44, 54, 27, 9]
min_num=number[0]
for num in number:
if num<min_num:
min_num=num
print("Minimum num: ",min_num)
#using function
def min_num(lst):
minimum=lst[0]
for num in lst:
if num<minimum:
minimum=num
return minimum
number=[2, 99, 67, 44, 54, 27, 9]
res=min_num(number)
print(f"Minimum number: {res}")
#using reduce
from functools import reduce
number=[2, 99, 67, 44, 54, 27, 9]
min_num=reduce(lambda a,b:a if a<b else b, number)
print(min_num)
numbers=[5,8,1,30,59,62,44,81,74,66,89]
print(numbers[::])
print(numbers[:7])
print(numbers[2:])
print(numbers[0:10:2])
print(numbers[::-1])
print(numbers[-8:-1])
print(numbers[9:1:-1])
print(numbers[-1:-8:-1])
Ouput:
Assignment 7
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.7: Program to demonstrate set operators (union , intersection, minus)
setA = {1,2,3,4}
setB = {3,4,5,6}
print(setA | setB)
print(setA.union(setB))
print(setA & setB)
print(setA.intersection(setB))
print(setA - setB)
print(setA.difference(setB))
Ouput:
Assignment 8
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.8: Program to print current date and time.
from datetime import datetime
current=datetime.now()
current_date=current.date()
current_time=current.time()
print(current)
print("Current Date: ",current_date)
print("Current Time: ",current_time)
Output:
Assignment 9
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.9: Program to Today’s Year, Month, and Date.
current=datetime.now()
current_year=current.year
current_month=current.month
current_day=current.day
print(current)
print("Current year: ",current_year)
print("Current month: ",current_month)
print("Current day: ",current_day)
Output:
Assignment 10
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.10: Program to convert Date to String.
import datetime
current_datetime=datetime.datetime.now()
print("current date and time:",current_datetime)
str_date = current_datetime.strftime("%d-%m-%Y")
Output:
Assignment 11
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.11: Program to display the Calendar of a given month.
import calendar
# Enter the month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
Output:
Assignment 12
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.12: Program to display calendar of the given year.
import calendar
print("\n" + calendar.calendar(year))
Output:
Assignment 13
Name: saurabh manohar sawant
Class: BCA III (SEM-VI) Roll.No: 189
import fileinput
for line in fileinput.input(files ='rohan.txt'):
print(line)
Output:
Assignment 14
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.14: Program to demonstrate File output.
try:
fileName = "rohan.txt";
fileObj = open(fileName,'r');
readText = fileObj.read();
except FileNotFoundError:
print("Failed to open file: ",fileName, "No such file");
else:
print("File is opened successfully",fileObj);
Output:
Assignment 15
Name: saurabh manohar
sawant Class: BCA III Roll.No: 189
(SEM-VI)
Q.15: Program two add two numbers using GUI.
import tkinter
def addNums():
try:
first = int(firstWin.get())
second = int(secoWin.get())
answer = first + second
txtBx.delete("1.0", "end") # Clear previous result
txtBx.insert("1.0", str(answer)) # Insert the new result
except ValueError:
txtBx.delete("1.0", "end")
txtBx.insert("1.0", "Error") # Show an error message for invalid input
bob = tkinter.Tk()
bob.title('Adding Box')
# Organize widgets
firstLbl.pack()
firstWin.pack()
secoLbl.pack()
secoWin.pack()
addButton.pack()
txtBx.pack()
bob.mainloop()
Output: