SlideShare a Scribd company logo
1 of 26
Python
List, Tuple, Set, Dictionaries
LIST
A list is a collection of different kinds of values or items. Since
Python lists are mutable, we can change their elements after
forming.
The comma (,) and the square brackets [enclose the List's items]
serve as separators.
LIST.py
LIST
List items are ordered, changeable, and allow duplicate values.
When we say that lists are ordered, it means that the items have
a defined order, and that order will not change (Some List
method can change too)
lists can have items with the same value:
List Indexing and Splitting
Negative Indexing in List
The list() Constructor
IT IS ALSO POSSIBLE TO USE
THE LIST() CONSTRUCTOR
WHEN CREATING A NEW LIST
LISTCONSTRUCTOR.PY.
Python - Change List Items(Update)
To change the value of a specific item, refer to the index number:
To change the value of items within a specific range, define a list with
the new values, and refer to the range of index numbers where you
want to insert the new values:
LISTUPDATE.py
Key point about List
The length of the list will change when the number of items inserted does
not match the number of items replaced.
If you insert less items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly:
LISTUPDATE2.py
Append Items
To add an item to the end of the list, use
the append() method:
INSERLIST.py
Extend List
To append elements
from another list to
the current list, use
the extend() method.
LISTEXTEND
Add Any Iterable
The extend() method does not have to append lists, you can add any iterable object
(tuples, sets, dictionaries etc.).
list1 = ["apple", "grapes", "cherry"]
tuple1 = ("kiwi", "orange")
list1.extend(tuple1)
print(list1)
Remove() del() and clear()
The remove() method
removes the specified item.
If there are more than one
item with the specified value,
the remove() method removes
the first occurance:
The pop() method removes
the specified index.
If we do not specify the index,
the pop() method removes the
last item.
The del keyword also
removes the specified index
The clear() method empties
the list.
The list still remains, but it has
no content
LISTREMOVE
Python - Loop Lists
WE can loop through the list items by using a for loop:
WEcan also loop through the list items by referring to their index number.
Use the range() and len() functions to create a suitable iterable.
LOOPSINLIST.py
Sort List Alphanumerically
List objects have a sort() method that will sort
the list alphanumerically, ascending, by default:
SORTLIST
Python - Loop Lists
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the
other 3 are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
Tuple
Ordered
When we say that tuples are ordered, it means that the items have a defined order, and that
order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple
has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value:
Python Tuple
A comma-separated group of items is called a Python
triple. The ordering, settled items, and reiterations of a
tuple are to some degree like those of a rundown, but in
contrast to a rundown, a tuple is unchanging.
The main difference between the two is that we cannot
alter the components of a tuple once they have been
assigned. On the other hand, we can edit the contents of
a list.
1TUPLE.py
Create tuple with one item
• thistuple = ("apple",)​
• print(type(thistuple))​
• #NOT a tuple​
• thistuple = ("apple")​
• print(type(thistuple))​
• Output​
• <class 'tuple'>​
<class 'str'>​
• When creating a tuple with only one item, remember to include a comma after the
item, otherwise it will not be identified as a tuple.
Python - Update Tuples
Once a tuple is created,
you cannot change its
values. Tuples
are unchangeable,
or immutable as it also
is called.
But there is a
workaround. You can
convert the tuple into a
list, change the list, and
convert the list back
into a tuple.
Tuples operations
1. Convert into a
list: Just like the
workaround
for changing a
tuple, you can
convert it into a list,
add your item(s),
and convert it back
into a tuple.
2. Add tuple to a
tuple. You are
allowed to add
tuples to tuples, so
if you want to add
one item, (or
many), create a
new tuple with the
item(s), and add it
to the existing
tuple:
Tuples
are unchangeable,
so you cannot
remove items from
it, but you can use
the same
workaround as we
used for changing
and adding tuple
items:
2TupleUpdate.py
Python - Unpack Tuples
WHEN WE CREATE A
TUPLE, WE NORMALLY
ASSIGN VALUES TO IT.
THIS IS CALLED
"PACKING" A TUPLE:
THE NUMBER OF
VARIABLES MUST MATCH
THE NUMBER OF VALUES
IN THE TUPLE, IF NOT,
YOU MUST USE AN
ASTERISK TO COLLECT
THE REMAINING VALUES
AS A LIST.
3TUPLE.PY
Using Asterisk*
If the number of variables
is less than the number of
values, you can
add an * to the variable
name and the values will
be assigned to the
variable as a list:
If the asterisk is added to
another variable name
than the last, Python will
assign values to the
variable until the number
of values left matches the
number of variables left
4Tuple.py
Join and multiply Tuples
TO JOIN TWO OR
MORE TUPLES YOU
CAN USE
THE + OPERATOR:
01
IF YOU WANT TO
MULTIPLY THE
CONTENT OF A TUPLE
A GIVEN NUMBER OF
TIMES, YOU CAN USE
THE * OPERATOR:
02
5TUPLE
03
Set
1
Set items are
unordered,
unchangeable, and do
not allow duplicate
values.
2
Unordered items in a
set do not have a
defined order.
3
Set items can appear in
a different order every
time you use them,
and cannot be referred
to by index or key.
4
Unchangeablemeaning
that we cannot change
the items
5
Once a set is
created, you can
remove items and add
new items.
Duplicates Not
Allowed
Access Items in Set
You cannot access items in a set by referring to an index or a
key.
But you can loop through the set items using a for loop, or ask
if a specified value is present in a set, by using the in keyword.
1SET.py
Add() update() remove() Discard() in set
Once a set is created, you cannot change its items, but you can add new items.
To add items from another set into the current set, use the update() method.
To remove an item in a set, use the remove()
discard() method. Discard the item
2SET.py

More Related Content

Similar to PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx

11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 

Similar to PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx (20)

Python tuple
Python   tuplePython   tuple
Python tuple
 
Lecture 09.pptx
Lecture 09.pptxLecture 09.pptx
Lecture 09.pptx
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
List data structure
List data structure List data structure
List data structure
 
Introduction of Python-1.pdf
Introduction of Python-1.pdfIntroduction of Python-1.pdf
Introduction of Python-1.pdf
 
PPT data science python sequence numpy.pptx
PPT data science python sequence numpy.pptxPPT data science python sequence numpy.pptx
PPT data science python sequence numpy.pptx
 
Python data handling notes
Python data handling notesPython data handling notes
Python data handling notes
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Tuples.pptx
Tuples.pptxTuples.pptx
Tuples.pptx
 
Python list
Python listPython list
Python list
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
Python list functions
Python list functionsPython list functions
Python list functions
 
Tuple Operation and Tuple Assignment in Python.pdf
Tuple Operation and Tuple Assignment in Python.pdfTuple Operation and Tuple Assignment in Python.pdf
Tuple Operation and Tuple Assignment in Python.pdf
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
set.pptx
set.pptxset.pptx
set.pptx
 
Tuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptxTuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptx
 
beginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet descriptionbeginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet description
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 

PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx

  • 2. LIST A list is a collection of different kinds of values or items. Since Python lists are mutable, we can change their elements after forming. The comma (,) and the square brackets [enclose the List's items] serve as separators. LIST.py
  • 3. LIST List items are ordered, changeable, and allow duplicate values. When we say that lists are ordered, it means that the items have a defined order, and that order will not change (Some List method can change too) lists can have items with the same value:
  • 4. List Indexing and Splitting
  • 6. The list() Constructor IT IS ALSO POSSIBLE TO USE THE LIST() CONSTRUCTOR WHEN CREATING A NEW LIST LISTCONSTRUCTOR.PY.
  • 7. Python - Change List Items(Update) To change the value of a specific item, refer to the index number: To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values: LISTUPDATE.py
  • 8. Key point about List The length of the list will change when the number of items inserted does not match the number of items replaced. If you insert less items than you replace, the new items will be inserted where you specified, and the remaining items will move accordingly: LISTUPDATE2.py
  • 9. Append Items To add an item to the end of the list, use the append() method: INSERLIST.py
  • 10. Extend List To append elements from another list to the current list, use the extend() method. LISTEXTEND
  • 11. Add Any Iterable The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.). list1 = ["apple", "grapes", "cherry"] tuple1 = ("kiwi", "orange") list1.extend(tuple1) print(list1)
  • 12. Remove() del() and clear() The remove() method removes the specified item. If there are more than one item with the specified value, the remove() method removes the first occurance: The pop() method removes the specified index. If we do not specify the index, the pop() method removes the last item. The del keyword also removes the specified index The clear() method empties the list. The list still remains, but it has no content LISTREMOVE
  • 13. Python - Loop Lists WE can loop through the list items by using a for loop: WEcan also loop through the list items by referring to their index number. Use the range() and len() functions to create a suitable iterable. LOOPSINLIST.py
  • 14. Sort List Alphanumerically List objects have a sort() method that will sort the list alphanumerically, ascending, by default: SORTLIST
  • 15. Python - Loop Lists Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.
  • 16. Tuple Ordered When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. Unchangeable Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. Allow Duplicates Since tuples are indexed, they can have items with the same value:
  • 17. Python Tuple A comma-separated group of items is called a Python triple. The ordering, settled items, and reiterations of a tuple are to some degree like those of a rundown, but in contrast to a rundown, a tuple is unchanging. The main difference between the two is that we cannot alter the components of a tuple once they have been assigned. On the other hand, we can edit the contents of a list. 1TUPLE.py
  • 18. Create tuple with one item • thistuple = ("apple",)​ • print(type(thistuple))​ • #NOT a tuple​ • thistuple = ("apple")​ • print(type(thistuple))​ • Output​ • <class 'tuple'>​ <class 'str'>​ • When creating a tuple with only one item, remember to include a comma after the item, otherwise it will not be identified as a tuple.
  • 19. Python - Update Tuples Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
  • 20. Tuples operations 1. Convert into a list: Just like the workaround for changing a tuple, you can convert it into a list, add your item(s), and convert it back into a tuple. 2. Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to add one item, (or many), create a new tuple with the item(s), and add it to the existing tuple: Tuples are unchangeable, so you cannot remove items from it, but you can use the same workaround as we used for changing and adding tuple items: 2TupleUpdate.py
  • 21. Python - Unpack Tuples WHEN WE CREATE A TUPLE, WE NORMALLY ASSIGN VALUES TO IT. THIS IS CALLED "PACKING" A TUPLE: THE NUMBER OF VARIABLES MUST MATCH THE NUMBER OF VALUES IN THE TUPLE, IF NOT, YOU MUST USE AN ASTERISK TO COLLECT THE REMAINING VALUES AS A LIST. 3TUPLE.PY
  • 22. Using Asterisk* If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list: If the asterisk is added to another variable name than the last, Python will assign values to the variable until the number of values left matches the number of variables left 4Tuple.py
  • 23. Join and multiply Tuples TO JOIN TWO OR MORE TUPLES YOU CAN USE THE + OPERATOR: 01 IF YOU WANT TO MULTIPLY THE CONTENT OF A TUPLE A GIVEN NUMBER OF TIMES, YOU CAN USE THE * OPERATOR: 02 5TUPLE 03
  • 24. Set 1 Set items are unordered, unchangeable, and do not allow duplicate values. 2 Unordered items in a set do not have a defined order. 3 Set items can appear in a different order every time you use them, and cannot be referred to by index or key. 4 Unchangeablemeaning that we cannot change the items 5 Once a set is created, you can remove items and add new items. Duplicates Not Allowed
  • 25. Access Items in Set You cannot access items in a set by referring to an index or a key. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword. 1SET.py
  • 26. Add() update() remove() Discard() in set Once a set is created, you cannot change its items, but you can add new items. To add items from another set into the current set, use the update() method. To remove an item in a set, use the remove() discard() method. Discard the item 2SET.py