Sol PYTHON PROG - III Sem Question Paper (2023-24) Odd
Sol PYTHON PROG - III Sem Question Paper (2023-24) Odd
Roll No.
B. TECH.
PYTHON PROGRAMMING
Note:
1. Attempt all Sections. If require any missing data; then choose suitably.
SECTION -A
Ans.
Ans.
>>25/4
>>6.25
>>25//4
>>6
c.Compute the output of the following python code:
def count(s):
forstr in string.split():
s = “&”.join(str)
return s
print(count(“Python is fun to learn.”))
Ans.
Syntax Error, There is no definition of string inside the count() function. But if
we consider that statement like “string”.split() then count() function insert &
after each character in “string” and then return.
Output will be
s&t&r&i&n&g
Let we have a function f1() in library.py and we wish to use it in main.py, then
we will use import statement as follows:
Ans.
import numpy as np
x = np.linspace(0, 30, 5)
e.g. [0,7.5,15,22.5,30]
Ans.
In line no 3 we are trying to change the string contents, which is not possible
in Python. In python string objects are immutable.
g.Describe about different functions of matplotlib and pandas.
Ans.
The Pandas library in Python provides high-level data structures and functions
designed to make working with structured or tabular data fast, easy
Pandas offers a rich set of functions for data manipulation, including merging
and joining datasets (merge(), concat()), reshaping data (pivot_table(), stack(),
unstack()), sorting data (sort_values(), sort_index()), and dealing with missing
data (dropna(), fillna()).
Section –B
2. Attempt any three of the following: 𝟑 × 𝟕 = 𝟐𝟏
Ans.
tuple_example = (1, 2, 3)
a, b, c = tuple_example
print(a)
# Output: 1
print(b)
# Output: 2
print(c)
# Output: 3
b.Illustrate different list slicing constructs for the following operations on
the following list:
L = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>L= [1, 2, 3, 4, 5, 6, 7, 8, 9]
>> print(L[1:][::-1])
>>print(L[2:-1])
>>print(L[1::2])
>>print(L[len(L)//2:])
>>print(L[:len(L)//2-1:-1])
c.Construct a function perfect_square(number) that returns a number if it is
a perfectsquare otherwise it returns -1.
For example:
perfect_square(1) returns 1
perfect_square (2) returns -1.
Ans.
import math
defperfect_square(n):
m=n**0.5
m=math.floor(m)
if m*m==n:
return 1
else:
return -1
print(perfect_square(25))
print(perfect_square(24))
d.Construct a program to change the contents of the file by reversing each
characterseparated by comma:
Hello!!
Output
H,e,l,l,o,!,!.
Ans.
f=open(“p1.txt”,”r”)
data=f.read()
f.close()
s=“,”.join(data)
f1=open(“p1.txt”,”w”)
f1.write(s)
f1.close()
e.Construct a plot for following dataset using matplotlib :
Food Calories Potassium fat
Meat 250 40 8
Banana 130 55 5
Avocados 140 20 3
Sweet 120 30 6
Potatoes
Spinach 20 40 1
Watermelon 20 32 1.5
Coconut 10 10 0
water
Beans 50 26 2
Legumes 40 25 1.5
Tomato 19 20 2.5
Ans.
food=["Meat","Banana","Avocados","Sweet
Potatoes","Spinach","Watermelon","Coconut
Water","Beans","Legumes","Tomato"]
calories=[250,130,140,120,20,20,10,50,40,19]
potassium=[40,55,20,30,40,32,10,26,25,20]
fat=[8,5,3,6,1,1.5,0,2,1.5,2.5]
plt.plot(food,calories)
plt.plot(food,potassium)
plt.plot(food,fat)
plt.show()
Section –C
Ans.
def removenth(s,n):
ans=""
if n<len(s):
fori in range(len(s)):
if(i!=n):
ans=ans+s[i]
else:
ans=s
returnans
print(removenth("Mango",1))
print(removenth("Mango",3))
print(removenth("Mango",7))
Ans.
def checkPassword(strpass):
islower=False
isdigit=False
isupper=False
isspecial=False
asc=ord(char)
isupper=True
islower=True
isdigit=True
if char in ['$','#','@']:
isspecial=True
return True
else:
return False
lst=passwords.split(",")
validlst=[]
if checkPassword(password):
validlst.append(password)
print(",".join(validlst))
Ans.
As we know that loops are used to execute the same statement or group of
statement multiple times.
If we knows the number of cycles or iteration then we use "for" loop, but
sometimes we have just some condition to stop the cycle, in such cases we use
"while" loop.
Both loops have some specific features associated with them.
e.g.
for i in range(10):
print(i)
#Output
1
2
3
4
5
6
7
8
9
lst=["ABC","XYZ","MNO"]
for item in list:
print(item)
#Output
ABC
XYZ
MNO
x=500
while x>100:
print(x)
x=x-100
#output
500
400
300
200
5. Attempt any one part of the following: 𝟕 × 𝟏 = 𝟕
a.Construct a function ret smaller(l) that returns smallest list from a nested
list. If two lists have same length then return the first list that is
encountered. For example:
ret smaller([ [ -2, -1, 0, 0.12, 1, 2], [3, 4, 5], [6 , 7, 8, 9, 10], [11, 12, 13,
14, 15]])
returns [3,4,5]
ret smaller([ [ -2, -1, 0, 0.12, 1, 2], [‘a’, ’b’, ’c’, ’d’, 3, 4, 5], [6 , 7, 8, 9,
10], [11,12, 13, 14, 15]]) returns [6 , 7, 8, 9, 10]
Ans.
def smaller(l):
smalllst=l[0]
for lst in l:
if len(smalllst)>len(lst):
smalllst=lst
return smalllst
3. Filter all the strings that contains any of the following noun: Agra,
Ramesh, Tomato, Patna.
Create a program that implements these filters to clean the text.
Ans.
def is_not_digit(char):
def starts_with_vowel(word):
vowels = 'aeiouAEIOU'
def contains_noun(word):
s = input()
words = filtered_string.split()
# Use filter() to remove all the words that start with a vowel
# Use filter() to remove all the words that contain any of the specified nouns
print(filtered_sentence)
a. Change all the numbers in the file to text. Construct a program for the
same.
Example:
Given 2 integer numbers, return their product only if the product is
equal to or lower than 10.
And the result should be:
Given two integer numbers, return their product only if the product is
equal to or lower than one zero
Ans.
f=open("data.txt","r")
s=f.read()
f.close()
lst=["zero","one","two","three","four","five","six","seven","eight","nine"]
for i in range(10):
s=s.replace(str(i),lst[i])
f=open("data.txt","w")
f.write(s)
f.close()
Ans.
def find_digit_words(file_path):
try:
content = file.read()
words = content.split()
return digit_words
except FileNotFoundError:
digit_words = find_digit_words(file_path)
print("Words composed of digits only:")
print(word)
Ans.
import csv
def read_csv(file_path):
data = []
last_column = []
csv_reader = csv.reader(file)
def plot_data(data):
plt.scatter(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
file_path = 'cities.csv'
plot_data(data)
b.Design a calculator with the following buttons and functionalities like
addition, subtraction, multiplication, division and clear.
Ans.