SlideShare a Scribd company logo
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

PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
List in Python
PPTX
Python list
PPTX
LIST IN PYTHON
PDF
Python List Comprehensions
PPTX
Unit 4 python -list methods
PDF
Python list
Python Variable Types, List, Tuple, Dictionary
List in Python
Python list
LIST IN PYTHON
Python List Comprehensions
Unit 4 python -list methods
Python list

What's hot (20)

PDF
Data type list_methods_in_python
PPTX
Python list concept
PDF
List , tuples, dictionaries and regular expressions in python
PPTX
Python programming -Tuple and Set Data type
PDF
Set methods in python
PDF
Arrays in python
PDF
Python set
PPTX
Dictionaries and Sets in Python
PDF
Arrays in python
PDF
List,tuple,dictionary
PPTX
Python Programming Essentials - M12 - Lists
PDF
Python programming : List and tuples
PDF
Python Workshop Part 2. LUG Maniapl
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Python programming Part -6
PDF
Joc live session
PPTX
Chapter 15 Lists
PPTX
Basic data structures in python
PDF
Basic data structures in python
Data type list_methods_in_python
Python list concept
List , tuples, dictionaries and regular expressions in python
Python programming -Tuple and Set Data type
Set methods in python
Arrays in python
Python set
Dictionaries and Sets in Python
Arrays in python
List,tuple,dictionary
Python Programming Essentials - M12 - Lists
Python programming : List and tuples
Python Workshop Part 2. LUG Maniapl
Arrays In Python | Python Array Operations | Edureka
Python programming Part -6
Joc live session
Chapter 15 Lists
Basic data structures in python
Basic data structures in python
Ad

Similar to Python PCEP Lists Collections of Data (20)

PPTX
institute of techonolgy and education tr
PPTX
Python-List.pptx
PPTX
lists8.pptx of chapter 12 IP Basic Python
PPTX
Python _dataStructures_ List, Tuples, its functions
PPTX
listppt.pptx h4wtgesvzdfvgsrbyhrrtgsvefcdef
PDF
Python lists & sets
PPTX
Module-2.pptx
PPTX
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
PDF
Python Unit 5 Questions n Notes.pdf
PDF
The Ring programming language version 1.6 book - Part 24 of 189
PPTX
Introduction To Programming with Python-4
PPTX
Chapter 3-Data structure in python programming.pptx
PDF
GE3151_PSPP_UNIT_4_Notes
PPTX
Python PRACTICAL NO 6 for your Assignment.pptx
PPTX
Brief Explanation On List and Dictionaries of Python
PPTX
list in python and traversal of list.pptx
PPTX
Python Array Power Point Presentation.pptx
PPTX
Python for the data science most in cse.pptx
PDF
Python List
PDF
The Ring programming language version 1.8 book - Part 27 of 202
institute of techonolgy and education tr
Python-List.pptx
lists8.pptx of chapter 12 IP Basic Python
Python _dataStructures_ List, Tuples, its functions
listppt.pptx h4wtgesvzdfvgsrbyhrrtgsvefcdef
Python lists & sets
Module-2.pptx
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
Python Unit 5 Questions n Notes.pdf
The Ring programming language version 1.6 book - Part 24 of 189
Introduction To Programming with Python-4
Chapter 3-Data structure in python programming.pptx
GE3151_PSPP_UNIT_4_Notes
Python PRACTICAL NO 6 for your Assignment.pptx
Brief Explanation On List and Dictionaries of Python
list in python and traversal of list.pptx
Python Array Power Point Presentation.pptx
Python for the data science most in cse.pptx
Python List
The Ring programming language version 1.8 book - Part 27 of 202
Ad

More from IHTMINSTITUTE (19)

PPTX
Python PCEP Tuples and Dictionaries
PPTX
Python PCEP Tuples and Dictionaries
PPTX
Python PCEP Creating Simple Functions
PPTX
Python PCEP Functions And Scopes
PPTX
Python PCEP Function Parameters
PPTX
Python PCEP Functions
PPTX
Python PCEP Multidemensional Arrays
PPTX
Python PCEP Operations On Lists
PPTX
Python PCEP Sorting Simple Lists
PPTX
Python PCEP Logic Bit Operations
PPTX
Python PCEP Loops
PPTX
Python PCEP Comparison Operators And Conditional Execution
PPTX
Python PCEP How To Talk To Computer
PPTX
Python PCEP Variables
PPTX
Python PCEP Operators
PPTX
Python PCEP Literals
PPTX
IHTM Python PCEP Hello World
PPTX
IHTM Python PCEP Introduction to Python
PPTX
Python PCEP Welcome Opening
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
Python PCEP Creating Simple Functions
Python PCEP Functions And Scopes
Python PCEP Function Parameters
Python PCEP Functions
Python PCEP Multidemensional Arrays
Python PCEP Operations On Lists
Python PCEP Sorting Simple Lists
Python PCEP Logic Bit Operations
Python PCEP Loops
Python PCEP Comparison Operators And Conditional Execution
Python PCEP How To Talk To Computer
Python PCEP Variables
Python PCEP Operators
Python PCEP Literals
IHTM Python PCEP Hello World
IHTM Python PCEP Introduction to Python
Python PCEP Welcome Opening

Recently uploaded (20)

PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
Internet___Basics___Styled_ presentation
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PPTX
SAP Ariba Sourcing PPT for learning material
PPTX
artificial intelligence overview of it and more
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
Digital Literacy And Online Safety on internet
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
E -tech empowerment technologies PowerPoint
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
innovation process that make everything different.pptx
PPTX
Introduction to Information and Communication Technology
DOCX
Unit-3 cyber security network security of internet system
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
Internet___Basics___Styled_ presentation
Introuction about ICD -10 and ICD-11 PPT.pptx
Power Point - Lesson 3_2.pptx grad school presentation
SAP Ariba Sourcing PPT for learning material
artificial intelligence overview of it and more
Unit-1 introduction to cyber security discuss about how to secure a system
An introduction to the IFRS (ISSB) Stndards.pdf
The New Creative Director: How AI Tools for Social Media Content Creation Are...
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
The Internet -By the Numbers, Sri Lanka Edition
Digital Literacy And Online Safety on internet
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
E -tech empowerment technologies PowerPoint
Cloud-Scale Log Monitoring _ Datadog.pdf
innovation process that make everything different.pptx
Introduction to Information and Communication Technology
Unit-3 cyber security network security of internet system
PptxGenJS_Demo_Chart_20250317130215833.pptx
introduction about ICD -10 & ICD-11 ppt.pptx

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