SlideShare a Scribd company logo
1 of 16
1
Presentation on Lists,
Tuples and Dictionaries
Outline
1. Lists
2. Functions and Methods for Lists
3. Tuples
4. Functions and Methods for Tuples
5. Dictionaries
6. Functions and Methods for Dictionaries
2
Lists
● A list is an object that holds a collection of objects; it represents a sequence of data. In that
sense, a list is similar to a string, except a string can hold only characters.
● A list can hold any Python object. A list need not be homogeneous; that is, the elements of a
list do not all have to be of the same type.
● Creating a simple list L: L = [1,2,3]
● Use square brackets to indicate the start and end of the list, and separate the items by
commas.
● The empty list is []. It is the list equivalent of 0 or ''.
3
Lists
● If you have a long list to enter, you can split it across several lines, like below: nums = [0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
● We can use eval(input()) to allow the user to enter a list.
● You can use the print function to print the entire contents of a list.
4
L = eval(input('Enter a list: '))
print('The list is', L)
Enter a list: [5,7,9]
The list is [5,7,9]
Lists
● Lists can contain all kinds of things, even other lists. For example, the following is a valid list:
L= [1, 2.718, 'abc', [5,6,7]]
5
L = [1, 2.718, 'abc', [5,6,7]]
print (L)
[1, 2.718, 'abc', [5,6,7]]
Lists
● There are a number of things which work the same way for lists as for string.
○ len — The number of items in L is given by len(L).
○ Indexing and slicing — These work exactly as with strings. For example, L[0] is the first item
of the list L and L[:3] gives the first three items.
○ index and count — These methods work the same as they do for strings.
○ + and * — The + operator adds one list to the end of another. The * operator repeats a list.
6
Functions and Methods for Lists
● There are several built-in functions that operate on lists. Here are some useful ones:
● For example, the following computes the average of the values in a list L:
● average = sum(L)/len(L)
7
Functions and Methods for Lists
● Here are some list methods:
8
Functions and Methods for Lists
● Important note There is a big difference between list methods and string methods: String
methods do not change the original string, but list methods do change the original list.
● To sort a list L, just use L.sort() and not L=L.sort(). In fact, the latter will not work at all.
9
Tuples
● A tuple is essentially an immutable list. Below is a list with three elements
and a tuple with three elements:
● Tuples are enclosed in parentheses, though the parentheses are actually
optional.
● Indexing and slicing work the same as with lists.
● As with lists, you can get the length of the tuple by using the len function,
and, like lists, tuples have count and index methods.
● However, since a tuple is immutable, it does not have any of the other
methods that lists have, like sort or reverse, as those change the list.
10
L = [1,2,3]
t = (1,2,3)
Tuples
● One reason why there are both lists and tuples is that in situations where
speed really matters, tuples are generally faster than lists.
● The flexibility of lists comes with a corresponding cost in speed.
● To convert an object into a tuple, use tuple.
● The following example converts a list and a string into tuples:
11
t1 = tuple([1,2,3])
t2 = tuple('abcde')
Dictionaries
● A dictionary is a more general version of a list. Here is a list that contains the
number of days in the months of the year. If we want the the number of days
in January, use days[0]. December is days[11] or days[-1].
● Here is a dictionary of the days in the months of the year:
● To get the number of days in January, we use days['January']. One benefit of
using dictionaries here is the code is more readable, and we don’t have to
figure out which index in the list a given month is at. Dictionaries have a
number of other uses, as well. 12
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days = {'January':31, 'February':28, 'March':31, 'April':30,
'May':31, 'June':30, 'July':31, 'August':31, 'September':30,
'October':31, 'November':30, 'December':31}
Dictionaries
● To declare a dictionary we enclose it in curly braces, {}.
● Each entry consists of a pair separated by a colon.
● The first part of the pair is called the key and the second is the value.
● The key acts like an index.
● Here is a simple dictionary:
● So in the first pair, 'A':100, the key is 'A', the value is 100, and d['A'] gives 100.
● Keys are often strings, but they can be integers, floats, and many other things
as well. You can mix different types of keys in the same dictionary and
different types of values, too.
13
d = {'A':100, 'B':200}
Functions and Methods for Dictionaries
● To change d['A'] to 400: d['A']=400
● To add a new entry to the dictionary, we can just assign it, like below:
d['C']=500
● To delete an entry from a dictionary, use the del operator: del d['A']
● To copy a dictionary, use its copy method. Here is an example: d2 = d.copy()
● The empty dictionary is {}, which is the dictionary equivalent of [] for lists or ''
for strings.
14
d = {'A':100, 'B':200}
Functions and Methods for Dictionaries
● The dict function is another way to create a dictionary.
d = dict([('A',100),('B',300)])
● This creates the dictionary {'A':100,'B':300}. This way of building a
dictionary is useful if your program needs to construct a dictionary
while it is running.
15
Thank You

More Related Content

Similar to Python PPT.pptx

Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov
 
Notes3
Notes3Notes3
Notes3hccit
 
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllfPython-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllfMeha46
 
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptxPYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptxsurajnath20
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptxChandanVatsa2
 
Programming in python - Week 3
Programming in python - Week 3Programming in python - Week 3
Programming in python - Week 3Priya Nayak
 
2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptxdeivanayagamramachan
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in PythonDevashish Kumar
 
Python data type
Python data typePython data type
Python data typeJaya Kumari
 
White paper on tuples
White paper on tuplesWhite paper on tuples
White paper on tuplesArehoYoutuber
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfrajkumar2792005
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiSowmya Jyothi
 
Reverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonReverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonKal Bartal
 

Similar to Python PPT.pptx (20)

Python lists
Python listsPython lists
Python lists
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 
Notes3
Notes3Notes3
Notes3
 
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllfPython-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
Python-Ukllllllllllllllllllllllllllllnit 2.pdklllllllf
 
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptxPYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
Programming in python - Week 3
Programming in python - Week 3Programming in python - Week 3
Programming in python - Week 3
 
2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
unit 2- PPT.pdf
unit 2- PPT.pdfunit 2- PPT.pdf
unit 2- PPT.pdf
 
tupple.pptx
tupple.pptxtupple.pptx
tupple.pptx
 
Python data type
Python data typePython data type
Python data type
 
Lists
ListsLists
Lists
 
Lecture-5.pdf
Lecture-5.pdfLecture-5.pdf
Lecture-5.pdf
 
White paper on tuples
White paper on tuplesWhite paper on tuples
White paper on tuples
 
Notes8
Notes8Notes8
Notes8
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
 
pythondatatypes.pptx
pythondatatypes.pptxpythondatatypes.pptx
pythondatatypes.pptx
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
Reverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonReverse a Linked List In Place in Python
Reverse a Linked List In Place in Python
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 

Python PPT.pptx

  • 2. Outline 1. Lists 2. Functions and Methods for Lists 3. Tuples 4. Functions and Methods for Tuples 5. Dictionaries 6. Functions and Methods for Dictionaries 2
  • 3. Lists ● A list is an object that holds a collection of objects; it represents a sequence of data. In that sense, a list is similar to a string, except a string can hold only characters. ● A list can hold any Python object. A list need not be homogeneous; that is, the elements of a list do not all have to be of the same type. ● Creating a simple list L: L = [1,2,3] ● Use square brackets to indicate the start and end of the list, and separate the items by commas. ● The empty list is []. It is the list equivalent of 0 or ''. 3
  • 4. Lists ● If you have a long list to enter, you can split it across several lines, like below: nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] ● We can use eval(input()) to allow the user to enter a list. ● You can use the print function to print the entire contents of a list. 4 L = eval(input('Enter a list: ')) print('The list is', L) Enter a list: [5,7,9] The list is [5,7,9]
  • 5. Lists ● Lists can contain all kinds of things, even other lists. For example, the following is a valid list: L= [1, 2.718, 'abc', [5,6,7]] 5 L = [1, 2.718, 'abc', [5,6,7]] print (L) [1, 2.718, 'abc', [5,6,7]]
  • 6. Lists ● There are a number of things which work the same way for lists as for string. ○ len — The number of items in L is given by len(L). ○ Indexing and slicing — These work exactly as with strings. For example, L[0] is the first item of the list L and L[:3] gives the first three items. ○ index and count — These methods work the same as they do for strings. ○ + and * — The + operator adds one list to the end of another. The * operator repeats a list. 6
  • 7. Functions and Methods for Lists ● There are several built-in functions that operate on lists. Here are some useful ones: ● For example, the following computes the average of the values in a list L: ● average = sum(L)/len(L) 7
  • 8. Functions and Methods for Lists ● Here are some list methods: 8
  • 9. Functions and Methods for Lists ● Important note There is a big difference between list methods and string methods: String methods do not change the original string, but list methods do change the original list. ● To sort a list L, just use L.sort() and not L=L.sort(). In fact, the latter will not work at all. 9
  • 10. Tuples ● A tuple is essentially an immutable list. Below is a list with three elements and a tuple with three elements: ● Tuples are enclosed in parentheses, though the parentheses are actually optional. ● Indexing and slicing work the same as with lists. ● As with lists, you can get the length of the tuple by using the len function, and, like lists, tuples have count and index methods. ● However, since a tuple is immutable, it does not have any of the other methods that lists have, like sort or reverse, as those change the list. 10 L = [1,2,3] t = (1,2,3)
  • 11. Tuples ● One reason why there are both lists and tuples is that in situations where speed really matters, tuples are generally faster than lists. ● The flexibility of lists comes with a corresponding cost in speed. ● To convert an object into a tuple, use tuple. ● The following example converts a list and a string into tuples: 11 t1 = tuple([1,2,3]) t2 = tuple('abcde')
  • 12. Dictionaries ● A dictionary is a more general version of a list. Here is a list that contains the number of days in the months of the year. If we want the the number of days in January, use days[0]. December is days[11] or days[-1]. ● Here is a dictionary of the days in the months of the year: ● To get the number of days in January, we use days['January']. One benefit of using dictionaries here is the code is more readable, and we don’t have to figure out which index in the list a given month is at. Dictionaries have a number of other uses, as well. 12 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] days = {'January':31, 'February':28, 'March':31, 'April':30, 'May':31, 'June':30, 'July':31, 'August':31, 'September':30, 'October':31, 'November':30, 'December':31}
  • 13. Dictionaries ● To declare a dictionary we enclose it in curly braces, {}. ● Each entry consists of a pair separated by a colon. ● The first part of the pair is called the key and the second is the value. ● The key acts like an index. ● Here is a simple dictionary: ● So in the first pair, 'A':100, the key is 'A', the value is 100, and d['A'] gives 100. ● Keys are often strings, but they can be integers, floats, and many other things as well. You can mix different types of keys in the same dictionary and different types of values, too. 13 d = {'A':100, 'B':200}
  • 14. Functions and Methods for Dictionaries ● To change d['A'] to 400: d['A']=400 ● To add a new entry to the dictionary, we can just assign it, like below: d['C']=500 ● To delete an entry from a dictionary, use the del operator: del d['A'] ● To copy a dictionary, use its copy method. Here is an example: d2 = d.copy() ● The empty dictionary is {}, which is the dictionary equivalent of [] for lists or '' for strings. 14 d = {'A':100, 'B':200}
  • 15. Functions and Methods for Dictionaries ● The dict function is another way to create a dictionary. d = dict([('A',100),('B',300)]) ● This creates the dictionary {'A':100,'B':300}. This way of building a dictionary is useful if your program needs to construct a dictionary while it is running. 15