Understanding Lists - Introduction
What is Lists
Lists contains a group of different elements and is similar to arrays.
Example: Employee = [‘parth’, 5, ‘M’, 2, 11, 1104]
We can also create an empty list by wiring empty [ ] bracket as given in below example.
Epm = [ ]
Now if we want to print any list we can use the print command as given below
Indexing a List
When we index any list, we access the element of the list by providing its position on that list. The Position number starts from 0 onwards and written inside square bracket, as shown in below example
Print (Employee ) will print 5 and print (Employee ) will print Parth.
Slicing a List
With the help of slicing we can extract the certain element from a List by providing starting and ending position numbers.
Syntax of Slicing a List is [Start: Stop: Step Size]
By default the start is 0 and stop will be last element and by default step size will be 1 and we can either define the step size also.
- Print (Employee [0:3:1]) will print parth’, 5, ‘M’
- Print (Employee [0:3:] will print parth’, 5, ‘M’
- Print9Employee[::] will print [‘parth’, 5, ‘M’, 2, 11, 1104
Program1: Write a Python program to create list with type of elements
Creating List using range () function
range() can be used to create the list which can store a range of elements. Syntax of range function is :
By default start is assumed to 0. Step Size is taken as 1. End is last boundary
This range () is also used in for Loops to display the numbers or with list () function to create the list ().
lst = list (range (3, 10, 2))
This will print the following: lst = 3,5,7,9
Program 2: Write a python program to create list via range ()