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

DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
Materi 4.a analisis data
Materi 4.a analisis dataMateri 4.a analisis data
Materi 4.a analisis data
SMAN 1 Wanasalam
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9dplunkett
 
Chapter 20 generic collections
Chapter 20   generic collectionsChapter 20   generic collections
Chapter 20 generic collections
CSDeptSriKaliswariCo
 
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
Sowmya Jyothi
 

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.pptx
JuanPicasso7
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
Prof. Dr. K. Adisesha
 
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
channa basava
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
AnitaDevi158873
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
MuhammadUmerIhtisham
 
Factors.pptx
Factors.pptxFactors.pptx
Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...
abhaysingh19149
 
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
 
DATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARIDATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARI
SivaSankari36
 
02 Python Data Structure.pptx
02 Python Data Structure.pptx02 Python Data Structure.pptx
02 Python Data Structure.pptx
ssuser88c564
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
ToniyaP1
 
Sql introduction
Sql introductionSql introduction
Sql introduction
Bhavya Chawla
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
Md.Mojibul Hoque
 
Python lists &amp; sets
Python lists &amp; setsPython lists &amp; sets
Python lists &amp; sets
Aswini Dharmaraj
 
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 - notes
swathirajstar
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
AshaWankar1
 

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
 
Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...
 
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
 

More from Munazza-Mah-Jabeen

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

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

Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

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]