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

Practical ScA

The document contains multiple Python programming tasks and their corresponding source code. Tasks include calculating statistics for user-input sentences, manipulating lists and dictionaries, and working with tuples. Each task is presented with a clear prompt and a code snippet demonstrating the solution.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Practical ScA

The document contains multiple Python programming tasks and their corresponding source code. Tasks include calculating statistics for user-input sentences, manipulating lists and dictionaries, and working with tuples. Each task is presented with a clear prompt and a code snippet demonstrating the solution.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

12.

Write a program that should prompt the user to type some sentence(s)
followed by "enter". It should then print the original sentence(s) and the following
statistics relating to the sentence(s) :

 Number of words
 Number of characters (including white-space and punctuation)
 Percentage of characters that are alphanumeric

Hints

 Assume any consecutive sequence of non-blank characters is a word.

Source Code:
str = input("Enter a few sentences: ")
length = len(str)
spaceCount = 0
alnumCount = 0

for ch in str :
if ch.isspace() :
spaceCount += 1
elif ch.isalnum() :
alnumCount += 1

alnumPercent = alnumCount / length * 100

print("Original Sentences:")
print(str)

print("Number of words =", (spaceCount + 1))


print("Number of characters =", (length + 1))
print("Alphanumeric Percentage =", alnumPercent)
16. Ask the user to enter a list of strings. Create a new list that consists of those
strings with their first characters removed.
Source code:
l1 = eval(input("Enter a list of strings: "))
l2 = []

for i in range(len(l1)):
l2.append(l1[i][1:])

print("List after removing first characters:")


print(l2)
13.Write a program to input a line of text and print the biggest word (length
wise) from it.

Source Code:
str = input("Enter a string: ")
words = str.split()
longWord = ''

for w in words :
if len(w) > len(longWord) :
longWord = w

print("Longest Word =", longWord)


11. Write a program which replaces all vowels in the string with '*'.
Source Code:
str = input("Enter the string: ")
newStr = ""
for ch in str :
lch = ch.lower()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
newStr += '*'
else :
newStr += ch
print(newStr)
21. Can you store the details of 10 students in a dictionary at the same time ?
Details include - rollno, name, marks, grade etc. Give example to support your
answer.

Source Code:-
n = 10
details = {}

for i in range(n):
name = input("Enter the name of student: ")
roll_num = int(input("Enter the roll number of student: "))
marks = int(input("Enter the marks of student: "))
grade = input("Enter the grade of student: ")
details[roll_num] = [name, marks, grade]
print()
print(details)
22. Write a program that repeatedly asks the user to enter product names and
prices. Store all of these in a dictionary whose keys are the product names and
whose values are the prices.

d = {}

ans = "y"

while ans == "y" or ans == "Y" :

p_name = input("Enter the product name: ")

p_price = float(input("Enter product price: "))

d[p_name] = p_price

ans = input("Do you want to enter more product names? (y/n): ")

ans = "y"

while ans == "y" or ans == "Y" :

p_name = input("Enter the product name to search: ")

print("Price:", d.get(p_name, "Product not found"))

ans = input("Do you want to know price of more products? (y/n): ")
23. Create a dictionary whose keys are month names and whose values are the
number of days in the corresponding months.
Source Code:
days_in_months = {
"january":31,
"february":28,
"march":31,
"april":30,
"may":31,
"june":30,
"july":31,
"august":31,
"september":30,
"october":31,
"november":30,
"december":31
}
m = input("Enter name of month: ")
if m not in days_in_months:
print("Please enter the correct month")
else:
print("There are", days_in_months[m], "days in", m)

print("Months in alphabetical order are:", sorted(days_in_months))

print("Months with 31 days:", end=" ")


for i in days_in_months:
if days_in_months[i] == 31:
print(i, end=" ")

day_month_lst = []
for i in days_in_months:
day_month_lst.append([days_in_months[i], i])
day_month_lst.sort()

month_day_lst =[]
for i in day_month_lst:
month_day_lst.append([i[1], i[0]])

sorted_days_in_months = dict(month_day_lst)
print()
print("Months sorted by days:", sorted_days_in_months)
24. Repeatedly
ask the user to enter a team name and how many games the
team has won and how many they lost. Store this information in a dictionary
where the keys are the team names and the values are lists of the form [wins,
losses].
Source Code:-
d = {}
ans = "y"
while ans == "y" or ans == "Y" :
name = input("Enter Team name: ")
w = int(input("Enter number of wins: "))
l = int(input("Enter number of losses: "))
d[name] = [w, l]
ans = input("Do you want to enter more team names? (y/n): ")

team = input("Enter team name for winning percentage: ")


if team not in d:
print("Team not found", team)
else:
wp = d[team][0] / sum(d[team]) * 100
print("Winning percentage of", team, "is", wp)

w_team = []
for i in d.values():
w_team.append(i[0])

print("Number of wins of each team", w_team)

w_rec = []
for i in d:
if d[i][0] > 0:
w_rec.append(i)

print("Teams having winning records are:", w_rec)


25. Writea program to convert a number entered by the user into its
corresponding number in words. For example, if the input is 876 then the output
should be 'Eight Seven Six'.
(Hint. use dictionary for keys 0-9 and their values as equivalent words.)

Source Code:

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


d = {0 : "Zero" , 1 : "One" , 2 : "Two" , 3 : "Three" , 4 : "Four" , 5 : "Five" , 6 :
"Six" , 7 : "Seven" , 8 : "Eight" , 9 : "Nine"}
digit = 0
str = ""
while num > 0:
digit = num % 10
num = num // 10
str = d[digit] + " " + str

print(str)
Write a program to create a nested tuple to store roll number, name and
29.
marks of students.

Souce code:
tup = ()

ans = "y"
while ans == "y" or ans == "Y" :
roll_num = int(input("Enter roll number of student: "))
name = input("Enter name of student: ")
marks = int(input("Enter marks of student: "))
tup += ((roll_num, name, marks),)
ans = input("Do you want to enter more marks? (y/n): ")
print(tup)
30.Create a tuple ('a', 'bb', 'ccc', 'dddd', ... ) that ends with 26 copies of the letter
z using a for loop.

Source code:
tup = ()
for i in range(1, 27):
tup = tup + (chr(i + 96)* i,)
print(tup)
Write a program that calculates and displays the mean of a tuple with
28.
numeric elements.

Source code:
tup = eval(input ("Enter the numeric tuple: "))

total = sum(tup)
tup_length = len(tup)
mean = total / tup_length

print("Mean of tuple:", mean)


27.Write a program that inputs two tuples and creates a third, that contains all
elements of the first followed by all elements of the second.

Source code:

tup1 = eval(input("Enter the elements of first tuple: "))


tup2 = eval(input("Enter the elements of second tuple: "))
tup3 = tup1 + tup2
print(tup3)

You might also like