SlideShare a Scribd company logo
2
Sequence Types
1. Tuple: (‘john’, 32, [CMSC])
 A simple immutable ordered sequence of items
 Items can be of mixed types, including collection types
2. Strings: “John Smith”
◦ Immutable
◦ Conceptually very much like a tuple
3. List: [1, 2, ‘john’, (‘up’, ‘down’)]
 Mutable ordered sequence of items of mixed types
Most read
5
Sequence Types 2
Access individual members of a tuple, list, or string using square bracket “array” notation
Note that all are 0 based…
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> tu[1] # Second item in the tuple.
‘abc’
>>> li = [“abc”, 34, 4.34, 23]
>>> li[1] # Second item in the list.
34
>>> st = “Hello World”
>>> st[1] # Second character in string.
‘e’
Most read
8
Slicing: return copy of a =subset
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
Omit first index to make copy starting from beginning of the container
>>> t[:2]
(23, ‘abc’)
Omit second index to make copy starting at first index and going to end
>>> t[2:]
(4.56, (2,3), ‘def’)
Most read
Sequence types: Tuples,
Lists, and Strings
M.BOBBY
ASSISTANT PROFESSOR & HEAD,
DEPARTMENT OF COMPUTER SCIENCE
SRI ADI CHUNCHANAGIRI WOMEN’S COLLEGE, CUMBUM
Sequence Types
1. Tuple: (‘john’, 32, [CMSC])
 A simple immutable ordered sequence of items
 Items can be of mixed types, including collection types
2. Strings: “John Smith”
◦ Immutable
◦ Conceptually very much like a tuple
3. List: [1, 2, ‘john’, (‘up’, ‘down’)]
 Mutable ordered sequence of items of mixed types
Similar Syntax
All three sequence types (tuples, strings, and lists) share much
of the same syntax and functionality.
Key difference:
◦Tuples and strings are immutable
◦ Lists are mutable
The operations shown in this section can be applied to all
sequence types
◦most examples will just show the operation performed on one
Sequence Types 1
Define tuples using parentheses and commas
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
Define lists are using square brackets and commas
>>> li = [“abc”, 34, 4.34, 23]
Define strings using quotes (“, ‘, or “““).
>>> st = “Hello World”
>>> st = ‘Hello World’
>>> st = “““This is a multi-line
string that uses triple quotes.”””
Sequence Types 2
Access individual members of a tuple, list, or string using square bracket “array” notation
Note that all are 0 based…
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> tu[1] # Second item in the tuple.
‘abc’
>>> li = [“abc”, 34, 4.34, 23]
>>> li[1] # Second item in the list.
34
>>> st = “Hello World”
>>> st[1] # Second character in string.
‘e’
Positive and negative indices
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
Positive index: count from the left, starting with 0
>>> t[1]
‘abc’
Negative index: count from right, starting with –1
>>> t[-3]
4.56
Slicing: return copy of a subset
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
Return a copy of the container with a subset of the original members. Start copying at
the first index, and stop copying before second.
>>> t[1:4]
(‘abc’, 4.56, (2,3))
Negative indices count from end
>>> t[1:-1]
(‘abc’, 4.56, (2,3))
Slicing: return copy of a =subset
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
Omit first index to make copy starting from beginning of the container
>>> t[:2]
(23, ‘abc’)
Omit second index to make copy starting at first index and going to end
>>> t[2:]
(4.56, (2,3), ‘def’)
Copying the Whole Sequence
[ : ] makes a copy of an entire sequence
>>> t[:]
(23, ‘abc’, 4.56, (2,3), ‘def’)
Note the difference between these two lines for mutable sequences
>>> l2 = l1 # Both refer to 1 ref,
# changing one affects both
>>> l2 = l1[:] # Independent copies, two refs
The ‘in’ Operator
Boolean test whether a value is inside a container:
>>> t = [1, 2, 4, 5]
>>> 3 in t
False
>>> 4 in t
True
>>> 4 not in t
False
For strings, tests for substrings
>>> a = 'abcde'
>>> 'c' in a
True
>>> 'cd' in a
True
>>> 'ac' in a
False
Be careful: the in keyword is also used in the syntax of for loops and list comprehensions
The + Operator
The + operator produces a new tuple, list, or string whose value is the concatenation of its arguments.
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> “Hello” + “ ” + “World”
‘Hello World’
The * Operator
The * operator produces a new tuple, list, or string that “repeats” the original content.
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> “Hello” * 3
‘HelloHelloHello’

More Related Content

