Vinx Python
Vinx Python
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.
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.
list = ['a','b','c','d','e','f','g','h','i','j']
print(list[:3])
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:
#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.
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: