Python File
Python File
1 Write a program to enter two integers, two floating numbers and then CO1
perform all arithmetic operations on them.
2 Write a program to check whether a number is an Armstrong number or not. CO1
Write a python program to that accepts length of three sides of a triangle as
inputs. The program should indicate whether or not the triangle is a right
angled triangle (use Pythagorean theorem).
3 Write a python program to find factorial of a number using recursion. CO2
4 Write a program to print the sum of all the primes between two ranges. CO1
5 Write a python program to construct the following pattern using nested for CO2
loop:
*
**
***
****
*****
6 Write a program to swap two strings. CO1
7 Write a Program to Count the Occurrences of Each Word in a Given String CO2,CO3
Sentence.
8 Write a program to create, concatenate and print a string and accessing CO2
substring from a given string.
9 Write a menu driven program to accept two strings from the user and CO1,CO2
perform the various functions using user defined functions.
10 Write a program to find smallest and largest number in a list. CO2,CO3
11 Write a Program to Remove the ith Occurrence of the Given Word in a List. C02,CO3
12 Create a dictionary whose keys are month names and whose values are the CO2,CO3
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
13 Make a list of first 10 letters of the alphabet, then use the slicing to do the CO2,CO3
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
14 Write a program that scans an email address and forms a tuple of user name CO2,CO3
and domain.
15 Write a program that uses a user defined function that accepts name and CO2,CO3
gender (as M for Male, F for Female) and prefixes Mr./Ms. on the basis of
the gender.
16 Write a Program to Sort a List of Tuples in Increasing Order by the Last CO2,CO3
Element in Each Tuple.
17 Write a Program to Swap the First and Last Element in a List. CO2,CO3
18 Write a program to display Bar graphs or Pie chart using Matplotlib. CO2,CO3
19 Write a program that defines a function large in a module which will be used CO2,CO3
to find larger of two values and called from code in another module.
20 Write a python program to define a module to find Fibonacci Numbers and CO2,CO3
import the module to another program.
21 Write a program to know the cursor position and print the text according to CO2,CO3
specifications given below:
● Print the initial position
● Move the cursor to 4th position
● Display next 5 characters
● Move the cursor to the next 10 characters
● Print the current cursor position
● Print next 10 characters from the current cursor position
22 Create a binary file with roll number, name and marks. Input a roll number CO4
and perform the following operations:
update the marks.
Delete the record
Display the record
Append the record
Search the record
23 Write a program to Create a CSV file by entering user-id and password, read CO5
and search the password for given user id.
24 Write a Program to Count the Number of Words in a Text File. CO5
25 Write a Program to Count the Number of Lines in Text File. CO5
26 Write a program that inputs a text file. The program should print all of the CO4
unique words in the file in alphabetical order.
27 Write a Python class to implement pow(x, n). CO4
28 Write a program to create a numpy array. CO5
29 Write a program to Extract first n columns of a Numpy matrix. CO5
32 Write a program of Numpy convert 1-D array with 8 elements into a 2-D CO4,CO5
array in Python
Program -1 Write a program to enter two integers, two floating numbers and then perform all
arithmetic operations on them.
output
Program -2 Write a program to check whether a number is an Armstrong number or not
sum = 0
temp = num
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
else:
output
Program-3 Write a python program to that accepts length of three sides of a triangle as inputs.
The program should indicate whether or not the triangle is a right angled triangle (use
Pythagorean theorem).
print("Triangle Possible")
else :
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
if num < 0:
elif num == 0:
output
program-5 Write a program to print the sum of all the primes between two ranges
code - lower_value = int(input ("Please, Enter the Lowest Range Value: "))
if number > 1:
if (number % i) == 0:
break
else:
print (number)
output-
program 6- Write a python program to construct the following pattern using nested for loop:
**
***
****
*****
print(str(i) * i)
output
string1 = input()
string2 = input()
temp = string1
string1 = string2
string2 = temp
print("\nString after swap:")
output--
program-7 Write a Program to Count the Occurrences of Each Word in a Given String Sentence
word = "am"
words = string.split()
count = 0
for w in words:
if w == word:
count += 1
print(count)
output
program -8 Write a program to create, concatenate and print a string and accessing substring
from a given string.
strOne = input()
strTwo = input()
program-9 Write a menu driven program to accept two strings from the user and perform the
various functions using user defined functions
program 10- Write a program to find smallest and largest number in a list
code lst = []
for n in range(num):
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :",
min(lst))
output
Program 11 Write a Program to Remove the ith Occurrence of the Given Word in a List
Code a=[]
for x in range(0,n):
a.append(element)
print(a)
c=[]
count=0
for i in a:
if(i==b):
count=count+1
if(count!=n):
c.append(i)
else:
c.append(i)
if(count==0):
else:
output
program 12 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
Print out the key value pairs sorted by number of days in each month
(ii)
lst.sort()
print( lst )
(iii)
for i in month :
if month [ i ] == 31 :
print( i )
(iv)
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("feb")
for i in month :
if month [ i ] == 30 :
print(i)
for i in month :
if month [ i ] == 31 :
print( i)
output
program 13 Make a list of first 10 letters of the alphabet, then use the slicing to do the following
operations:
Print the letter from any particular index to the end of the list
test_list = []
test_list = list(string.ascii_uppercase)
output
program 14 – Write a program that scans an email address and forms a tuple of user name and
domain.
res = test_str[test_str.index('@') + 1 : ]
# printing result
output
program -15 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.
Code
def prefix(name,gender):
print("Hello, Mr.",name)
print("Hello, Ms.",name)
else:
gender = input("Enter your gender: M for Male, and F for Female: ")
prefix(name,gender)
output
program-16- Write a Program to Sort a List of Tuples in Increasing Order by the Last Element in
Each Tuple
temp = my_tup[j]
my_tup[j]= my_tup[j + 1]
return my_tup
print(my_tuple)
print(sort_tuple(my_tuple))
output
program 17- Write a Program to Swap the First and Last Element in a List.
size_of_list = len(my_list)
temp = my_list[0]
my_list[0] = my_list[size_of_list - 1]
my_list[size_of_list - 1] = temp
return my_list
my_list = [34, 21, 56, 78, 93, 20, 11, 9]
print(my_list)
print(list_swapping(my_list))
output
program 18- Write a program to display Bar graphs or Pie chart using Matplotlib ?
'Python':35}
courses = list(data.keys())
values = list(data.values())
width = 0.4)
plt.xlabel("Courses offered")
plt.show()
ouput -
program- 19 Write a program 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.
def add_numbers(x,y):
sum = x + y
return sum
num1 = 5
num2 = 6
output
program 20 Write a python program to define a module to find Fibonacci Numbers and import
the module to another program.
code
def Fibonacci(n):
if n < 0:
print("Incorrect input")
elif n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
program 21
program 22 Create a binary file with roll number, name and marks. Input a roll number and
perform the following operations:
marks = []
Student.name = name
Student.marks.append(m1)
Student.marks.append(m2)
Student.marks.append(m3)
def displayData(self):
def total(self):
def average(self):
s1 = Student()
s1.displayData()
output
program 23 Write a program to Create a CSV file by entering user-id and password, read and
search the password for given user id.
# writing to CSV
import csv
# field names
filename = "university_records.csv"
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
output
# number of words
number_of_words = 0
data = file.read()
lines = data.split()
# variable
number_of_words += len(lines)
print(number_of_words)
output
lines = len(fp.readlines())
program 26
# In read mode, open the given file with the name 'filename'
words = line.split()
for i in words:
# Traverse through all the characters of the word using another for loop.
for letter in i:
if(letter.isdigit()):
print(letter)
output
return x
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.pow(x,-n)
val = self.pow(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
print(py_solution().pow(2, -3));
print(py_solution().pow(3, 5));
print(py_solution().pow(100, 0));
output
print(arr)
output
[4, 5, 6, 7, 5, 3, 2, 5],
print(the_arr[:, 1:5])
output
array1 = np.array(
[[1, 2],
[3, 4],
[5, 6]])
total = np.sum(array1)
print(f'Sum of all the elements is {total}')
output
import numpy
# initializing list
lst = [1, 7, 0, 6, 2, 5, 6]
# displaying list
# displaying array
output
Program 32 Write a program of Numpy convert 1-D array with 8 elements into a 2-D array in
Python
# initializing list
lst = [1, 7, 0, 6, 2, 5, 6]
arr = numpy.array(lst)
# displaying list
# displaying array