0% found this document useful (0 votes)
7 views10 pages

List Notes

Python list

Uploaded by

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

List Notes

Python list

Uploaded by

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

LIST

1. How many types of built-in types of sequences in Python.


Python has six built-in types of sequences.
2. How can we remove list element ?
To remove a list element, you can use either the del statement or the remove])
method?
3. Give an example to access values in lists.
To access value in lists, use the square brackets for slicking along with the index or
indices to obtain value available at that index.
4. Write the output of the given Python code :

aList = [123, ‘xyz’, ‘zara’, ‘abc’, 123];


bList = [2009, ‘manni’];
aList.extend (bList)
print “Extended List :”, aList;

Answer:

This will produce the following result:


Extended List : [123, ‘xyz’, ‘zara’, ‘abc’, 123, 2009, ‘manni’]

5. Write the output of the given python code :

aList1 = [123, ‘xvz’, zara’, abc’];


print “Index for xyz : ” aList. index) ‘xyz’);
print “Index for zara :”, aList. index(‘zara’);

Answer:

This will produce the following result:


Index for xyz : 1 Index for xxx : 2

6. Write the output of the given python code :


aList = [123, ‘xyz’, ‘zara’, ‘abc’];
aList.insert (3,2009) print “Final List:”, aList
Answer:
Output:
Final List: [123, ‘xyz’, ‘zara’, 2009, ‘abc’]
7. Write the output of the given python code :

aList1 = [123, ‘xyz’, ‘zara’, ‘abc’];


aList.insert (3,2009) print “Final Lista List

Answer:

Output:
Final List: [123, ‘xyz’, ’zara1, 2009,’abc1’]
8. Write the output of the given python code :

aList1 = [123, ‘xyz’, ‘zara’, ‘abc’];


print “A List:”, aList.pop()
print “B List:”, aList.pop(2)

Answer:

Output:
A List: abc B List: zara

9. How are lists different from strings when both are sequences ?
Answer:
The lists and strings are different in following ways :

(a) The lists are mutable sequences while strings are immutable.
(b) Strings store single type of elements, all characters while lists can store
elements belonging to different types.
(c) In consecutive locations, strings store the individual characters while list
stores the references of its elements.

10. Write a program to calculate and display the sum of all the odd numbers in the
list.

Answer:

pos = 0
sum = 0
while pos < len (L):
if L[pos] %2 = = 1 :
sum = sum + L [pos]
pos = pos + 1
print sum

11. What is a tuple ?


Answer:
Tuple is a sequence of immutable Python objects

12. Can we remove individual tuple elements ?


Answer:
No, we cannot remove individual tuple elements.

13. Write a program to input ‘n’ numbers and store it in tuple.


Answer:
t = tuple ( )
n = input (“Enter any number”)
print “Enter all numbers one after other”
for i in range (n) :
a = input (“Enter number”)
t = t+(a, )
print “Output is”
print t

14. Write a program to input any two tuples and interchange the tupel values.
Answer:
t1 = tuple( )
n = input(“Total number of values m first tuple”) for i in range (n):
a = input(“Enter elements”)
t2 = t2 + (a, )
print “First tuple”
print t1
print “Second tuple”
print t2
t1, t2 = t2, t1
print “After swapping”
print “First tuple”
print t1
print “Second tuple”
print t2

15.

You might also like