What's hot (20)

Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
Mohd Arif
 
NUMPY
NUMPY NUMPY
NUMPY
Global Academy of Technology
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
Saurabh Kumar
 
C fundamental
C fundamentalC fundamental
C fundamental
Selvam Edwin
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
ForwardBlog Enewzletter
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
GirdharRatne
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Chapter 11 - File System Implementation
Chapter 11 - File System ImplementationChapter 11 - File System Implementation
Chapter 11 - File System Implementation
Wayne Jones Jnr
 
Data file handling
Data file handlingData file handling
Data file handling
Prof. Dr. K. Adisesha
 
Built in function
Built in functionBuilt in function
Built in function
MD. Rayhanul Islam Sayket
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
Explore Skilled
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
Madishetty Prathibha
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
Sachin Yadav
 

Similar to Sequence Types in Python Programming (20)

Python Basics it will teach you about data types
Python Basics it will teach you about data typesPython Basics it will teach you about data types
Python Basics it will teach you about data types
NimitSinghal2
 
Data types usually used in python for coding
Data types usually used in python for codingData types usually used in python for coding
Data types usually used in python for coding
PriyankaRajaboina
 
02python.ppt
02python.ppt02python.ppt
02python.ppt
rehanafarheenece
 
02python.ppt
02python.ppt02python.ppt
02python.ppt
ssuser492e7f
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
Mr. Vikram Singh Slathia
 
Python lec2
Python lec2Python lec2
Python lec2
Swarup Ghosh
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
gjcross
 
Notes8
Notes8Notes8
Notes8
Amba Research
 
UNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptxUNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptx
harikahhy
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Sujith Kumar
 
dataStructuresInPython.pptx
dataStructuresInPython.pptxdataStructuresInPython.pptx
dataStructuresInPython.pptx
YashaswiniChandrappa1
 
XI_CS_Notes for strings.docx
XI_CS_Notes for strings.docxXI_CS_Notes for strings.docx
XI_CS_Notes for strings.docx
AnithaSathiaseelan1
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptxPRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
kirtisharma7537
 
Python review2
Python review2Python review2
Python review2
vibrantuser
 
Data structures in Python
Data structures in PythonData structures in Python
Data structures in Python
MITULJAMANG
 
Pythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of ProgrammingPythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
Python review2
Python review2Python review2
Python review2
vibrantuser
 
Python ds
Python dsPython ds
Python ds
Sharath Ankrajegowda
 
Python Basics it will teach you about data types
Python Basics it will teach you about data typesPython Basics it will teach you about data types
Python Basics it will teach you about data types
NimitSinghal2
 
Data types usually used in python for coding
Data types usually used in python for codingData types usually used in python for coding
Data types usually used in python for coding
PriyankaRajaboina
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
gjcross
 
UNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptxUNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptx
harikahhy
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Sujith Kumar
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptxPRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
kirtisharma7537
 
Data structures in Python
Data structures in PythonData structures in Python
Data structures in Python
MITULJAMANG
 
Pythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of ProgrammingPythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
Ad

More from Bobby Murugesan (10)

Study Material for Problem Solving Techniques
Study Material for Problem Solving TechniquesStudy Material for Problem Solving Techniques
Study Material for Problem Solving Techniques
Bobby Murugesan
 
Fundamentals of Information Technology study Material
Fundamentals of Information Technology study MaterialFundamentals of Information Technology study Material
Fundamentals of Information Technology study Material
Bobby Murugesan
 
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
Bobby Murugesan
 
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENTNOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
Bobby Murugesan
 
Computer skills for web designing and video editing_Lab Manual.pdf
Computer skills for web designing and video editing_Lab Manual.pdfComputer skills for web designing and video editing_Lab Manual.pdf
Computer skills for web designing and video editing_Lab Manual.pdf
Bobby Murugesan
 
Python Lab Manual
Python Lab ManualPython Lab Manual
Python Lab Manual
Bobby Murugesan
 
Python The basics
Python   The basicsPython   The basics
Python The basics
Bobby Murugesan
 
Impressive Google Apps
Impressive Google AppsImpressive Google Apps
Impressive Google Apps
Bobby Murugesan
 
How to register in Swayam
How to register in SwayamHow to register in Swayam
How to register in Swayam
Bobby Murugesan
 
Green computing introduction
Green computing introductionGreen computing introduction
Green computing introduction
Bobby Murugesan
 
