SlideShare a Scribd company logo
2
Most read
5
Most read
8
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)

Instruction cycle
Instruction cycle
shweta-sharma99
 
Algorithm and flowchart
Algorithm and flowchart
Rabin BK
 
Instruction codes
Instruction codes
pradeepa velmurugan
 
Instruction cycle presentation
Instruction cycle presentation
Moniba Irfan
 
Program control instructions
Program control instructions
Dr. Girish GS
 
latches
latches
Unsa Shakir
 
Escape sequences
Escape sequences
Way2itech
 
Algorithm
Algorithm
IHTISHAM UL HAQ
 
Chapter 13 - I/O Systems
Chapter 13 - I/O Systems
Wayne Jones Jnr
 
Operating System
Operating System
Griffinder VinHai
 
Sequential Version / Version 1 Unsigned Multiplication Algorithm
Sequential Version / Version 1 Unsigned Multiplication Algorithm
babuece
 
Variables in C Programming
Variables in C Programming
programming9
 
Counters
Counters
Ketaki_Pattani
 
Memory hierarchy
Memory hierarchy
NancyBeaulah_R
 
BOOLEAN ALGEBRA AND LOGIC GATE
BOOLEAN ALGEBRA AND LOGIC GATE
Tamim Tanvir
 
Turing Machine
Turing Machine
Rajendran
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital Systems
SSE_AndyLi
 
Unit 1 introduction to Operating System
Unit 1 introduction to Operating System
zahid7578
 
Johnson Counter
Johnson Counter
Jatinder Kumar Chaurasia
 
PPT ON ALGORITHM
PPT ON ALGORITHM
prashant tripathi
 
Algorithm and flowchart
Algorithm and flowchart
Rabin BK
 
Instruction cycle presentation
Instruction cycle presentation
Moniba Irfan
 
Program control instructions
Program control instructions
Dr. Girish GS
 
Escape sequences
Escape sequences
Way2itech
 
Chapter 13 - I/O Systems
Chapter 13 - I/O Systems
Wayne Jones Jnr
 
Sequential Version / Version 1 Unsigned Multiplication Algorithm
Sequential Version / Version 1 Unsigned Multiplication Algorithm
babuece
 
Variables in C Programming
Variables in C Programming
programming9
 
BOOLEAN ALGEBRA AND LOGIC GATE
BOOLEAN ALGEBRA AND LOGIC GATE
Tamim Tanvir
 
Turing Machine
Turing Machine
Rajendran
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital Systems
SSE_AndyLi
 
Unit 1 introduction to Operating System
Unit 1 introduction to Operating System
zahid7578
 

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 types
NimitSinghal2
 
Data types usually used in python for coding
Data types usually used in python for coding
PriyankaRajaboina
 
02python.ppt
02python.ppt
rehanafarheenece
 
02python.ppt
02python.ppt
ssuser492e7f
 
‘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
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
Guru Nanak Technical Institutions
 
mooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthon
garvitbisht27
 
Notes3
Notes3
hccit
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
Syed Mustafa
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
Syed Mustafa
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
RamiHarrathi1
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
channa basava
 
Module III.pdf
Module III.pdf
R.K.College of engg & Tech
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
chap 06 hgjhg hghg hh ghg jh jhghj gj g.ppt
chap 06 hgjhg hghg hh ghg jh jhghj gj g.ppt
santonino3
 
Pytho_tuples
Pytho_tuples
BMS Institute of Technology and Management
 
Session -5for students.pdf
Session -5for students.pdf
priyanshusoni53
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
GDSCKYAMBOGO
 
Module-3.pptx
Module-3.pptx
Manohar Nelli
 
Programming in Python Lists and its methods .ppt
Programming in Python Lists and its methods .ppt
Dr. Jasmine Beulah Gnanadurai
 
Python 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 coding
PriyankaRajaboina
 
‘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
 
mooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthon
garvitbisht27
 
Notes3
Notes3
hccit
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
Syed Mustafa
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
Syed Mustafa
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
RamiHarrathi1
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
channa basava
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
chap 06 hgjhg hghg hh ghg jh jhghj gj g.ppt
chap 06 hgjhg hghg hh ghg jh jhghj gj g.ppt
santonino3
 
Session -5for students.pdf
Session -5for students.pdf
priyanshusoni53
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
GDSCKYAMBOGO
 
Ad

More from Bobby Murugesan (10)

Study Material for Problem Solving Techniques
Study Material for Problem Solving Techniques
Bobby Murugesan
 
Fundamentals 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 "
Bobby Murugesan
 
NOTES 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.pdf
Bobby Murugesan
 
Python Lab Manual
Python Lab Manual
Bobby Murugesan
 
Python The basics
Python The basics
Bobby Murugesan
 
Impressive Google Apps
Impressive Google Apps
Bobby Murugesan
 
How to register in Swayam
How to register in Swayam
Bobby Murugesan
 
Green computing introduction
Green computing introduction
Bobby Murugesan
 
Study Material for Problem Solving Techniques
Study Material for Problem Solving Techniques
Bobby Murugesan
 
Fundamentals 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 "
Bobby Murugesan
 
NOTES 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.pdf
Bobby Murugesan
 
How to register in Swayam
How to register in Swayam
Bobby Murugesan
 
Green computing introduction
Green computing introduction
Bobby Murugesan
 
Ad

Recently uploaded (20)

Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 

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’