Practical ScA
Practical ScA
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
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
print("Original Sentences:")
print(str)
for i in range(len(l1)):
l2.append(l1[i][1:])
Source Code:
str = input("Enter a string: ")
words = str.split()
longWord = ''
for w in words :
if len(w) > len(longWord) :
longWord = w
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"
d[p_name] = p_price
ans = input("Do you want to enter more product names? (y/n): ")
ans = "y"
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)
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): ")
w_team = []
for i in d.values():
w_team.append(i[0])
w_rec = []
for i in d:
if d[i][0] > 0:
w_rec.append(i)
Source Code:
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
Source code: