0% found this document useful (0 votes)
1 views16 pages

3 Lists

Chapter 1 introduces the basics of Python lists, explaining their definition, how to access elements, and methods for changing, adding, and removing items. It emphasizes the importance of lists in programming and provides practical exercises for better understanding. The chapter also covers organizing lists and avoiding index errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views16 pages

3 Lists

Chapter 1 introduces the basics of Python lists, explaining their definition, how to access elements, and methods for changing, adding, and removing items. It emphasizes the importance of lists in programming and provides practical exercises for better understanding. The chapter also covers organizing lists and avoiding index errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 1: Python basic

Lecturer: Nguyen Tuan Long, Phd


Email: [email protected]
Mobile: 0982 746 235
CONTENTS 2

1.3. Lists
• What Is a List?
• Changing, Adding, and Removing Elements
• Organizing a List
• Avoiding index errors when working with lists
What Is a List? 3
• A list stores a series of items in a particular order.
• 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, and they tie together many important concepts in
programming.
Defining a list
Use square brackets ([]) to define a list, and use commas to separate
individual items in the list.
• Use plural names for lists, to make your code easier to read.
What Is a List? 4

Accessing elements
• Individual elements in a list are accessed according to their position, called
the index.
• To get a particular element, write the list's name and then the element's
index in square brackets.
Start End
Index

Negative index
Practice 5

3-1,...,3-3
Changing, Adding, and Removing Elements 6

 Changing
Changing, Adding, and Removing Elements 7

 Adding
Changing, Adding, and Removing Elements 8
Changing, Adding, and Removing Elements 9

 Removing
Changing, Adding, and Removing Elements 10

 Removing
Practice 11

3-4,…, 3.7
Organizing a List 12

The sort() method : Sorts the list in alphabetical order


Organizing a List 13

• Notice that reverse()


doesn’t sort backward
alphabetically; it simply
reverses the order of the
list.

• len(cars): Return the


number of items in cars
Practice 14

3-8,…, 3.10
Avoiding Index Errors When Working with Lists 15
Practice 16

3-11

You might also like