Page 407 - Ai_417_V3.0_C9_Flipbook
P. 407
Important Features of Lists
Based on the above definition, the list has the following important features:
• It is mutable data type.
• It is an ordered sequence of values.
• Each element is separated by comma and enclosed in square brackets [ ].
• Each value/element is accessed by an index number.
• The values can be added, modified or deleted by the user throughout the program.
• It stores the values of different data types.
Creating a List
A list is created by enclosing the values within square brackets. Each value/element is separated by a comma
and stored under a common name. These values can be of different data types. The name follows the rules of
naming conventions for identifiers as shown below:
Examples Commands
Creating an empty list list1 = []
Creating a list with single value list2 = [56]
Creating a list of friends friends = ["Saisha", "Advika", "Arshia"]
Creating a list of marks marks=[92, 91, 89, 95, 93]
Creating a list of students with roll no students = [21, "Gunjan", 94.5, 34, "Rohit",
and percentage (list of mixed values) 91.3]
Creating a list within another list section = ["A", 25, "English", ["B",34], ["C",37],
56.7]
Creating a list with repeated values L1 = [10] * 5 # L1 will have [10, 10, 10, 10,
10]
Creating a list from another sequence alpha = list("hello") #string converted to a
or string using list( ) function list #output will ['h', 'e', 'l', 'l', 'o']
Values input from the user in a list L1 = list(input("enter numbers :"))
Accessing the Elements of a List
The elements of a list can be accessed by using its index number starts with 0 (zero). There are two different
ways of using the index number:
• Positive/Forward Indexing: The index number begins with 0 till length-1 and we refer the elements from left
to right.
• Negative/Backward Indexing: The index number begins with -1 till -length and we refer the elements from
right to left. For example,
L1 = ['P', 'Y', 'T', 'H', 'O', 'N']
Forward Indexing 0 1 2 3 4 5
P Y T H O N
–6 –5 –4 –3 –2 –1 Backward Indexing
Introduction to Python 405

