ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Sequence Data Types
by
Jyostna Devi Bodapati
Python Programming Specialization
Python Programming ¨C Data Structures
Python Data Types
Data Types
Scalar Data
int
float
complex
boolean
Sequence Data
List
Tuple
String
Mapping Type
Dictionary
Set Type
Set
Frozen Set
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Types
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Data Types
Sequence is the ordered collection of elements
Allows to store multiple values in an organized and efficient manner
Sequence
String List Tuple
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Data Type: str
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Types: str
? A string is a collection of one or more characters
? Python strings can be enclosed either in single, double or triple quotes
? Python Strings belongs to str class
Python Programming Specialization
Python Programming ¨C Data Structures
Creation of Strings
>>>S1 = ¡®Python¡¯
>>>S2 = ¡°Python¡±
>>>S3 = ¡®¡¯¡¯Python¡¯¡¯¡¯
>>>S4 = ¡®¡¯¡¯Python
Programming
Specialization¡¯¡¯¡¯
Python Programming Specialization
Python Programming ¨C Data Structures
Accessing String Elements
? Let S1 = ¡°HELLO WORLD¡±
? How do we access the contents of the string?
? String elements can be accessed by passing an index with the name of the
string
? String name [ index ]
Python Programming Specialization
Python Programming ¨C Data Structures
String Indexing
? Indexing must be an integer
? Both negative and positive indexing are valid
? Ex: S1 = ¡°HELLO WORLD¡±
Python Programming Specialization
Python Programming ¨C Data Structures
String Indexing
? Integer indexing can be used to access string elements
? Both negative and positive indexing are valid
? Ex: S1 = ¡°HELLO WORLD¡±
Python Programming Specialization
Python Programming ¨C Data Structures
String Indexing
? Integer indexing can be used to access string elements
? Both negative and positive indexing are valid
? Ex: S1 = ¡°HELLO WORLD¡±
Python Programming Specialization
Python Programming ¨C Data Structures
Accessing String Elements
? Ex: S1 = ¡°HELLO WORLD¡±
>>> S1 [ 0 ]
>>> S1 [ 4 ]
>>> S1 [ -2 ]
>>> S1 [ 2: 7 ]
Python Programming Specialization
Python Programming ¨C Data Structures
Accessing String Elements
? Ex: S1 = ¡°HELLO WORLD¡±
>>> S1 [ 0 ]
>>> S1 [ 4 ]
>>> S1 [ -2 ]
>>> S1 [ 2 : 7 ]
>>> S1 [ 2 : 10 : 2 ]
#returns H
#returns O
#returns L
#returns LLO W
#returns LOWR
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Data Type: list
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Types: list
? Ordered collections of objects? of same or different types
? Lists are Mutable?
? Examples:
? >>>L3 = [10, 20, 30, 40]
? >>> L4 = [¡®Ram', 786 , 2.23, ¡®Raj', 70.2]
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Types: list
? L1 = [10, 20, 30, 40, 50, 60, 70]
? Access the entire list ¡ú use is its name.?
? >>> Print ( L1 )?
? Access a single item ¡ú use index operator [ ] ?
? >>> L1 [ 0 ] ?
? Access part of a list ¡ú use slicing operator [ : : ]?
? >>> L1 [ 4 : 8 : 2 ]
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Data Type: tuple
Python Programming Specialization
Python Programming ¨C Data Structures
Sequence Types: tuple
? Ordered collections of objects? of same or different types
? Tuples are Immutable
? Examples:
? >>> T1 = ( 9, 2, 13, 24 )
? >>> T2 = ( ¡®Ram', 786 , 2.23, ¡®Raj', 70.2 )
Python Programming Specialization
Python Programming ¨C Data Structures
Access tuple Elements
? T1 = ( 10, 20, 30, 40, 50, 60, 70 )
? Access the entire tuple ¡ú use is its name.?
? >>> Print ( T1 )?
? Access a single item ¡ú use index operator [ ] ?
? >>> T1 [ 0 ] ?
? Access part of a tuple ¡ú use slicing operator [ : : ]?
? >>> T1 [ 2 : 6 : 2 ]
Python Sequence Data types in Brief

More Related Content

