10+-+Lists+in+Python (1)
10+-+Lists+in+Python (1)
• Presentation By Uplatz
• Contact us: https://training.uplatz.com
• Email: info@uplatz.com
• Phone: +44 7836 212635
Lists
Learning outcomes:
Python Lists
Accessing Values in Lists
Updating Lists
Deleting List Elements
Basic List Operations
Built-in List Functions and Methods
List
The list is a most versatile data type available in
Python which can be written as a list of comma-
separated values (items) between square brackets.
Important thing about a list is that items in a list
need not be of the same type.
A single list may contain Data Types like Integers,
Strings, as well as Objects. Lists are mutable, and
hence, they can be altered even after their
creation. List in Python are ordered and have a
definite count. The elements in a list are indexed
according to a definite sequence and the indexing
of a list is done with 0 being the first index.
List
❖Collection of Multiple Data.
❖Holds Multiple Data types.
❖Lists are Mutable.
❖Store multiple data at same time.
❖Creating a list is as simple as putting different
comma-separated values between square brackets.
For example:
a = [1, 12.5, ‘a’, “python”]
list1 = ['physics', 'chemistry', 1997, 2000];
Accessing Values in Lists
To access values in lists, use the square brackets for
slicing along with the index or indices to obtain
value available at that index.
For example:
a = [1, 12.5, ‘a’, “python”, “Programming”]
print(a[1]) # Output is 12.5
print(a[3]) # Output is python
print(a[1:4]) # Output is [12.5, ’a’, ‘python’]
Accessing Values in Lists
Example:
L = [9,18,'Hi',12,"Hello",15.55,'Programming',100,125.5]
print(L[5])
print(L[1:5])
print(L[2:8])
print(L[2:9:3])
print(L[-1])
print(L[-5])
How to take every nth-element of a list?
What if we want to have only every 2-nd element
of L? This is where the step parameter comes into
play.
Accessing Values in Lists
Example:
L = [9,18,'Hi',12,"Hello",15.55,'Programming',100,125.5]