0% found this document useful (0 votes)
3 views

Lecture 03-Introducing List

The document provides an overview of Python lists, explaining their definition, structure, and how to access and modify elements within them. It emphasizes that lists can store various types of information and that Python uses zero-based indexing for accessing elements. Additionally, it illustrates examples of creating, accessing, and modifying lists in Python.

Uploaded by

Tausif Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 03-Introducing List

The document provides an overview of Python lists, explaining their definition, structure, and how to access and modify elements within them. It emphasizes that lists can store various types of information and that Python uses zero-based indexing for accessing elements. Additionally, it illustrates examples of creating, accessing, and modifying lists in Python.

Uploaded by

Tausif Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DS 1501: Programming for Data Science

Python List

Dr. Md Saddam Hossain Mukta


Associate Professor & Undergraduate Program
Coordinator
Department of CSE, UIU
Email: saddam@cse.uiu.ac.bd
What is List?
● Lists allow you to store sets of information in one place, whether you have just a few items or
millions of items.
● Lists are one of Python’s most powerful features readily accessible to new programmers
● A list is a collection of items in a particular order. You can make a list that includes the letters of
the alphabet, the digits from 0–9, or the names of all the people in your family.
● You can put anything you want into a list, and the items in your list don’t have to be related in
any particular way.
● Because a list usually contains more than one element, it’s a good idea to make the name of
your list plural, such as letters, digits, or names.
What is List?
● In Python, square brackets ([]) indicate a list, and individual elements in the list are separated
by commas.
● Here’s a simple example of a list that contains a few kinds of bicycles:

bicycles = ['trek', 'cannondale', 'redline', 'specialized']

print(bicycles)

● Output:

['trek', 'cannondale', 'redline', 'specialized']


Accessing elements
● However, you will not want to see the list as a list only
● Lists are ordered collections, so you can access any element in a list by telling Python the
position, or index, of the item desired.
● To access an element in a list, write the name of the list followed by the index of the item
enclosed in square brackets.
● For example, let’s pull out the first bicycle in the list bicycles:

bicycles = ['trek', 'cannondale', 'redline', 'specialized']

print(bicycles[0])

● Output: Trek
● Python returns just that element without square brackets or quotation marks.
Accessing elements (Index position 0, not 1)
● Python considers the first item in a list to be at position 0, not position 1. The second item in a
list has an index of 1. You can get any element you want from a list by subtracting one from its
position in the list.
● For instance, to access the fourth item in a list, you request the item at index 3.
● The following asks for the bicycles at index 1 and index 3:

bicycles = ['trek', 'cannondale', 'redline', 'specialized']


print(bicycles[1])
print(bicycles[3])

● This code returns the second and fourth bicycles in the list:

cannondale
specialized
Accessing elements (Index position 0, not 1)
● Python has a special syntax for accessing the last element in a list.
● By asking for the item at index -1, Python always returns the last item in the list:

bicycles = ['trek', 'cannondale', 'redline', 'specialized']

print(bicycles[-1])

● This code returns the value 'specialized'. This syntax is quite useful, because you’ll often want
to access the last items in a list without knowing exactly how long the list is.
● This convention extends to other negative index values as well. The index -2 returns the second
item from the end of the list, the index -3 returns the third item from the end, and so forth.
Using Individual Values from a List
● You can use individual values from a list just as you would any other variable.
● For example, you can use concatenation to create a message based on a value from a list.
● Let’s try pulling the first bicycle from the list and composing a message using that value.

bicycles = ['trek', 'cannondale', 'redline', 'specialized']


message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)

● Output:

My first bicycle was a Trek.


Modifying Elements in a List
● Modifying an element is similar to the syntax for accessing an element in a list.
● To change an element, use the name of the list followed by the index of the element you want
to change, then provide the new value you want that item to have.

motorcycles = ['honda', 'yamaha', 'suzuki']


print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
● The code defines the original list, with 'honda' as the first element. The code at changes the
value of the first item to 'ducati'.
● Output:

['honda', 'yamaha', 'suzuki']

['ducati', 'yamaha', 'suzuki']


“Complex is better than complicated.”

You might also like