A7_Assignment
A7_Assignment
23
Ex. No: 7
following conditions:
if Marks >=90 then grade A
if Marks >=80 && <90 then grade B
if Marks >65 && < 80 then grade C
if Marks > =40 && <=65 then grade D
if Marks <40 then grade F
Create the List of Marks of 5 Students in English Subject
by getting the input
from the user
Python Code
def Assign_grade():
global marks
marks = []
for i in range(0, 5):
n = int(input("What is the student's mark?"))
marks.append(n)
print(marks)
Assign_grade()
for i in marks:
if i >= 90:
print(i, "Grade A")
elif i >= 80 and i < 90:
print(i, "Grade B")
elif i >= 65 and i < 80:
print(i, "Grade C")
elif i >= 40 and i <= 65:
print(i, "Grade D")
else:
print(i, "Grade F")
Test Cases
Test case 1:
Get the input of students mark for 5 students with the
below question
Expected output: “what is the students mark?”
Input: 95
Expected output: “what is the students mark?”
Input: 78
Expected output: “what is the students mark?”
Input: 56
Expected output: “what is the students mark?”
Input: 87
Expected output: “what is the students mark?”
Input: 67
Test case 2:
Provide valid input (marks) for 5 students and it should
display the marks as list and the grade for each marks
based on the condition given
if Marks >=90 then grade A
if Marks >=80 && <90 then grade B
if Marks >65 && < 80 then grade C
if Marks > =40 && <=65 then grade D
if Marks <40 then grade F
Test case 3:
Provide invalid input (marks) as string and it should
display the following error immediately after the string
input
Input: xy
Expected error: invalid literal for int() with base 10: 'yx'
Test case 4:
Provide invalid input (marks) as float it should display
the following error immediately after the string input
Input: 95.6
Expected error: invalid literal for int() with base 10: '95.6'
Output
Python Code
import random
glist = []
for i in range(0, 10):
n = random.randrange(0, 6)
glist.append(n)
print (glist)
new_list = []
for i in glist:
if i not in new_list:
new_list.append(i)
print(new_list)
Test Cases
Test case 1:
Test case 2:
When you run the program, the system to generate 10
random numbers only between 0 and 6
Expected output: [4,0,4,3,2,1,4,5,4,2]
Test case 3:
Duplicate numbers to be removed and only unique numbers
to be displayed as a next list from the system generated
10 random numbers
Expected output: [4,0,3,2,1,5]
Output
Python Code
arr = []
n = int(input("How many elements?"))
for i in range(0, n):
a = int(input("What is the number?"))
arr.append(a)
print("The given list is", arr)
prod_list = []
pro = 1
for i in range(0, len(arr)):
for j in range(0, len(arr)):
if i == j:
continue
else:
pro = pro*list[j]
prod_list.append(pro)
pro = 1
print("The modified list is", prod_list)
Test Cases
Test case 1:
Get the number of elements as input
Expected output: “How many elements?”
Input: 3
Test case 2:
If the input given is ‘3’ for no. of elements, should get
3 numbers from users by asking ‘what is the number’ 3
times
Expected output: “What is the number?”
Input: 1
“What is the number?”
Input: 2
“What is the number?”
Input: 3
Test case 3:
Once the input is received, do the calculation as per
below logic and display the modified list
Eg: l1=[1,2,3] where l1[0]==2*3, l1[1]=1*3 and l1[2]=1*2
Prod_list=[6,3,2]
Expected output:[6, 3, 2]
Output
Python Code
list1 = []
n = int(input("How many elements?"))
for i in range(0, n):
a = input("What is the item?")
list1.append(a)
print(list1)
def print_reverse():
new_list = []
for i in range(len(list1)-1, -1, -1):
new_list.append(list1[i])
print(new_list)
print_reverse()
Test cases:
Test case 1:
Get the number of elements as input
Expected output: “How many elements?”
Input: 5
Test case 2:
If the input given is ‘5’ for no. of elements, should get
5 nitems from users by asking ‘what is the item’ 5 times
and display them as a list
Expected output: “What is the item?”
Input: 4
“What is the item?”
Input: 6
“What is the item?”
Input: 5
“What is the item?”
Input: 3
“What is the item?”
Input: 2
Test case 3:
Once the input list is received, Print the reversed list
Expected output: [‘2’, ‘3’, ‘5’, ‘6’, ‘4’]
Output
Python Code
glist = []
k = None
for i in range(0, 10):
y = input("What is the students name?")
glist.append(y)
print(glist)
x = input("Which students name do you want to search?")
for i in range(0, len(glist)-1):
if glist[i] == x:
k = True
else:
continue
if k == True:
print("The student", x, "is present")
else:
print("The student", x, "is not present")
mid = 0
while low <= high:
mid = (high+low)//2
if list1[mid]<n:
low = mid + 1
elif list1[mid]>n:
high = mid - 1
else:
return mid
return -1
glist = []
for i in range(0, 10):
y = input("What is the students name?")
glist.append(y)
print(glist)
glist.sort()
n = input("What is the element you want to find?")
result = binary_search(glist, n)
if result != -1:
print("The element is present in index", result)
else:
print("Element not found")
Test Case
Test case 1:
Get Input of 10 students name by asking the below
question 10 times
Expected output: “What is the students name?”
Input: k
P
Q
S
L
C
N
A
W
T
Test case 2:
Sort the received name list in ascending order and
display the sorted list
Expected output: [‘a’, ‘c’, ‘k’, ‘l’ ‘n’, ‘p’, ‘q’, ‘s’,
‘t’, ‘w’]
Test case 3:
Get the name to be searched from user, search that name
in the list and display the corresponding index number
for that name
Expected output: ”What is the element you want to find?”
Input: t
Expected output: ”The element is present in index 8”
Test case 4:
Get the name to be searched from user, search that name
in the list and display whether the student is present in
the list or not
Expected output: ”Which name do you want to search?”
Input: q
Expected output: ”The student q is present”
Output
Python Code
lname = []
neet = []
jee = []
a = int(input("Total number of students?"))
for i in range(0, a):
name = input("What is the students name?")
n = input("Have they passed NEET?")
j = input("Have they passed JEE?")
lname.append(name)
if n == "yes":
neet.append(name)
if j == "yes":
jee.append(name)
else:
continue
print("Students who have attended atleast one exam:", lname)
print("Students who have passed neet: ", neet)
Test Case
Test case 1:
Get Input of total no. of students
Expected output: “Total no. of students?”
Input: 5
Test case 2:
Get the input of student name, whether they have passed
JEE and NEET. Program to get the input for 5 times by
asking the below questions
Expected output: “What is the student name?” Input: k
“Have they passed NEET?” Input: yes
“Have they passed JEE?” Input: yes
“What is the student name?” Input: m
“Have they passed NEET?” Input: yes
“Have they passed JEE?” Input: no
“What is the student name?” Input: v
“Have they passed NEET?” Input: yes
“Have they passed JEE?” Input: yes
“What is the student name?” Input: s
“Have they passed NEET?” Input: no
“Have they passed JEE?” Input: no
“What is the student name?” Input: h
Test case 3:
Find the students who have attended atleast one exam and
it should display their name in the list
Expected output: ”students who have attended atleast one
exam : [‘k’, ‘m’, ‘v’, ‘s’, ‘h’]”
Test case 4:
Find the students who have passed NEET, JEE and both and
should display their name as each separate list
Expected output: ”Which name do you want to search?”
Input: q
Expected output: ”students Who have passed NEET: [‘k’,
‘m’, ‘v’]”
Expected output: ”students Who have passed JEE: [‘k’,
‘v’, ‘h’]”
Expected output: ”students Who have passed both NEET and
JEE: [‘k’, ‘v’]”
Output
Python Code
data = [5, 3, 7]
for i in range(0, len(data)):
if i == 0:
data[i]= data[i]*(-1)
else:
continue
print(data)
data.append(10)
print(data)
data.insert(2, 22)
print(data)
data.pop(1)
print(data)
newdata = [9, 11, 13]
data.extend(newdata)
print(data)
index = 0
for i in range(0, len(data)):
if data[i]==7:
break
else:
index += 1
print(index)
print(max(data))
print(min(data))
data.sort()
print(data)
Test Case
Test case 1:
Test case 2:
When you run the program, perform the following operation
and display the output as a list
Add the value 10 to the end of data.
Expected output: [-5, 3, 7, 10]
Test case 3:
When you run the program, perform the following operation
and display the output as a list
Insert the value 22 at position 2 in data
Expected output: [-5, 3, 22, 7, 10]
Test case 4:
When you run the program, perform the following operation
and display the output as a list
Test case 5:
When you run the program, perform the following operation
and display the output as a list
Locate the index of the value 7 in data, safely.
Find the maximum element in the list.
Find the sum of all elements in the list.
Sort the values in data.
Expected output: 2
22
-5
[-5, 7, 9, 10, 11, 13, 22]
Output
Python Code
glist = []
n = int(input("Total number of names: "))
for i in range (0, n):
a = input("Enter the name: ")
glist.append(a)
print(glist)
count = 0
for j in glist:
for k in j:
if k == "a":
count = count + 1
else:
continue
print("The total number of a is", count)
Test Code
Test case 1:
Get Input of total no. of names
Expected output: “Total no. of names?”
Input: 5
Test case 2:
Get the input of name. Program to get the input for 5
times by asking the below question
Test case 3:
The program should count and display the number of times
that the alphabet ‘a’ appears in all the names
Expected output: “The total number of a is 5”
Output
Python Code
x = [[1, 2],
[3, 4]]
y = [[5, 6],
[7, 8]]
result = [[0,0], [0, 0]]
print(len(x))
print(len(y))
# iterate through rows
for i in range(len(x)):
# iterate through columns
for j in range(len(x[0])):
X = [[1, 2, 3],
[4 , 5, 6],
[7 , 8, 9]]
Y = [[5, 8, 1],
[6, 7, 3],
[4, 5, 9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
print(result)
Test case
Test case 1:
When you run the program, it should create 2 matrices. To
verify the same, print the length of list
Expected output: 2
2
Test case 2:
When you run the program, it should create 2 matrices and
perform the below operation
Pass 2 matrices to a function called add_mat(m1,m2).
Compute addition of 2 matrices and return the resultant
matrix.
Test case 3:
When you run the program, it should create 2 matrices and
perform the below operation
Pass 2 matrices to a function called mul_mat(m1,m2).
Compute
multiplication of 2 matrices
Expected output: [[29, 37, 34], [74, 97, 73], [119, 157,
112]]
Output
Python Code
L1 = [1, 2, 3, 4]
L2 = [1, 2, 3, 4]
L3 = []
o = [L3.append(L1[x]+L2[x]) for x in range(4)]
print(L3)
Test Case
Test case 1:
When you run the program, it should display the list
containing only integers
Expected output: [1, 4, 9, 0, 4]
Test case 2:
Get Input of string and display only the consonant from
the given string
Expected output: “what is the string?”
Input: hello
Expected output: [‘h’, ‘l’, ‘l’]
Test case 3:
Get the input of the limit (till what number) and perform
the following
Multiples of 10 (n values)
Construct a list of the form: [‘1a’,’2a’,’3a’,’4a’]
Create a list which stores the sum of each of the
elements from the two lists.
Expected output: “Till What number?” Input: 60
Expected output: [10, 20, 30, 40, 50, 60]
Expected output: [‘1a’, ‘2b’, ‘3c’, ‘4d’]
Expected output: [2, 4, 6, 8]
Output
Python code
m = [[1,2],[3,4]]
rez = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
print(rez)
Test case
Test case 1:
When you run the program, it should display the given
matrix in the program
Expected output: [[1, 2], [3, 4]]
Test case 2:
It should display the transpose of the given matrix
Expected output: [[1, 3], [2, 4]]
Output
Learning Outcomes