Sitemap
Gaurav Verma

Gaurav Verma

Member-only story

Sequence in Python

6 min readAug 27, 2024

Sequence is any ordered set in python like List, Tuple, String

  • List: List is mutable and can store any type of object.
  • Tuple: Tuple is immutable and can store any type of object.
  • String: String is sequence which can store only characters e.g. “Gaurav” has characters ‘G’, ‘a’, ‘u’, ‘r’, ‘a’, ‘v’.
Press enter or click to view image in full size
indexing of sequence

Common operations in sequence:

  1. Fetch element using index: mySequence[index] will return element at given index. index is zero based.
str1 = "Gaurav"
print(str1[2]) #output is "u"
list1 = [1, 2, "Gaurav", 38.5, 'v']
print(list1[-1]) #-1 represents the last element. output is 'v'
print(list[-3]) #output is "Gaurav"

2. + : combines two sequences of same data type e.g. [1, 2] + [3, 4] = [1, 2, 3, 4]

str1 = "Gaurav " + "Verma"
print(str1) #output is " Gaurav Verma"

tuple1 = (1, 2, 3) + (4, 5, 6)
print(tuple1) #output is (1, 2, 3, 4, 5, 6)

list1 = [1, 2, 3] + ["4", "5"]
print(list1) #output is [1, 2, 3, "4", "5"}

2. * : repeats the sequence. e.g. 3*”Gaurav” = “GauravGauravGaurav” or
[1, 2]*3 = [1, 2, 1, 2, 1, 2]

str1 = 3*" Gaurav"
print(str1) #output is " Gaurav Gaurav Gaurav"

str2 = "Gaurav "*3
print(str2) #output is "Gaurav Gaurav Gaurav "

tuple1 = (1, 2, 3)*3 #can…

--

--