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)

PPTX
Wide area network (wan)
Bhavesh Goswami
 
PPTX
chp unit 1 Provide Network System Administration.pptx
TadeseBeyene
 
PPTX
Wi Fi Security
yousef emami
 
PPTX
Subentting, Supernetting and VLSM presentation
Zakaria Hossain
 
PPTX
Computer network ppt communication
Kajal Sharma
 
PDF
Data security and Integrity
Zaid Shabbir
 
PPTX
OSI Model
Simran Kaur
 
PPTX
Bash shell scripting
VIKAS TIWARI
 
PPTX
Virus and its types 2
Saud G
 
PPTX
Modern Network Security Issue and Challenge
Ikhtiar Khan Sohan
 
PPT
AES.ppt
ssuser6602e0
 
PPT
Ch:2 The Physical Layer
Mubashir Yasin
 
PPTX
Buffer and scanner
Arif Ullah
 
PPTX
Client Server Network By Usman Ihsan
Subhan_Virk_UAF
 
PPTX
Rootkits
TharinduUdaraRanasin
 
PDF
Cs8792 cns - unit iv
ArthyR3
 
PPT
Security models
LJ PROJECTS
 
PPTX
Wireless network security
Shahid Beheshti University
 
PPTX
USES OF THE COMPUTER NETWORK
GLOBAL TECHNOLOGY CONSULTANCY
 
Wide area network (wan)
Bhavesh Goswami
 
chp unit 1 Provide Network System Administration.pptx
TadeseBeyene
 
Wi Fi Security
yousef emami
 
Subentting, Supernetting and VLSM presentation
Zakaria Hossain
 
Computer network ppt communication
Kajal Sharma
 
Data security and Integrity
Zaid Shabbir
 
OSI Model
Simran Kaur
 
Bash shell scripting
VIKAS TIWARI
 
Virus and its types 2
Saud G
 
Modern Network Security Issue and Challenge
Ikhtiar Khan Sohan
 
AES.ppt
ssuser6602e0
 
Ch:2 The Physical Layer
Mubashir Yasin
 
Buffer and scanner
Arif Ullah
 
Client Server Network By Usman Ihsan
Subhan_Virk_UAF
 
Cs8792 cns - unit iv
ArthyR3
 
Security models
LJ PROJECTS
 
Wireless network security
Shahid Beheshti University
 
USES OF THE COMPUTER NETWORK
GLOBAL TECHNOLOGY CONSULTANCY
 

Similar to Sequence Types in Python Programming (20)

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

More from Bobby Murugesan (10)

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

Recently uploaded (20)

PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PPTX
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 

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’