SlideShare a Scribd company logo
1 of 26
DATA STRUCTURES:
• DATA STRUCTURES ARE PARTICULAR WAYS OF STORING DATA TO
MAKE SOME OPERATION EASIER OR MORE EFFICIENT. THAT IS,
THEY ARE TUNED FOR CERTAIN TASKS
• DATA STRUCTURES ARE SUITED TO SOLVING CERTAIN PROBLEMS,
AND THEY ARE OFTEN ASSOCIATED WITH ALGORITHMS.
ROUGHLY TWO KINDS OF DATA STRUCTURES:
• BUILT-IN DATA STRUCTURES, DATA STRUCTURES THAT ARE SO
COMMON AS TO BE PROVIDED BY DEFAULT
• USER-DEFINED DATA STRUCTURES (CLASSES IN OBJECT ORIENTED
PROGRAMMING) THAT ARE DESIGNED FOR A PARTICULAR TASK
THE PYTHON LIST DATA STRUCTURE
• PYTHON COMES WITH A GENERAL SET OF BUILT IN DATA
STRUCTURES:
• LISTS
• TUPLES
• STRING
• DICTIONARIES
• SETS
• OTHERS...
• A LIST IS AN ORDERED SEQUENCE OF ITEMS.
• YOU HAVE SEEN SUCH A SEQUENCE BEFORE IN A STRING. A
STRING IS JUST A PARTICULAR KIND OF LIST
MAKE A LIST
SIMILARITIES WITH STRINGS
• CONCATENATE/+ (BUT ONLY OF LISTS)
• REPEAT/*
• INDEXING (THE [ ] OPERATOR)
• SLICING ([:])
• MEMBERSHIP (THE IN OPERATOR)
• LEN (THE LENGTH OPERATOR)
OPERATORS
[1, 2, 3] + [4]  [1, 2, 3, 4]
[1, 2, 3] * 2  [1, 2, 3, 1, 2, 3]
1 IN [1, 2, 3]  TRUE
[1, 2, 3] < [1, 2, 4]  TRUE
COMPARE INDEX TO INDEX, FIRST DIFFERENCE DETERMINES THE
RESULT
DIFFERENCES BETWEEN LISTS AND
STRINGS
• LISTS CAN CONTAIN A MIXTURE OF
ANY PYTHON OBJECT, STRINGS CAN
ONLY HOLD CHARACTERS
• 1,"BILL",1.2345, TRUE
• LISTS ARE MUTABLE, THEIR VALUES
CAN BE CHANGED, WHILE STRINGS
ARE IMMUTABLE
• LISTS ARE DESIGNATED WITH [ ],
WITH ELEMENTS SEPARATED BY
COMMAS, STRINGS USE " " OR ' '
INDEXING
• CAN BE A LITTLE CONFUSING, WHAT DOES THE [ ] MEAN, A LIST OR
AN INDEX?
[1, 2, 3][1]  2
• CONTEXT SOLVES THE PROBLEM. INDEX ALWAYS COMES AT THE
END OF AN EXPRESSION, AND IS PRECEDED BY SOMETHING (A
VARIABLE, A SEQUENCE)
LIST OF LISTS
MY_LIST = ['A', [1, 2, 3], 'Z']
• WHAT IS THE SECOND ELEMENT (INDEX 1) OF THAT LIST? ANOTHER LIST.
MY_LIST[1][0] # APPLY LEFT TO RIGHT
MY_LIST[1]  [1, 2, 3]
[1, 2, 3][0]  1
LIST FUNCTIONS
• LEN(LST): NUMBER OF ELEMENTS IN LIST (TOP LEVEL).
LEN([1, [1, 2], 3])  3
• MIN(LST): SMALLEST ELEMENT. MUST ALL BE THE SAME
TYPE!
• MAX(LST): LARGEST ELEMENT, AGAIN ALL MUST BE THE
SAME TYPE
• SUM(LST): SUM OF THE ELEMENTS, NUMERIC ONLY
ITERATION
YOU CAN ITERATE THROUGH THE ELEMENTS OF A LIST
LIKE YOU DID WITH A STRING:
CHANGE AN OBJECT'S CONTENTS
• STRINGS ARE IMMUTABLE. ONCE CREATED, THE OBJECT'S CONTENTS CANNOT
BE CHANGED. NEW OBJECTS CAN BE CREATED TO REFLECT A CHANGE, BUT THE
OBJECT ITSELF CANNOT BE CHANGED
MY_STR = 'ABC'
MY_STR[0] = 'Z' # CANNOT DO!
# INSTEAD, MAKE NEW STR
NEW_STR = MY_STR.REPLACE('A','Z')
LISTS ARE MUTABLE
UNLIKE STRINGS, LISTS ARE MUTABLE. YOU CAN CHANGE THE
OBJECT'S CONTENTS!
MY_LIST = [1, 2, 3]
MY_LIST[0] = 127
PRINT(MY_LIST)  [127, 2, 3]
LIST METHODS
• REMEMBER, A FUNCTION IS A SMALL PROGRAM (SUCH AS
LEN) THAT TAKES SOME ARGUMENTS, THE STUFF IN THE
PARENTHESIS, AND RETURNS SOME VALUE
• A METHOD IS A FUNCTION CALLED IN A SPECIAL WAY,
THE DOT CALL. IT IS CALLED IN THE CONTEXT OF AN
OBJECT (OR A VARIABLE ASSOCIATED WITH AN OBJECT)
AGAIN, LISTS HAVE METHODS
MY_LIST = ['A',1,TRUE]
MY_LIST.APPEND('Z')
the object that
we are calling the
method with
the name of
the method
arguments to
the method
SOME NEW METHODS
• A LIST IS MUTABLE AND CAN CHANGE:
• MY_LIST[0]='A' #INDEX ASSIGNMENT
• MY_LIST.APPEND()
• MY_LIST.EXTEND()
• MY_LIST.POP()
• MY_LIST.INSERT()
• MY_LIST.REMOVE()
• MY_LIST.SORT()
• MY_LIST.REVERSE()
MORE ABOUT LIST METHODS
• MOST OF THESE METHODS DO NOT RETURN A VALUE
• THIS IS BECAUSE LISTS ARE MUTABLE, SO THE METHODS
MODIFY THE LIST DIRECTLY. NO NEED TO RETURN
ANYTHING.
• CAN BE CONFUSING
UNUSUAL RESULTS
MY_LIST = [4, 7, 1, 2]
MY_LIST = MY_LIST.SORT()
MY_LIST  NONE # WHAT HAPPENED?
WHAT HAPPENED DID THE SORT OPERATION CHANGED THE
ORDER OF THE LIST IN PLACE (RIGHT SIDE OF ASSIGNMENT).
THEN THE SORT METHOD RETURNED NONE, WHICH WAS
ASSIGNED TO THE VARIABLE. THE LIST WAS LOST AND NONE IS
NOW THE VALUE OF THE VARIABLE.
RANGE
• WE HAVE SEEN THE RANGE FUNCTION BEFORE. IT
GENERATES A SEQUENCE OF INTEGERS.
• IN FACT WHAT IT GENERATES IS A LIST WITH THAT
SEQUENCE:
MYLIST = RANGE(1,5)
MYLIST IS [1,2,3,4]
SPLIT
• THE STRING METHOD SPLIT GENERATES A SEQUENCE OF
CHARACTERS BY SPLITTING THE STRING AT CERTAIN SPLIT-
CHARACTERS.
• IT RETURNS A LIST (WE DIDN'T MENTION THAT BEFORE)
SPLIT_LIST = 'THIS IS A TEST'.SPLIT()
SPLIT_LIST
 ['THIS', 'IS', 'A', 'TEST']
SORTING
ONLY LISTS HAVE A BUILT IN SORTING METHOD. THUS YOU OFTEN CONVERT YOUR
DATA TO A LIST IF IT NEEDS SORTING
MY_LIST = LIST('XYZABC')
MY_LIST ['X','Y','Z','A','B','C']
MY_LIST.SORT() # NO RETURN
MY_LIST 
['A', 'B', 'C', 'X', 'Y', 'Z']
REVERSE WORDS IN A STRING
JOIN METHOD OF STRING PLACES THE CALLING STRING
BETWEEN EVERY ELEMENT OF A LIST
SORTED FUNCTION
THE SORTED FUNCTION WILL BREAK A SEQUENCE INTO
ELEMENTS AND SORT THE SEQUENCE, PLACING THE RESULTS
IN A LIST
SORT_LIST = SORTED('HI MOM')
SORT_LIST 
[‘ ’,'H','I','M','M','O']
TUPLES
• TUPLES ARE SIMPLY IMMUTABLE LISTS
• THEY ARE PRINTED WITH (,)
LISTS AND TUPLE
• EVERYTHING THAT WORKS WITH A LIST WORKS WITH A
TUPLE EXCEPT METHODS THAT MODIFY THE TUPLE
• THUS INDEXING, SLICING, LEN, PRINT ALL WORK AS
EXPECTED
• HOWEVER, NONE OF THE MUTABLE METHODS WORK:
APPEND, EXTEND, DEL
COMMAS MAKE A TUPLE
FOR TUPLES, YOU CAN THINK OF A COMMA AS THE OPERATOR
THAT MAKES A TUPLE, WHERE THE ( ) SIMPLY ACTS AS A
GROUPING:
MYTUPLE = 1,2 # CREATES (1,2)
MYTUPLE = (1,) # CREATES (1)
MYTUPLE = (1) # CREATES 1 NOT (1)
MYTUPLE = 1, # CREATES (1)
MULTIPLE COLLECTS
[X+Y FOR X IN RANGE(1,4) FOR Y IN RANGE (1,4)]
IT IS AS IF WE HAD DONE THE FOLLOWING:
MY_LIST = [ ]
FOR X IN RANGE (1,4):
FOR Y IN RANGE (1,4):
MY_LIST.APPEND(X+Y)
 [2,3,4,3,4,5,4,5,6]

More Related Content

What's hot

What's hot (6)

DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
Materi 4.a analisis data
Materi 4.a analisis dataMateri 4.a analisis data
Materi 4.a analisis data
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9
 
Queries
QueriesQueries
Queries
 
Chapter 20 generic collections
Chapter 20   generic collectionsChapter 20   generic collections
Chapter 20 generic collections
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 

Similar to Lists and Tuples

Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Naveenkumar Muguda
 
powerpoint 2-13.pptx
powerpoint 2-13.pptxpowerpoint 2-13.pptx
powerpoint 2-13.pptxJuanPicasso7
 
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 pythonchanna basava
 
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.pdfMCCMOTOR
 
DATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARIDATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARISivaSankari36
 
02 Python Data Structure.pptx
02 Python Data Structure.pptx02 Python Data Structure.pptx
02 Python Data Structure.pptxssuser88c564
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear listsToniyaP1
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesswathirajstar
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 
Databases - Unit 2.pdf
Databases - Unit 2.pdfDatabases - Unit 2.pdf
Databases - Unit 2.pdfCynthiaAdzornu
 

Similar to Lists and Tuples (20)

Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Abstract Algebra and Category Theory
Abstract Algebra and Category Theory
 
powerpoint 2-13.pptx
powerpoint 2-13.pptxpowerpoint 2-13.pptx
powerpoint 2-13.pptx
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
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-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
Factors.pptx
Factors.pptxFactors.pptx
Factors.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
 
DATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARIDATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARI
 
02 Python Data Structure.pptx
02 Python Data Structure.pptx02 Python Data Structure.pptx
02 Python Data Structure.pptx
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Python lists &amp; sets
Python lists &amp; setsPython lists &amp; sets
Python lists &amp; sets
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 
Intro ds
Intro dsIntro ds
Intro ds
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Databases - Unit 2.pdf
Databases - Unit 2.pdfDatabases - Unit 2.pdf
Databases - Unit 2.pdf
 

More from Munazza-Mah-Jabeen (20)

Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
 
Object-Oriented Software
Object-Oriented SoftwareObject-Oriented Software
Object-Oriented Software
 
Templates and Exceptions
 Templates and Exceptions Templates and Exceptions
Templates and Exceptions
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
More About Strings
More About StringsMore About Strings
More About Strings
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
 
Functions
FunctionsFunctions
Functions
 
Pointers
PointersPointers
Pointers
 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
 
C++ programming basics
C++ programming basicsC++ programming basics
C++ programming basics
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

Lists and Tuples

  • 1. DATA STRUCTURES: • DATA STRUCTURES ARE PARTICULAR WAYS OF STORING DATA TO MAKE SOME OPERATION EASIER OR MORE EFFICIENT. THAT IS, THEY ARE TUNED FOR CERTAIN TASKS • DATA STRUCTURES ARE SUITED TO SOLVING CERTAIN PROBLEMS, AND THEY ARE OFTEN ASSOCIATED WITH ALGORITHMS. ROUGHLY TWO KINDS OF DATA STRUCTURES: • BUILT-IN DATA STRUCTURES, DATA STRUCTURES THAT ARE SO COMMON AS TO BE PROVIDED BY DEFAULT • USER-DEFINED DATA STRUCTURES (CLASSES IN OBJECT ORIENTED PROGRAMMING) THAT ARE DESIGNED FOR A PARTICULAR TASK
  • 2. THE PYTHON LIST DATA STRUCTURE • PYTHON COMES WITH A GENERAL SET OF BUILT IN DATA STRUCTURES: • LISTS • TUPLES • STRING • DICTIONARIES • SETS • OTHERS... • A LIST IS AN ORDERED SEQUENCE OF ITEMS. • YOU HAVE SEEN SUCH A SEQUENCE BEFORE IN A STRING. A STRING IS JUST A PARTICULAR KIND OF LIST
  • 4. SIMILARITIES WITH STRINGS • CONCATENATE/+ (BUT ONLY OF LISTS) • REPEAT/* • INDEXING (THE [ ] OPERATOR) • SLICING ([:]) • MEMBERSHIP (THE IN OPERATOR) • LEN (THE LENGTH OPERATOR)
  • 5. OPERATORS [1, 2, 3] + [4]  [1, 2, 3, 4] [1, 2, 3] * 2  [1, 2, 3, 1, 2, 3] 1 IN [1, 2, 3]  TRUE [1, 2, 3] < [1, 2, 4]  TRUE COMPARE INDEX TO INDEX, FIRST DIFFERENCE DETERMINES THE RESULT
  • 6. DIFFERENCES BETWEEN LISTS AND STRINGS • LISTS CAN CONTAIN A MIXTURE OF ANY PYTHON OBJECT, STRINGS CAN ONLY HOLD CHARACTERS • 1,"BILL",1.2345, TRUE • LISTS ARE MUTABLE, THEIR VALUES CAN BE CHANGED, WHILE STRINGS ARE IMMUTABLE • LISTS ARE DESIGNATED WITH [ ], WITH ELEMENTS SEPARATED BY COMMAS, STRINGS USE " " OR ' '
  • 7. INDEXING • CAN BE A LITTLE CONFUSING, WHAT DOES THE [ ] MEAN, A LIST OR AN INDEX? [1, 2, 3][1]  2 • CONTEXT SOLVES THE PROBLEM. INDEX ALWAYS COMES AT THE END OF AN EXPRESSION, AND IS PRECEDED BY SOMETHING (A VARIABLE, A SEQUENCE)
  • 8. LIST OF LISTS MY_LIST = ['A', [1, 2, 3], 'Z'] • WHAT IS THE SECOND ELEMENT (INDEX 1) OF THAT LIST? ANOTHER LIST. MY_LIST[1][0] # APPLY LEFT TO RIGHT MY_LIST[1]  [1, 2, 3] [1, 2, 3][0]  1
  • 9. LIST FUNCTIONS • LEN(LST): NUMBER OF ELEMENTS IN LIST (TOP LEVEL). LEN([1, [1, 2], 3])  3 • MIN(LST): SMALLEST ELEMENT. MUST ALL BE THE SAME TYPE! • MAX(LST): LARGEST ELEMENT, AGAIN ALL MUST BE THE SAME TYPE • SUM(LST): SUM OF THE ELEMENTS, NUMERIC ONLY
  • 10. ITERATION YOU CAN ITERATE THROUGH THE ELEMENTS OF A LIST LIKE YOU DID WITH A STRING:
  • 11. CHANGE AN OBJECT'S CONTENTS • STRINGS ARE IMMUTABLE. ONCE CREATED, THE OBJECT'S CONTENTS CANNOT BE CHANGED. NEW OBJECTS CAN BE CREATED TO REFLECT A CHANGE, BUT THE OBJECT ITSELF CANNOT BE CHANGED MY_STR = 'ABC' MY_STR[0] = 'Z' # CANNOT DO! # INSTEAD, MAKE NEW STR NEW_STR = MY_STR.REPLACE('A','Z')
  • 12. LISTS ARE MUTABLE UNLIKE STRINGS, LISTS ARE MUTABLE. YOU CAN CHANGE THE OBJECT'S CONTENTS! MY_LIST = [1, 2, 3] MY_LIST[0] = 127 PRINT(MY_LIST)  [127, 2, 3]
  • 13. LIST METHODS • REMEMBER, A FUNCTION IS A SMALL PROGRAM (SUCH AS LEN) THAT TAKES SOME ARGUMENTS, THE STUFF IN THE PARENTHESIS, AND RETURNS SOME VALUE • A METHOD IS A FUNCTION CALLED IN A SPECIAL WAY, THE DOT CALL. IT IS CALLED IN THE CONTEXT OF AN OBJECT (OR A VARIABLE ASSOCIATED WITH AN OBJECT)
  • 14. AGAIN, LISTS HAVE METHODS MY_LIST = ['A',1,TRUE] MY_LIST.APPEND('Z') the object that we are calling the method with the name of the method arguments to the method
  • 15. SOME NEW METHODS • A LIST IS MUTABLE AND CAN CHANGE: • MY_LIST[0]='A' #INDEX ASSIGNMENT • MY_LIST.APPEND() • MY_LIST.EXTEND() • MY_LIST.POP() • MY_LIST.INSERT() • MY_LIST.REMOVE() • MY_LIST.SORT() • MY_LIST.REVERSE()
  • 16. MORE ABOUT LIST METHODS • MOST OF THESE METHODS DO NOT RETURN A VALUE • THIS IS BECAUSE LISTS ARE MUTABLE, SO THE METHODS MODIFY THE LIST DIRECTLY. NO NEED TO RETURN ANYTHING. • CAN BE CONFUSING
  • 17. UNUSUAL RESULTS MY_LIST = [4, 7, 1, 2] MY_LIST = MY_LIST.SORT() MY_LIST  NONE # WHAT HAPPENED? WHAT HAPPENED DID THE SORT OPERATION CHANGED THE ORDER OF THE LIST IN PLACE (RIGHT SIDE OF ASSIGNMENT). THEN THE SORT METHOD RETURNED NONE, WHICH WAS ASSIGNED TO THE VARIABLE. THE LIST WAS LOST AND NONE IS NOW THE VALUE OF THE VARIABLE.
  • 18. RANGE • WE HAVE SEEN THE RANGE FUNCTION BEFORE. IT GENERATES A SEQUENCE OF INTEGERS. • IN FACT WHAT IT GENERATES IS A LIST WITH THAT SEQUENCE: MYLIST = RANGE(1,5) MYLIST IS [1,2,3,4]
  • 19. SPLIT • THE STRING METHOD SPLIT GENERATES A SEQUENCE OF CHARACTERS BY SPLITTING THE STRING AT CERTAIN SPLIT- CHARACTERS. • IT RETURNS A LIST (WE DIDN'T MENTION THAT BEFORE) SPLIT_LIST = 'THIS IS A TEST'.SPLIT() SPLIT_LIST  ['THIS', 'IS', 'A', 'TEST']
  • 20. SORTING ONLY LISTS HAVE A BUILT IN SORTING METHOD. THUS YOU OFTEN CONVERT YOUR DATA TO A LIST IF IT NEEDS SORTING MY_LIST = LIST('XYZABC') MY_LIST ['X','Y','Z','A','B','C'] MY_LIST.SORT() # NO RETURN MY_LIST  ['A', 'B', 'C', 'X', 'Y', 'Z']
  • 21. REVERSE WORDS IN A STRING JOIN METHOD OF STRING PLACES THE CALLING STRING BETWEEN EVERY ELEMENT OF A LIST
  • 22. SORTED FUNCTION THE SORTED FUNCTION WILL BREAK A SEQUENCE INTO ELEMENTS AND SORT THE SEQUENCE, PLACING THE RESULTS IN A LIST SORT_LIST = SORTED('HI MOM') SORT_LIST  [‘ ’,'H','I','M','M','O']
  • 23. TUPLES • TUPLES ARE SIMPLY IMMUTABLE LISTS • THEY ARE PRINTED WITH (,)
  • 24. LISTS AND TUPLE • EVERYTHING THAT WORKS WITH A LIST WORKS WITH A TUPLE EXCEPT METHODS THAT MODIFY THE TUPLE • THUS INDEXING, SLICING, LEN, PRINT ALL WORK AS EXPECTED • HOWEVER, NONE OF THE MUTABLE METHODS WORK: APPEND, EXTEND, DEL
  • 25. COMMAS MAKE A TUPLE FOR TUPLES, YOU CAN THINK OF A COMMA AS THE OPERATOR THAT MAKES A TUPLE, WHERE THE ( ) SIMPLY ACTS AS A GROUPING: MYTUPLE = 1,2 # CREATES (1,2) MYTUPLE = (1,) # CREATES (1) MYTUPLE = (1) # CREATES 1 NOT (1) MYTUPLE = 1, # CREATES (1)
  • 26. MULTIPLE COLLECTS [X+Y FOR X IN RANGE(1,4) FOR Y IN RANGE (1,4)] IT IS AS IF WE HAD DONE THE FOLLOWING: MY_LIST = [ ] FOR X IN RANGE (1,4): FOR Y IN RANGE (1,4): MY_LIST.APPEND(X+Y)  [2,3,4,3,4,5,4,5,6]