SlideShare a Scribd company logo
1 of 12
Module 2
Data Collections – Lists, Tuples,
and Dictionaries
Lists - collections of data
Module 2 Lists - collections of data
Why do we need lists?
var1 = int(input())
var2 = int(input())
var3 = int(input())
var4 = int(input())
var5 = int(input())
var6 = int(input())
:
:
declare such
multi-value variables
numbers = [10, 5, 7, 2, 1]
List is a collection of elements, but each element is a scalar;
the elements in a list are always numbered starting from zero.
Module 2 Lists - collections of data
Indexing lists
numbers = [10, 5, 7, 2, 1]
print("Original list content:", numbers) # Printing original list content.
numbers[0] = 111
print("nPrevious list content:", numbers) # Printing previous list content.
numbers[1] = numbers[4] # Copying value of the fifth element to the second.
print("New list content:", numbers) # Printing current list content.
Original list content: [10, 5, 7, 2, 1]
Previous list content: [111, 5, 7, 2, 1]
New list content: [111, 1, 7, 2, 1]
Module 2 Lists - collections of data
Accessing list content
numbers = [10, 5, 7, 2, 1]
# Accessing the list's first element.
print(numbers[0])
numbers = [10, 5, 7, 2, 1]
# Printing the list's length.
print("List length:", len(numbers))
Each of the list's elements may be accessed separately.
List length: 5
Module 2 Lists - collections of data
Removing elements from a list
numbers = [10, 5, 7, 2, 1]
print("Original list content:", numbers) # Printing original list content.
numbers[0] = 111
print("nPrevious list content:", numbers) # Printing previous list content.
numbers[1] = numbers[4] # Copying value of the fifth element to the second.
print("Previous list content:", numbers) # Printing previous list content.
print("nList's length:", len(numbers)) # Printing previous list length.
###
del numbers[1] # Removing the second element from the list.
print("New list's length:", len(numbers)) # Printing new list length.
print("nNew list content:", numbers) # Printing current list content.
###
del: Any of the list's elements may be removed
Module 2 Lists - collections of data
Negative indices are legal
numbers = [111, 7, 2, 1]
print(numbers[-1])
print(numbers[-2])
1
2
an index -1 -> last one element in the list
Module 2 Lists - collections of data
Functions vs. methods
Function method
A method is a specific kind
of function;
Invocvation:
result =
function(arg)
Doesn't belong
to any data
Is owned by the
whole code
Function
Invocation:
result =
data.method(arg)
Is able to change
the state of a
selected entity
Is owned by the
data it works for
Method
Module 2 Lists - collections of data
append(), insert()
numbers = [111, 7, 2, 1]
print(len(numbers))
print(numbers)
numbers.append(4)
print(len(numbers))
print(numbers)
numbers.insert(0, 222)
print(len(numbers))
print(numbers)
my_list = [] # Creating an empty list.
for i in range(5):
my_list.append(i + 1)
print(my_list)
• new element
to the end of
the existing list
list.append(value)
• add a new
element at any
place
list.insert(location,
value)
Module 2 Lists - collections of data
Making use of lists
my_list = [10, 1, 8, 3, 5]
total = 0
for i in range(len(my_list)):
total += my_list[i]
print(total)
my_list = [10, 1, 8, 3, 5]
total = 0
for i in my_list:
total += i
print(total)
27 27
Module 2 Lists - collections of data
Lists in action
variable_1 = 1
variable_2 = 2
variable_2 = variable_1
variable_1 = variable_2
1 1
variable_1 = 1
variable_2 = 2
auxiliary = variable_1
variable_1 = variable_2
variable_2 = auxiliary
2 1
my_list = [10, 1, 8, 3, 5]
my_list[0], my_list[4] = my_list[4], my_list[0]
my_list[1], my_list[3] = my_list[3], my_list[1]
print(my_list)
[5, 3, 8, 1, 10]
my_list = [10, 1, 8, 3, 5]
length = len(my_list)
for i in range(length // 2):
my_list[i], my_list[length - i - 1] = my_list[length - i - 1], my_list[i]
[5, 3, 8, 1, 10]
Module 2 Lists - collections of data
Key takeaways
The list is a type of
data used to store
multiple objects.
Lists can be indexed
and updated.
List elements and lists
can be deleted.
Lists can be iterated
through using the
for loop.
The len() function: to
check the list's length Function, Method
Module 2 Lists - collections of data
21. The basics of lists
22. The basics of lists - the Beatles
LAB Practice

More Related Content

What's hot

What's hot (20)

Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
Python list concept
Python list conceptPython list concept
Python list concept
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Lists
ListsLists
Lists
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python set
Python setPython set
Python set
 
Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
Joc live session
Joc live sessionJoc live session
Joc live session
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 

Similar to Python PCEP Lists Collections of Data

Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 

Similar to Python PCEP Lists Collections of Data (20)

Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
lists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Pythonlists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Python
 
Python lists & sets
Python lists & setsPython lists & sets
Python lists & sets
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
 
Introduction To Programming with Python-4
Introduction To Programming with Python-4Introduction To Programming with Python-4
Introduction To Programming with Python-4
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
 
Python PRACTICAL NO 6 for your Assignment.pptx
Python PRACTICAL NO 6 for your Assignment.pptxPython PRACTICAL NO 6 for your Assignment.pptx
Python PRACTICAL NO 6 for your Assignment.pptx
 
Python List
Python ListPython List
Python List
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210
 
Groovy
GroovyGroovy
Groovy
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
 
List in Python
List in PythonList in Python
List in Python
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptx
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
 

More from IHTMINSTITUTE

More from IHTMINSTITUTE (18)

Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
 
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
 
Python PCEP Creating Simple Functions
Python PCEP Creating Simple FunctionsPython PCEP Creating Simple Functions
Python PCEP Creating Simple Functions
 
Python PCEP Functions And Scopes
Python PCEP Functions And ScopesPython PCEP Functions And Scopes
Python PCEP Functions And Scopes
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function Parameters
 
Python PCEP Functions
Python PCEP FunctionsPython PCEP Functions
Python PCEP Functions
 
Python PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysPython PCEP Multidemensional Arrays
Python PCEP Multidemensional Arrays
 
Python PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsPython PCEP Sorting Simple Lists
Python PCEP Sorting Simple Lists
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit Operations
 
Python PCEP Loops
Python PCEP LoopsPython PCEP Loops
Python PCEP Loops
 
Python PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionPython PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional Execution
 
Python PCEP How To Talk To Computer
Python PCEP How To Talk To ComputerPython PCEP How To Talk To Computer
Python PCEP How To Talk To Computer
 
Python PCEP Variables
Python PCEP VariablesPython PCEP Variables
Python PCEP Variables
 
Python PCEP Operators
Python PCEP OperatorsPython PCEP Operators
Python PCEP Operators
 
Python PCEP Literals
Python PCEP LiteralsPython PCEP Literals
Python PCEP Literals
 
IHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTM Python PCEP Hello World
IHTM Python PCEP Hello World
 
IHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to Python
 
Python PCEP Welcome Opening
Python PCEP Welcome OpeningPython PCEP Welcome Opening
Python PCEP Welcome Opening
 

Recently uploaded

一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
c6eb683559b3
 
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
AS
 
一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理
SS
 
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
AS
 
一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理
F
 
一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样
一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样
一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样
ayvbos
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
ayvbos
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理
AS
 
一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理
A
 
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
mikehavy0
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
F
 

Recently uploaded (20)

一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
 
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
 
APNIC Updates presented by Paul Wilson at CaribNOG 27
APNIC Updates presented by Paul Wilson at  CaribNOG 27APNIC Updates presented by Paul Wilson at  CaribNOG 27
APNIC Updates presented by Paul Wilson at CaribNOG 27
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
 
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
 
一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理
 
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样
一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样
一比一原版(USYD毕业证书)悉尼大学毕业证原件一模一样
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理
 
一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理
 
Washington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers ShirtWashington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers Shirt
 
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
 

Python PCEP Lists Collections of Data

  • 1. Module 2 Data Collections – Lists, Tuples, and Dictionaries Lists - collections of data
  • 2. Module 2 Lists - collections of data Why do we need lists? var1 = int(input()) var2 = int(input()) var3 = int(input()) var4 = int(input()) var5 = int(input()) var6 = int(input()) : : declare such multi-value variables numbers = [10, 5, 7, 2, 1] List is a collection of elements, but each element is a scalar; the elements in a list are always numbered starting from zero.
  • 3. Module 2 Lists - collections of data Indexing lists numbers = [10, 5, 7, 2, 1] print("Original list content:", numbers) # Printing original list content. numbers[0] = 111 print("nPrevious list content:", numbers) # Printing previous list content. numbers[1] = numbers[4] # Copying value of the fifth element to the second. print("New list content:", numbers) # Printing current list content. Original list content: [10, 5, 7, 2, 1] Previous list content: [111, 5, 7, 2, 1] New list content: [111, 1, 7, 2, 1]
  • 4. Module 2 Lists - collections of data Accessing list content numbers = [10, 5, 7, 2, 1] # Accessing the list's first element. print(numbers[0]) numbers = [10, 5, 7, 2, 1] # Printing the list's length. print("List length:", len(numbers)) Each of the list's elements may be accessed separately. List length: 5
  • 5. Module 2 Lists - collections of data Removing elements from a list numbers = [10, 5, 7, 2, 1] print("Original list content:", numbers) # Printing original list content. numbers[0] = 111 print("nPrevious list content:", numbers) # Printing previous list content. numbers[1] = numbers[4] # Copying value of the fifth element to the second. print("Previous list content:", numbers) # Printing previous list content. print("nList's length:", len(numbers)) # Printing previous list length. ### del numbers[1] # Removing the second element from the list. print("New list's length:", len(numbers)) # Printing new list length. print("nNew list content:", numbers) # Printing current list content. ### del: Any of the list's elements may be removed
  • 6. Module 2 Lists - collections of data Negative indices are legal numbers = [111, 7, 2, 1] print(numbers[-1]) print(numbers[-2]) 1 2 an index -1 -> last one element in the list
  • 7. Module 2 Lists - collections of data Functions vs. methods Function method A method is a specific kind of function; Invocvation: result = function(arg) Doesn't belong to any data Is owned by the whole code Function Invocation: result = data.method(arg) Is able to change the state of a selected entity Is owned by the data it works for Method
  • 8. Module 2 Lists - collections of data append(), insert() numbers = [111, 7, 2, 1] print(len(numbers)) print(numbers) numbers.append(4) print(len(numbers)) print(numbers) numbers.insert(0, 222) print(len(numbers)) print(numbers) my_list = [] # Creating an empty list. for i in range(5): my_list.append(i + 1) print(my_list) • new element to the end of the existing list list.append(value) • add a new element at any place list.insert(location, value)
  • 9. Module 2 Lists - collections of data Making use of lists my_list = [10, 1, 8, 3, 5] total = 0 for i in range(len(my_list)): total += my_list[i] print(total) my_list = [10, 1, 8, 3, 5] total = 0 for i in my_list: total += i print(total) 27 27
  • 10. Module 2 Lists - collections of data Lists in action variable_1 = 1 variable_2 = 2 variable_2 = variable_1 variable_1 = variable_2 1 1 variable_1 = 1 variable_2 = 2 auxiliary = variable_1 variable_1 = variable_2 variable_2 = auxiliary 2 1 my_list = [10, 1, 8, 3, 5] my_list[0], my_list[4] = my_list[4], my_list[0] my_list[1], my_list[3] = my_list[3], my_list[1] print(my_list) [5, 3, 8, 1, 10] my_list = [10, 1, 8, 3, 5] length = len(my_list) for i in range(length // 2): my_list[i], my_list[length - i - 1] = my_list[length - i - 1], my_list[i] [5, 3, 8, 1, 10]
  • 11. Module 2 Lists - collections of data Key takeaways The list is a type of data used to store multiple objects. Lists can be indexed and updated. List elements and lists can be deleted. Lists can be iterated through using the for loop. The len() function: to check the list's length Function, Method
  • 12. Module 2 Lists - collections of data 21. The basics of lists 22. The basics of lists - the Beatles LAB Practice