0% found this document useful (0 votes)
10 views21 pages

A7_Assignment

Uploaded by

hopeless2693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

A7_Assignment

Uploaded by

hopeless2693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Date: 22.01.

23
Ex. No: 7

LISTS, NESTED LISTS AND LISTS COMPREHENSION


Aim

To execute simple python programming using lists, nested lists and


lists comprehension.

Q.No 1 : Write a function Assign_grade(Lst) which reads


the marks of a student from a list
and assigns a grade and display the grade based on the

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")

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

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

Input: 95, 78, 56, 87, 67


Expected output: [95, 78, 56, 87, 67]
95 Grade A
78 Grade C
56 Grade D
87 Grade B
67 Grade C

Test case 3:
Provide invalid input (marks) as string and it should
display the following error immediately after the string
input
Input: xy

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

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

Q.No 2 : Generate a list of 10 elements between 0 to 6


using random number generator.
Write a function rem_dup( glist) which gets the generated
list as input and
remove the duplicates and return the updated list

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:

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

When you run the program, the system to generate 10


random numbers and it should get displayed as list.
Expected output: [4,0,4,3,2,1,4,5,4,2]

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

Q.No 3 : Create a list arr with “n” integers, construct a


new list prod_list of same size
such that prod_list[i] is equal to the product of all the
elements of arr except
arr[i].
Eg: l1=[1,2,3] where l1[0]==2*3, l1[1]=1*3 and l1[2]=1*3
Prod_list=[6,3,2]

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:

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

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

Q.No 4 : Write a function print_reverse(Lst) to reverse


the elements of a list. Pass the list
as parameter to a function which reverses the list

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

without using slice operation.


Print the reversed list in the main program. (Avoid using
return statement in
function)

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

[‘4’, ‘6’, ‘5’, ‘3’, ‘2’]

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Test case 3:
Once the input list is received, Print the reversed list
Expected output: [‘2’, ‘3’, ‘5’, ‘6’, ‘4’]

Output

Q.No 5 : Create a list of names with 10 students and


perform the following:
i. Print the list
ii. Search for a particular student (linear search)
iii. Sort the students list and then do search (binary
search)

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")

def binary_search(list1, a):


low = 0
high = len(list1)-1

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Q.No 6 : Create a list with “n” students name who have


passed in JEE. Create another list
with “m” students name who have passed NEET exams.
Perform the following
operations:
a. Find the list of students who have qualified in both
the exams.
b. Print the students who have passed only in JEE as new
list.
c. Print the students who have passed only in NEET as new
list.
d. Print the students who have attended at least one
exam.

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)

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

print("Students who have passed jee: ", jee)


new_list = []
if len(neet)> len(jee):
for i in neet:
for j in jee:
if i == j:
new_list.append(j)
else:
continue
else:
for i in jee:
for j in neet:
if i==j:
new_list.append(j)
else:
continue

print("students who have passed both jee and neet", new_list)

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

“Have they passed NEET?” Input: no


“Have they passed JEE?” Input: yes

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

Q.No 7 : Assume that the variable’ data’ refers to the


list [5, 3, 7]. Write the expressions that
perform the following tasks:

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

a. Replace the value at position 0 in data with that


value’s negation.
b. Add the value 10 to the end of data.
c. Insert the value 22 at position 2 in data.
d. Remove the value at position 1 in data.
e. Add the values in the list newData = [9,11,13] to the
end of data.
f. Locate the index of the value 7 in data, safely.
g. Find the maximum element in the list.
h. Find the sum of all elements in the list.
i. Sort the values in data.

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:

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

When you run the program, perform the following operation


and display the output as a list
Replace the value at position 0 in data with that value’s
negation.

Expected output: [-5, 3, 7]

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

Remove the value at position 1 in data.


Add the values in the list newData = [9,11,13] to the end
of data.
Expected output: [-5, 22, 7, 10]
[-5, 22, 7, 10, 9, 11, 13]

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]

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Output

Q.No 8 : Write a Python program that prompts the user to


enter a list of names and stores
them in a list. The program should display how many times
the letter 'a' appears
within the list.

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Expected output: “Enter the name?” Input: ads


“Enter the name?” Input: hse
“Enter the name?” Input: aasp
“Enter the name?” Input: qoawsa
“Enter the name?” Input: pqwed

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

Q.No 9 : Create two matrices and perform the following


actions:
i) Pass 2 matrices to a function called add_mat(m1,m2).
Compute addition of
2 matrices and return the resultant matrix.
ii) Pass 2 matrices to a function called mul_mat(m1,m2).
Compute
multiplication of 2 matrices

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])):

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

result[i][j] = x[i][j] + y[i][j]


print(result)

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.

Expected output: [[6, 8], [10, 12]]

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Expected output: [[29, 37, 34], [74, 97, 73], [119, 157,
112]]

Output

Q.No 10 : Give an appropriate list comprehension for each


of the following:
i) L1 = [1, ’x’, 4, 5.6, ’z’, 9, ‘a’, 0, 4] create a list
which consists of integer
values.
ii) Producing a list of consonants that appear in string
w.
iii) Multiples of 10 (n values)
iv) Construct a list of the form: [‘1a’,’2a’,’3a’,’4a’]
v) Create a list which stores the sum of each of the
elements from the two lists.

Python Code

L1 = [1, 'x', 4, 5.6, 'z', 9, 'a', 0, 4]


t = [x for x in L1 if (type(x)==int)]
print(t)

w = input("What is the string?")


lst = []
for letter in w:
lst.append(letter)
j = [x for x in lst if x != 'a' and x!='e' and x!='i' and x!= 'o' and x!
='u']
print(j)

n = int(input("Till what number?"))


list = []
for i in range(1, n+1):
list.append(i)

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

j = [x for x in list if (x%10==0)]


print(j)

L1 = ['1', '2', '3', '4']


L2 = ['a', 'b', 'c', 'd']
L3 = []
o = [L3. append(L1[x]+L2[x]) for x in range(4)]
print(L3)

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Q.No 11 : Find the transpose of a given matrix using list


comprehension

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

Department of Computer Science and Engineering


Date: 22.01.23
Ex. No: 7

Learning Outcomes

From this assignment I have learnt the following:


 How to create and store elements in a list
 How to access elements of the list using the index ranges
 How to iterate through a list using for and while loops
 How to do list comprehension
 How to do operations using nested lists

Department of Computer Science and Engineering

You might also like