Study Material for Problem Solving Techniques
Study Material for Problem Solving TechniquesStudy Material for Problem Solving Techniques
Study Material for Problem Solving Techniques
Bobby Murugesan
 
Fundamentals of Information Technology study Material
Fundamentals of Information Technology study MaterialFundamentals of Information Technology study Material
Fundamentals of Information Technology study Material
Bobby Murugesan
 
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
Bobby Murugesan
 
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENTNOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
Bobby Murugesan
 
Computer skills for web designing and video editing_Lab Manual.pdf
Computer skills for web designing and video editing_Lab Manual.pdfComputer skills for web designing and video editing_Lab Manual.pdf
Computer skills for web designing and video editing_Lab Manual.pdf
Bobby Murugesan
 
How to register in Swayam
How to register in SwayamHow to register in Swayam
How to register in Swayam
Bobby Murugesan
 
Green computing introduction
Green computing introductionGreen computing introduction
Green computing introduction
Bobby Murugesan
 
Ad

Recently uploaded (20)

Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 

Sequence Types in Python Programming

  • 1. Sequence types: Tuples, Lists, and Strings M.BOBBY ASSISTANT PROFESSOR & HEAD, DEPARTMENT OF COMPUTER SCIENCE SRI ADI CHUNCHANAGIRI WOMEN’S COLLEGE, CUMBUM
  • 2. Sequence Types 1. Tuple: (‘john’, 32, [CMSC])  A simple immutable ordered sequence of items  Items can be of mixed types, including collection types 2. Strings: “John Smith” ◦ Immutable ◦ Conceptually very much like a tuple 3. List: [1, 2, ‘john’, (‘up’, ‘down’)]  Mutable ordered sequence of items of mixed types
  • 3. Similar Syntax All three sequence types (tuples, strings, and lists) share much of the same syntax and functionality. Key difference: ◦Tuples and strings are immutable ◦ Lists are mutable The operations shown in this section can be applied to all sequence types ◦most examples will just show the operation performed on one
  • 4. Sequence Types 1 Define tuples using parentheses and commas >>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’) Define lists are using square brackets and commas >>> li = [“abc”, 34, 4.34, 23] Define strings using quotes (“, ‘, or “““). >>> st = “Hello World” >>> st = ‘Hello World’ >>> st = “““This is a multi-line string that uses triple quotes.”””
  • 5. Sequence Types 2 Access individual members of a tuple, list, or string using square bracket “array” notation Note that all are 0 based… >>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’) >>> tu[1] # Second item in the tuple. ‘abc’ >>> li = [“abc”, 34, 4.34, 23] >>> li[1] # Second item in the list. 34 >>> st = “Hello World” >>> st[1] # Second character in string. ‘e’
  • 6. Positive and negative indices >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Positive index: count from the left, starting with 0 >>> t[1] ‘abc’ Negative index: count from right, starting with –1 >>> t[-3] 4.56
  • 7. Slicing: return copy of a subset >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Return a copy of the container with a subset of the original members. Start copying at the first index, and stop copying before second. >>> t[1:4] (‘abc’, 4.56, (2,3)) Negative indices count from end >>> t[1:-1] (‘abc’, 4.56, (2,3))
  • 8. Slicing: return copy of a =subset >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Omit first index to make copy starting from beginning of the container >>> t[:2] (23, ‘abc’) Omit second index to make copy starting at first index and going to end >>> t[2:] (4.56, (2,3), ‘def’)
  • 9. Copying the Whole Sequence [ : ] makes a copy of an entire sequence >>> t[:] (23, ‘abc’, 4.56, (2,3), ‘def’) Note the difference between these two lines for mutable sequences >>> l2 = l1 # Both refer to 1 ref, # changing one affects both >>> l2 = l1[:] # Independent copies, two refs
  • 10. The ‘in’ Operator Boolean test whether a value is inside a container: >>> t = [1, 2, 4, 5] >>> 3 in t False >>> 4 in t True >>> 4 not in t False For strings, tests for substrings >>> a = 'abcde' >>> 'c' in a True >>> 'cd' in a True >>> 'ac' in a False Be careful: the in keyword is also used in the syntax of for loops and list comprehensions
  • 11. The + Operator The + operator produces a new tuple, list, or string whose value is the concatenation of its arguments. >>> (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) >>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] >>> “Hello” + “ ” + “World” ‘Hello World’
  • 12. The * Operator The * operator produces a new tuple, list, or string that “repeats” the original content. >>> (1, 2, 3) * 3 (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> [1, 2, 3] * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> “Hello” * 3 ‘HelloHelloHello’