CS
CS
# write a program to create a tuple of integers using fucntion and then interchange
the first half and second half of it using a function
def input_tuple():
l = []
n = int(input("Enter the number of elements: "))
for i in range(n):
l.append(int(input("Enter the element: ")))
return tuple(l)
def interchange(t):
l = list(t)
n = len(l)//2
l[:n], l[n:] = l[n:], l[:n]
return tuple(l)
t = input_tuple()
t = interchange(t)
print(t)
# also write the output of above program in comments
# Enter the number of elements: 6
# Enter the element: 1
# Enter the element: 2
# Enter the element: 3
# Enter the element: 4
# Enter the element: 5
# Enter the element: 6
# (4, 5, 6, 1, 2, 3)
#write a program to create a list of 10 random no.s between M & N where M and N are
to entered by the users, create two more lists, one containing all odd elements of
it and another containing all even elements. Use a user defined function for the
purpose. Print all 3 lists
import random
def create_list():
l = []
n = int(input("Enter the number of elements: "))
m = int(input("Enter the lower limit: "))
N = int(input("Enter the upper limit: "))
for i in range(n):
l.append(random.randint(m,N))
return l
def odd_even(l):
odd = []
even = []
for i in l:
if i%2==0:
even.append(i)
else:
odd.append(i)
return odd, even
l = create_list()
odd, even = odd_even(l)
print(l)
print(odd)
print(even)
# also write the output of above program in comments
# Enter the number of elements: 10
# Enter the lower limit: 1
# Enter the upper limit: 100
# [1, 78, 61, 41, 2, 3, 90, 88, 5, 33]
# [1, 61, 41, 3, 5, 33]
# [78, 2, 90, 88]
#write a program to input a sentence and find out the longest and smallest words
from it using user defined functions
def longest_shortest(s):
l = s.split()
longest = l[0]
shortest = l[0]
for i in l:
if len(i)>len(longest):
longest = i
if len(i)<len(shortest):
shortest = i
return longest, shortest
s = input("Enter the sentence: ")
longest, shortest = longest_shortest(s)
print(longest)
print(shortest)
# also write the output of above program in comments
# Enter the sentence: Hello, how are you?
# Hello,
# are
#write a program to check whether a given number is an armstrong number, write the
main segment to input some integers and find out who amogst them are armstrong
numbers
def armstrong(n):
s = str(n)
l = len(s)
sum = 0
for i in s:
sum += int(i)**l
if sum == n:
return True
return False
l = []
n = int(input("Enter the number of elements: "))
for i in range(n):
l.append(int(input("Enter the element: ")))
for i in l:
if armstrong(i):
print(i)
# also write the output of above program in comments
# Enter the number of elements: 5
# Enter the element: 154
# Enter the element: 370
# Enter the element: 360
# Enter the element: 407
# Enter the element: 371
# 370
# 407
# 371
# write a program to receive a list of floats and interchange the largest and
smallest among them, also write the main segegment to execute the function
def interchange(l):
max = l[0]
min = l[0]
for i in l:
if i>max:
max = i
if i<min:
min = i
max_index = l.index(max)
min_index = l.index(min)
l[max_index], l[min_index] = l[min_index], l[max_index]
return l
l = []
n = int(input("Enter the number of elements: "))
for i in range(n):
l.append(float(input("Enter the element: ")))
l = interchange(l)
print(l)
# also write the output of above program in comments
# Enter the number of elements: 4
# Enter the element: 1.2
# Enter the element: 3.4
# Enter the element: 5.6
# Enter the element: 7.8
# [7.8, 3.4, 5.6, 1.2]
# write a program to receive a string and convert all lower case consonants into
uppercase
def convert(s):
vowels = "aeiou"
for i in s:
if i.islower() and i not in vowels:
s = s.replace(i, i.upper())
return s
s = input("Enter the string: ")
s = convert(s)
print(s)
# write a program to input some lines of text and write them onto a text file
myfile.txt and then read the content and find out the number of lowercase
consonants and special symbols.
def count(s):
consonants = "bcdfghjklmnpqrstvwxyz"
special = "!@#$%^&*()_+{}|:<>?`~"
count1 = 0
count2 = 0
for i in s:
if i.islower() and i in consonants:
count1 += 1
if i in special:
count2 += 1
return count1, count2
with open('myfile.txt', 'w') as file:
n = int(input("Enter the number of lines: "))
for i in range(n):
file.write(input("Enter the line: ") + '\n')
file.close()
with open('myfile.txt', 'r') as file:
s = file.read()
file.close()
count1, count2 = count(s)
print(count1)
print(count2)
# write a function to read a text file a.txt print which distinct digits present in
it in a list and also print average if all digits present in the text file
def distinct_digits(s):
digits = "0123456789"
l = []
sum = 0
count = 0
for i in s:
if i in digits and i not in l:
l.append(i)
if i in digits:
sum += int(i)
count += 1
return l, sum/count
with open('a.txt', 'r') as file:
s = file.read()
file.close()
l, avg = distinct_digits(s)
print(l)
print(avg)
# write a fucntion that accepts 2 filenames and copies all lines that start with
uppercase from 1st file to the 2nd file
def copy_lines(file1, file2):
with open(file1, 'r') as f1:
with open(file2, 'w') as f2:
for line in f1:
if line[0].isupper():
f2.write(line)
f2.close()
f1.close()
copy_lines('a.txt', 'b.txt')
# write a function to read a text file story.txt and print al words starting with
an uppercase vowel or a special symbol
def read_file(file):
vowels = "AEIOU"
special = "!@#$%^&*()_+{}|:<>?`~"
with open(file, 'r') as f:
s = f.read()
f.close()
l = s.split()
for i in l:
if i[0] in vowels or i[0] in special:
print(i)
read_file('story.txt')
# write a program with 2 fucntions use pickle where the first one will create a
binary file emp.dat to store empid empname salary of some employees, the 2nd
fucntion will read the file and disoplay the employees whose salary is 50000
import pickle
def create_file():
with open('emp.dat', 'wb') as file:
n = int(input("Enter the number of employees: "))
for i in range(n):
empid = int(input("Enter the employee id: "))
empname = input("Enter the employee name: ")
salary = int(input("Enter the salary: "))
l = [empid, empname, salary]
pickle.dump(l, file)
file.close()
def read_file():
with open('emp.dat', 'rb') as file:
while True:
try:
l = pickle.load(file)
if l[2] == 50000:
print(l)
except EOFError:
break
file.close()
create_file()
read_file()
#Write a program to read the above binary file of employees abd modify salary of
80000 to increase it by 10%
def modify_salary():
with open('emp.dat', 'rb') as file:
l = []
while True:
try:
l.append(pickle.load(file))
except EOFError:
break
file.close()
with open('emp.dat', 'wb') as file:
for i in l:
if i[2] == 80000:
i[2] = i[2]*1.1
pickle.dump(i, file)
file.close()
modify_salary()
#write customer name city and amount of some customers to a csv file and then print
a report of all customers whose city is cuttack after reading the data from file
import csv
def create_csv():
with open('customers.csv', 'w', newline='') as file:
writer = csv.writer(file)
n = int(input("Enter the number of customers: "))
for i in range(n):
name = input("Enter the name of the customer: ")
city = input("Enter the city of the customer: ")
amount = int(input("Enter the amount: "))
writer.writerow([name, city, amount])
file.close()
def read_csv():
with open('customers.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
if row[1] == 'cuttack':
print(row)
file.close()
create_csv()
read_csv()
# write a function PUSH(Arr) where Arr is a list of integers, from this list push
all those integers who divisible by 2 but not by 5 into a stack implemented by a
list, write another function POP() to pop an element from the stack using this
function display the stack contents, if it has atleast 1 element otherwise display
an appropriate message
def PUSH(Arr):
stack = []
for i in Arr:
if i%2==0 and i%5!=0:
stack.append(i)
return stack
def POP(stack):
if len(stack) == 0:
print("Stack is empty")
else:
stack.pop()
return stack
Arr = [2, 5, 10, 15, 20, 25]
stack = PUSH(Arr)
print(stack)
stack = POP(stack)
print(stack)
# a csv file exists named employee.csv containing emp no. name and salary, write a
function to display details of those employees whose salary is between A and B, the
function should receive A and B as arguments
def display(A, B):
with open('employee.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
if A<=int(row[2])<=B:
print(row)
file.close()
display(10000, 20000)
# a csv file is existing that contains roll no, name, aggregate, Write a fucntion
that will copy details of those studetns with aggregate more than 75 to another csv
file
def copy():
with open('students.csv', 'r') as file:
with open('students1.csv', 'w', newline='') as file1:
reader = csv.reader(file)
writer = csv.writer(file1)
for row in reader:
if int(row[2])>75:
writer.writerow(row)
file1.close()
file.close()
copy()