0% found this document useful (0 votes)
5 views

Vinx Python

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Vinx Python

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

BACHELOR OF COMPUTER APPLICATIONS

IOT Lab File


Course Code: BCA Practical 372
Batch – 2021-24 (E)

Submitted by: - Submitted to: -


Name: Vineet Singh Dr. Sonam Kaushik
Enrollment No: 01621202021 Assistant Professor, MSI
Sem: 6th Sec: A (2nd shift)
Index

S.No. Program statement Date


1. Write a program to enter two integers, two floating numbers and then 19-09-2022
perform all arithmetic operations on them.
2. Write a program to check whether a number is an Armstrong number 19-09-2022
or not.
3. Write a program to print the sum of all the primes between two 26-09-2022
ranges.
4. Write a program to swap two strings. 26-09-2022
5. Write a menu driven program to accept two strings from the user and 03-10-2022
perform the various functions using user defined functions.
6. Write a program to find smallest and largest number in a list 10-10-2022
7. Create a dictionary whose keys are month names and whose values are 17-10-2022
the number of days in the corresponding months.
 Ask the user to enter a month name and use the dictionary to
tell them how many days are in the month.
 Print out all keys in the alphabetically order
 Print out all the months with 31 days
 Print out the key value pairs sorted by number of days in each
month
8. Make a list of first 10 letters of the alphabet, then use the slicing to do 17-10-2022
the following operations:
 Print the first 3 letters of the list
 Print any 3 letters from the middle
Print the letter from any particular index to the end of the list
9. Write a program that scans an email address and forms a tuple of user 07-11-2022
name and domain.
10. Write a program that uses a user defined function that accepts name 07-11-2022
and gender (as M for Male, F for Female) and prefixes Mr. /Ms. on the
basis of the gender.
1. Write a program to enter two integers, two floating numbers and
then perform all arithmetic operations on them.

num1 = int(input("Enter first Int number: ")) # user int input


