Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Introduction to Python
  5. Values and Types
  6. Sequence Data Type

Sequence Data Type

A sequence is an ordered collection of items, indexed by positive integers. It is a combination of mutable (value can be changed) and immutable (values that cannot be changed).

There are three types of sequence data type such as Strings, Lists, Tuples

Strings

  • Strings are array of bytes representing Unicode characters. In other words, string is defined as a sequence of characters represented in quotation marks (either single (‘) or double quotes (“))
  • A string is a collection of one or more characters put in a single or double quotes or triple quotes

Example

‘python’ 
“python”
“”” python “””
  • In python, there is no character data type, a character is a string of length one
  • It is represented by str class
  • Individual character in a string is accessed using subscript (index). The index should always be an integer (positive or negative). Index starts from 0 and ends at n-1
  • Python will get the input at run time by default as a string
  • Characters can be accessed using indexing and slicing operations
  • Strings are immutable i.e. the contents of the string cannot be changed after it is created

Example

Indexing

  • Square brackets can be used to access elements of the string
  • Positive indexing help in accessing the string from the beginning
  • Negative indexing helps in accessing the string from the end
  • Accessing the strings:
    • To display the first character either the string should be accessed using 0 or –n where n represents the length of the string
      • Str1[0] will display P
      • Str1[-6] will display P
    • To display second character
      • Str1[1] will display Y
      • Str1[-5] will display Y

Operations on strings

  • Indexing [ ]
  • Slicing [:]
  • Concatenation +
  • Repetition *
  • Membership(in, not in)

Creating a String

Creates a string

Example

str1 = "Learning Python"

Indexing

  • Accessing the item in position 0
  • Accessing the item in position 1
  • Accessing the last item

Example

print(str1[0])
L
 print(str1[1])
e
 print(str1[-1])
n

Slicing

  • Display items from position 1 to last
  • Display the items from position 2 to position 5-1
  • When given negative index, starts from -5 th position and printi the string till -1-1
  • Displays all the strings ranging from position 0
  • Displays all the strings ranging from position -6
  • Displays all the characters till position 4-1
  • [:] displays entire string
  • If the first index is greater than or equal to the second index, then empty string is returned
print(str1[1:])
earning Python
print(str1[2:5])
arn
>>> str1 = "Learning Python"
>>> print(str1[-5:-1])
ytho
>>> print(str1[4:])
ning Python
>>> print(str1[-6:])
Python
>>> print(str1[1:9:2])
erig
>>> print(str1[1:9:3])
Eng
>>> str1[:4]
'Lear'
>>> str1[:]
'Learning Python'
>>> str1[3:3]
''

Concatenation (+)

Adding and printing the characters of two strings

>>> print(str1+"is easy")
Learning Pythonis easy
>>> a = "SAVE"
>>> b = "TREES"
>>> print(a+b)
SAVETREES

Repetition (*)

Creates new strings by concatenating multiple copies of the same string

>>> print(str1*2)
Learning PythonLearning Python

Membership operators

  • Using membership operators to check a particular character is in string or not.
  • in-Returns true if present else returns false
  • not in – Returns true if not present else returns false

Syntax

in, not in

Example

>>> "n" in str1
True
>>> "m" in str1
False
>>> "m" not in str1
True

Lists

  • A list can contain items of same type of items or different types of items
  • A list is an ordered and indexable sequence of data items
  • Declare a list: Separate the items using commas and enclose them with square brackets [ ]
  • Operations on list
    • Indexing
    • Slicing
    • Concatenation
    • Repetitions
    • Updation, insertion, deletion

Creating a list

Creating the list with different type of data items

Example

>>> print(first_list)
[1, 'apple', '35.0', 'Mango', '45.0']

Indexing

  • Accessing the item in the position 0
  • Accessing the item in the position 1

Example

>>> print(first_list[0])
1
>>> print(first_list[1])
apple

Slicing

  •  Displays the items from the position 1 till the last
  • Displays the items from the position 1 till 4-1
  • Displays the items from -3 till last
  • Displays the items from position – 4 till position -2-1

Example

>>> print(first_list[1:])
['apple', '35.0', 'Mango', '45.0']
>>> print(first_list[1:4])
['apple', '35.0', 'Mango']
>>> print(first_list[-3:])
['35.0', 'Mango', '45.0']
>>> print(first_list[-4:-2])
['apple', '35.0']

Concatenation

Adding and printing items of two lists

Example

>>> second_list =[2,"red",'40.0',"yellow",32.5] 
>>> print(first_list +second_list)
[1, 'apple', '35.0', 'Mango', '45.0', 2, 'red', '40.0', 'yellow', 32.5]

Repetition

Creates new strings by concatenating multiple copies of the same string

Example

>>> print(second_list*2)
[2, 'red', '40.0', 'yellow', 32.5, 2, 'red', '40.0', 'yellow', 32.5]

Updating the list

Updating the list using index value

Example

>>> second_list[1] = "green"
>>> print(second_list)
[2, 'green', '40.0', 'yellow', 32.5]

Inserting an element

  • Inserting an element in position 5
  • Inserting an eleemnet in position 3

Example

>>> first_list.insert(5,"Fruits")
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', '45.0', 'Fruits']
>>> first_list.insert(3,"guava")
>>> print(first_list)
[1, 'apple', '35.0', 'guava', 'Mango', '45.0', 'Fruits']

Removing an element

Removing an element by giving the element directly

Example

>>> first_list.remove("guava")
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', '45.0', 'Fruits']
>>> first_list.remove('45.0')
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', 'Fruits']

Tuples

  • A tuple is used to store sequence of items
  • A tuple consists of items separated by commas
  • Tuples are enclosed within parentheses rather than within square brackets
  • A tuple is an immutable list i.e. once a tuple is created, one can’t add elements to a tuple or remove elements from the tuple

Advantages

  • Tuples are immutable i.e. it is impossible to add / delete elements to / from a tuple
  • Tuples are faster than lists, because they have a constant set of values
  • Tuples can be used as dictionary keys, because they contain immutable values like strings, numbers

Creating a tuple

Creating tuple with elements of different data types

Example

>>> first_tuple =(1,"one",2.0,"three")
>>> print (first_tuple)
(1, 'one', 2.0, 'three')

Indexing

  • Accessing the item in the position 0
  • Accessing the item in position 3

Example

>>> print(first_tuple[0])
1
>>> print(first_tuple[3])
Three

Slicing

  •  Displaying the items from postion 1 to position 3-1
  • Displaying the items from poston – 4 to -2-1

Example

>>> print(first_tuple[1:3])
('one', 2.0)
>>> print(first_tuple[-4:])
(1, 'one', 2.0, 'three')
>>> print(first_tuple[-4:-2])
(1, 'one')

Concatenation

Adding tuple elements at the end of another tuple elements

Example

>>> second_tuple=(4.0,"four")
>>> print(first_tuple + second_tuple)
(1, 'one', 2.0, 'three', 4.0, 'four')

Repetition

Concatenates multiple copies of same string

Example

>>> print(second_tuple*2)
(4.0, 'four', 4.0, 'four')

List Vs Tuples

Loading

Views: 11

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments