Python Revision Tour – II : String, Lists, Tuples, Dictionary – Notes

Class 12 Computer Science Python Revision Tour – II Notes


List Manipulation


What is a List?

Python lists are containers that are used to store a list of values of any type. Python lists are mutable i.e. you can change the elements of a list in place. Lists are depicted through square brackets [ ]. It can store a sequence of values belongings of any type.

Example: –

list1 = [ 1, 2, 3 , 4 , 5 ]   

list2 = [1.5, 2.5,  3.6, 4.9]      

list3 = [‘a’, ‘n’, ‘j’, ‘e’, ‘e’, ‘v’ ] 

list4 = [‘anjeev’, ‘singh’ ]

How to create an Empty List?

t  = [ ]
t  = list( )

How to create a Value List?

t  = [value1, value2, …. ]
t  = list( <sequence>) 

What is a Nested List?

A list contains another list i.e. list inside another list is called a nested list.
t  = [ [ ]]

list5 = [[1, 2, 3], [4, 5, 6, 7] ]
list = [1, [2, 3, 4], 5, [‘ramesh’, ‘suresh’] ]

Characteristics of List

List is a type of sequences like strings and tuples but it differs from them in the way that lists are mutable but strings and tuples are immutable. Dictionaries are also mutable.

Indexing in List

List is a type of sequence like strings & tuples, so it has also followed the same rule of indexing. Every element of the list has a unique index value.

The indexes of the list begin from 0 to length-1, in the forward direction, and  -1, -2, -3, …., -length in the backward direction.

How to input list through Keyboard?

eval( )

eval( ) function of Python can be used to evaluate and return the result of an expression given as a string.

list( )

list( ) function convert all inputted characters/symbols/digits as the elements of list using each character input.

Input List value at the run time i.e. by keyboard:

>>> t1  = list(input(‘Enter List : ’ ))

Enter a List: [1,2,3,4]   

# then t1 will store as –

>>> print(t1)

[‘[‘, ‘1’, ‘,’, ‘2’, ‘,’, ‘3’, ‘,’, ‘4’, ‘]’]

>>> t2  = eval(input(‘Enter List : ’))

Enter a List : [1,2,3,4]    # then t2 will store as –

>>> print(t2)

[1, 2, 3, 4]

List vs. String

There are some similarities and differences between List and Strings.

Similarity: –
ParticularsDescription
len ( )Both allows to use len( ) method to find number of elements.
IndexingBoth allows forward (0,1, 2 , 3, …..) and backward (-1, -2, -3, .., -n).
MembershipBoth allows to check membership using in or not in.
SlicingBoth allow to extract of some elements by using slicing
Concatenation and ReplicationBoth allows to use concatenation (+) and replication (*) operator
Accessing Individual ElementBoth allow to access the element from the given index position.
Difference: –
ParticularsDescription
StorageList stores reference at each index instead of single character as in string.
MutabilityStrings are not mutable while lists are mutable.


Class 12 Computer Science Python Revision Tour – II Notes


Leave a Comment

You cannot copy content of this page

Scroll to Top