0% found this document useful (0 votes)
20 views4 pages

Unit 4 Notes

python simple programs cbse

Uploaded by

Chamu Ndeeswari
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)
20 views4 pages

Unit 4 Notes

python simple programs cbse

Uploaded by

Chamu Ndeeswari
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/ 4

Unit-4 ch-2 Lists

EXERCISE
1. Define list

2. What is the output of the following code:


a) print type ([1,2])
(i) <type „complex‟> (ii) <type „int‟> (iii) <type „list‟>
b) a= [1, 2, 3, None, ( ), [ ]}
print len(a)
(i) Syntax error (ii) 4 (iii) 5 (iv) 6 (v) 7

3. Write the output from the following code:


A=[2,4,6,8,10]
L=len(L)
S=0
for I in range(1,L,2):
S+=A[I]
print “Sum=”,S

4. Find the errors from the following program


n=input (Enter total number of elements)
l=range(n)
print l
for i in (n);
l[i]=input("enter element")
print "All elements in the list on the output screen"
for i on range(n):
print l[i]

5. Write a function group of list (list, size) that takes a list and splits into smaller
list
of given size.

6. Write a function to find all duplicates in the list.

7. For each of the expression below, specify its type and value. If it generates
error,
write error.
Assume that expressions are evaluated in order.
x= [1, 2, [3, „abc‟, 4], „Hi‟]
(i) x[0]
(ii) x[2]
(iii) x[-1]
(iv) x[0:1]
(v) 2 in x
(vi) x[0]=8
8. For each of the expression below, specify its type and value. If it generates
error,
write error:
List A= [1, 4, 3, 0]
List B= [„x‟, „z‟, „t‟, „q‟]
(i) List A.sort ( )
(ii) List A
(iii) List A.insert (0, 100)
(iv) List A.remove (3)
(v) List A.append (7)
(vi) List A+List B
(vii) List B.pop ( )
(viii) List A.extend ([4, 1, 6, 3])

LAB EXERCISE
1. We can use list to represent polynomial.
For Example
p (x) = -13.39 + 17.5 x + 3 x2 + x4
can be stored as
[-13.39, 17.5, 3, 1.0]
Here „index‟ is used to represent power of „x‟ and value at the index used to
represent the coefficient of the term.
Write a function to evaluate the polynomial for a given „x‟.

2. Write a function that takes a list of numbers and returns the cumulative sum;
that
is, a new list where the its element is the sum of the first i+1 elements from the
original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].

3. Write a function called chop that takes a list and modifies it, removing the
first and
last elements, and returns None. Then write a function called middle that takes
a list
and returns a new list that contains all but the first and last elements.

4. Write a function called is_sorted that takes a list as a parameter and returns
True if
the list is sorted in ascending order and False otherwise. You can assume (as a
precondition) that the elements of the list can be compared with the relational
operators <, >, etc.
For example, is_sorted ([1, 2, 2]) should return True and is_sorted ([‘b’, ‘a’])
should
return False.

5. Write a function called remove_duplicates that takes a list and returns a new
list
with only the unique elements from the original. Hint: they don‟t have to be in
the
same order.

6. Write a function that takes in two sorted lists and merges them. The lists may
not
be of same length and one or both may be empty. Don‟t use any Python built-in
methods or functions.
7. Create a list that contains the names of 5 students of your class. (Do not ask
for
input to do so)
(i) Print the list
(ii) Ask the user to input one name and append it to the list
(iii) Print the list
(iv) Ask user to input a number. Print the name that has the number as index
(Generate error message if the number provided is more than last index
value).
(v) Add “Kamal” and “Sanjana” at the beginning of the list by using „+‟.
(vi) Print the list
(vii) Ask the user to type a name. Check whether that name is in the list. If exist,
delete the name, otherwise append it at the end of the list.
(viii) Create a copy of the list in reverse order
(ix) Print the original list and the reversed list.
(x) Remove the last element of the list.

8. Use the list of student names from the previous exercise. Create a for loop
that asks
the user for every name whether they would like to keep the name or delete it.
Delete the names which the user no longer wants. Hint: you cannot go through
a
list using a for loop and delete elements from the same list simultaneously
because
in that way the for loop will not reach all elements. You can either use a second
copy of the list for the loop condition or you can use a second empty list to
which
you append the elements that the user does not want to delete.

9. Write a function to find product of the element of a list. What happens when
the
function is called with list of strings?

10. Write a program to input NXM matrix and find sum of all even numbers in
the
matrix.

11. Write a program to print upper triangle matrix.

12. Write a program to print lower triangle matrix.

13. Write a program to find sum of rows and columns of the matrix.

You might also like