Page 78 - Code & Click - 8
P. 78
2. Press the F5 key to execute the code. Sample output of the program is given below.
WORKING WITH LISTS
A List is a special type of variable that acts like a container and can store multiple values in it.
The values stored in a list are called its elements and can be of the same data type or different data types.
Each element in a list is assigned an index, starting with 0, that can be used to access it. The various
elements of a list are enclosed within brackets ([ ]), separated by commas.
Creating a List
A list in Python can be created in two ways:
1. Assigning values to a list while writing the code
The various values in a list are specified, enclosed within brackets ([ ]), and separated by commas.
For example, list_num = [1, 2, 3, 4, 5]
2. Assigning values to a list while execution of program
The values for a list can be accepted from the user and stored in the list, using a loop and list
functions.
Empty Lists
A list without any elements in it is called an Empty list. An empty list can be crated in two ways:
• list1 = [ ] • list1 = list( )
Mixed Data List
A mixed data list contains values of different data types. An example of a mixed data list is
[“A”, 5, “Python”, 10, “Hello”]
Accessing a List
Similar to traversing a string, a list can be traversed and its elements accessed by using the index of
its elements.
Experiential Learning
Lab Activity 6
To create a list with mixed data values and display the elements in the list.
1. Type the code as shown in the image in the Python Editor.
76