num2 = int(input("Enter second Int number: ")) # user int input
flo1=float(input("Enter first Float number: ")) # user float input
flo2= float(input("Enter second Float number: ")) # user float input
print("Int Addition: ",num1 + num2) # Addition
print("Float Addition: ",flo1 + flo2) # Addition
print("Int Subtraction: ",num1 - num2) # Subtraction
print("Float Subtraction: ",flo1 - flo2) # Subtraction
print("Int Multiplication: ",num1 * num2) # Multiplication
print("Float Multiplication: ",flo1 * flo2) # Multiplication
print("Int Division: ",num1 / num2) # simple Division
print("Float Division: ",flo1 / flo2) # simple Division
print("Int Floor Division: ",num1 // num2) # floor/ integer Division
print("Float Floor Division: ",flo1 // flo2) # floor/ integer Division
print("Int Modulus: ", num1 % num2) # Modulus
print("Float Modulus: ", flo1 % flo2) # Modulus
print("Int Exponentiation: ",num1 ** num2) # Exponentiation
print("Float Exponentiation: ",flo1 ** flo2) # Exponentiation
2. Write a program to check whether a number is an Armstrong
number or not.

n = int(input("Enter a number : "))


s=n
b = len(str(n))
sum1 = 0
while n != 0:
r = n % 10
sum1 = sum1+(r**b)
n = n//10
if s == sum1:
print("The given number", s, "is armstrong number")
else:
print("The given number", s, "is not armstrong number")
3. Write a program to print the sum of all the primes between two
ranges.

lower_value = int(input ("Please, Enter the Lowest Range Value: "))


upper_value = int(input ("Please, Enter the Upper Range Value: "))
sum=0
print ("The Sum of Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print(number,end=', ')
sum=sum+number
print("\nTotal : ",sum)
4. Write a program to swap two strings.

str1 = "Hello";
str2 = "Brother";
print("Strings before swapping: " + str1 + " " + str2);
str1 = str1 + str2;
str2 = str1[0 : (len(str1) - len(str2))];
str1 = str1[len(str2):];
print("Strings after swapping: " + str1 + " " + str2)
5. Write a menu driven program to accept two strings from the user
and perform the various functions using user defined functions.

str=input("Enter the String : \n")


def len1(str):
print(len(str))
def slice2(str):
r1=input("Enter the starting range : ")
r2=input("Enter the ending range : ")
print(str[r1:r2])
def upper3(str):
print(str.upper())
def lower4(str):
print(str.lower())
def remove5(str):
print(str.strip())
def replace6(str):
r1=input("Enter the place to be replaced : ")
r2=input("Enter the value to be replace with : ")
print(a.replace(r1, r2))
def combine7(str):
a = input("Enter the String : ")
b = input("Enter the String : ")
c=a+b
print(c)
print("\nMENU")
print("1. Need length of the given string")
print("2. Wanna new string from the given one")
print("3. Convert it to upper case")
print("4. Convert it to lower case")
print("5. Remove whiteSpace")
print("6. Replace with new alpha")
print("7. combine to different string")
choice=int(input("Enter your Choice : "))
if choice == 1:
len1(str);
elif choice == 2:
slice2(str);
elif choice == 3:
upper3(str);
elif choice == 4:
lower4(str);
elif choice == 5:
remove5(str);
elif choice == 6:
replace6(str);
elif choice == 7:
combine7(str);
else:
print("Incorrect Choice. Please, try again.")
6. Write a program to find smallest and largest number in a list

lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :",
min(lst))
7. Create a dictionary whose keys are month names and whose values
are the number of days in the corresponding months.

 Ask the user to enter a month name and use the dictionary
to tell them how many days are in the month.

month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,


"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

mon = input("Enter the month name in short form :- ")


print("Number of days in ",mon,"=",month [ mon ])

 Print out all keys in the alphabetically order

month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,


"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

lst = list ( month . keys() )


lst.sort()
print( lst )
 Print out all the months with 31 days

month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,


"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

print( "Month which have 31 days !!-- ")


for i in month :
if month [ i ] == 31 :
print( i )

 Print out the key value pairs sorted by number of days in


each month
month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,
"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

print("Month according to number of days ---")


print("feb")
for i in month :
if month [ i ] == 30 :
print(i)
for i in month :
if month [ i ] == 31 :
print( i)
8. Make a list of first 10 letters of the alphabet, then use the slicing to
do the following operations:
 Print the first 3 letters of the list

list = ['a','b','c','d','e','f','g','h','i','j']
print(list[:3])

 Print any 3 letters from the middle

list = ['a','b','c','d','e','f','g','h','i','j']
print(list[3:8])

 Print the letter from any particular index to the end of the
list

list = ['a','b','c','d','e','f','g','h','i','j']
print(list[6:])
9. Write a program that scans an email address and forms a tuple of
user name and domain.

test_str = '[email protected]'
print("The original string is : " + str(test_str))
res = test_str[test_str.index('@') + 1 : ]
print("The extracted domain name : " + str(res))
thistuple = (test_str,res)
print(type(thistuple))
print(thistuple)
10. Write a program that uses a user defined function that accepts
name and gender (as M for Male, F for Female) and prefixes
Mr./Ms. on the basis of the gender.

def pre(str,gender):
if(gender=='M'):
str="Mr. "+str;
return str
elif(gender=="F"):
str="Ms. "+str;
return str
else:
return "Invalid input"
str=input("Enter the name : ")
gender=input("Gender\nfor male : M\t for female : F\n")
print(pre(str,gender))
11. WAP to display Bar graphs or Pie chart using Matplotlib.
import matplotlib.pyplot as pyplot
labels = ('Python', 'Java', 'JavaScript', 'C#', 'PHP', 'C,C++', 'R')
index = (1, 2, 3, 4, 5, 6, 7) # provides locations on x axis
sizes = [29.9, 19.1, 8.2, 7.3, 6.2, 5.9, 3.7]
pyplot.bar(index, sizes, color="#6c3376", tick_label=labels)
pyplot.ylabel('Usage in %')
pyplot.xlabel('Programming Languages')
pyplot.show()

Output:

import matplotlib.pyplot as plt


labels = 'Label_1', 'Label_2', 'Label_3'
sizes = [10, 34, 56]
explode = (0, 0.1, 0)
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal')
plt.show()
Output:
12.WAP that defines a function large in a module which will be used to
find larger of two values and called from code in another module.

#largest.py def
large(a, b):
if a >= b:
return a
else:
return b
a=2
b=4
print(large(a, b))

#myfile.py
import largest as l
print("The largest number:",l.large(81,45))

Output:
13.WAP to know the cursor position and print the text according to
specifications given below.

• Print initial position


• Move cursor to 4th position
• Display next 5 characters
• Move cursor to next 10 characters
• Print current position
• Print next 10 characters from current cursor position

with open("C:\\Users\\aksh\\Documents\\table.txt", "r") as doc:


pos1 = doc.tell()
print("Initial Position: ", pos1)
s1 = doc.seek(4)
a = doc.read(5)
print("Next 5 Charcters: ", a)
doc.seek(s1+10)
pos2 = doc.tell()
print("Current Position: ", pos2)
a1 = doc.read(10)
print("Next 10 Charcters: ", a1)

Output:
14. Create a binary file with roll number, name and marks. Input a roll
number and perform the following operations:

• Update marks
• Display records
• Search record
• Delete record
import pickle
student = []
ans = 'y'
with open("E:\temp.bin", "wb") as wd:
while ans.lower()=='y':
roll_no = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
marks = int(input("Enter Marks: "))
student.append([roll_no, name, marks])
ans = input("Do you want to continue? (y/n): ")
pickle.dump(student, wd)
with open("E:\temp.bin", "rb") as rd:
data= pickle.load(rd)
new_marks = int(input("Enter New Marks: "))
for record in data:
record[2] = new_marks print("Record Updated")
r = int(input("Enter Roll No. to Delete: "))
for record in data:
if record[0]==r:
data.remove(record)
print("Record Deleted")
break
else:
print("Record Not Found")
print("Roll No. Name Marks")
for record in data:
print(record[0], record[1], record[2]) #e
r = int(input("Enter Roll No. to Search: "))
for record in data:
if record[0]==r:
print("Roll No. Name Marks")
print(record[0], record[1], record[2])
break
else:
print("Record Not Found")
Output:
15. Create a binary file with roll number, name and marks. Input a roll
number and perform the following operations:

import csv
with open("user_info.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("user_info.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
if i[0] == given:
print(i[1])
break

Output:

You might also like