Python Sequence Data types in Brief

  • 2. Python Programming Specialization Python Programming ¨C Data Structures Python Data Types Data Types Scalar Data int float complex boolean Sequence Data List Tuple String Mapping Type Dictionary Set Type Set Frozen Set
  • 3. Python Programming Specialization Python Programming ¨C Data Structures Sequence Types
  • 4. Python Programming Specialization Python Programming ¨C Data Structures Sequence Data Types Sequence is the ordered collection of elements Allows to store multiple values in an organized and efficient manner Sequence String List Tuple
  • 5. Python Programming Specialization Python Programming ¨C Data Structures Sequence Data Type: str
  • 6. Python Programming Specialization Python Programming ¨C Data Structures Sequence Types: str ? A string is a collection of one or more characters ? Python strings can be enclosed either in single, double or triple quotes ? Python Strings belongs to str class
  • 7. Python Programming Specialization Python Programming ¨C Data Structures Creation of Strings >>>S1 = ¡®Python¡¯ >>>S2 = ¡°Python¡± >>>S3 = ¡®¡¯¡¯Python¡¯¡¯¡¯ >>>S4 = ¡®¡¯¡¯Python Programming Specialization¡¯¡¯¡¯
  • 8. Python Programming Specialization Python Programming ¨C Data Structures Accessing String Elements ? Let S1 = ¡°HELLO WORLD¡± ? How do we access the contents of the string? ? String elements can be accessed by passing an index with the name of the string ? String name [ index ]
  • 9. Python Programming Specialization Python Programming ¨C Data Structures String Indexing ? Indexing must be an integer ? Both negative and positive indexing are valid ? Ex: S1 = ¡°HELLO WORLD¡±
  • 10. Python Programming Specialization Python Programming ¨C Data Structures String Indexing ? Integer indexing can be used to access string elements ? Both negative and positive indexing are valid ? Ex: S1 = ¡°HELLO WORLD¡±
  • 11. Python Programming Specialization Python Programming ¨C Data Structures String Indexing ? Integer indexing can be used to access string elements ? Both negative and positive indexing are valid ? Ex: S1 = ¡°HELLO WORLD¡±
  • 12. Python Programming Specialization Python Programming ¨C Data Structures Accessing String Elements ? Ex: S1 = ¡°HELLO WORLD¡± >>> S1 [ 0 ] >>> S1 [ 4 ] >>> S1 [ -2 ] >>> S1 [ 2: 7 ]
  • 13. Python Programming Specialization Python Programming ¨C Data Structures Accessing String Elements ? Ex: S1 = ¡°HELLO WORLD¡± >>> S1 [ 0 ] >>> S1 [ 4 ] >>> S1 [ -2 ] >>> S1 [ 2 : 7 ] >>> S1 [ 2 : 10 : 2 ] #returns H #returns O #returns L #returns LLO W #returns LOWR
  • 14. Python Programming Specialization Python Programming ¨C Data Structures Sequence Data Type: list
  • 15. Python Programming Specialization Python Programming ¨C Data Structures Sequence Types: list ? Ordered collections of objects? of same or different types ? Lists are Mutable? ? Examples: ? >>>L3 = [10, 20, 30, 40] ? >>> L4 = [¡®Ram', 786 , 2.23, ¡®Raj', 70.2]
  • 16. Python Programming Specialization Python Programming ¨C Data Structures Sequence Types: list ? L1 = [10, 20, 30, 40, 50, 60, 70] ? Access the entire list ¡ú use is its name.? ? >>> Print ( L1 )? ? Access a single item ¡ú use index operator [ ] ? ? >>> L1 [ 0 ] ? ? Access part of a list ¡ú use slicing operator [ : : ]? ? >>> L1 [ 4 : 8 : 2 ]
  • 17. Python Programming Specialization Python Programming ¨C Data Structures Sequence Data Type: tuple
  • 18. Python Programming Specialization Python Programming ¨C Data Structures Sequence Types: tuple ? Ordered collections of objects? of same or different types ? Tuples are Immutable ? Examples: ? >>> T1 = ( 9, 2, 13, 24 ) ? >>> T2 = ( ¡®Ram', 786 , 2.23, ¡®Raj', 70.2 )
  • 19. Python Programming Specialization Python Programming ¨C Data Structures Access tuple Elements ? T1 = ( 10, 20, 30, 40, 50, 60, 70 ) ? Access the entire tuple ¡ú use is its name.? ? >>> Print ( T1 )? ? Access a single item ¡ú use index operator [ ] ? ? >>> T1 [ 0 ] ? ? Access part of a tuple ¡ú use slicing operator [ : : ]? ? >>> T1 [ 2 : 6 : 2 ]