Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (55.6k points)

Can anyone tell me what are the different types of sequences in Python?

1 Answer

0 votes
by (119k points)

There are six types of sequences in Python such as:

Strings- Sequence that stores characters and strings.

Examples:

firstName = 'Lionel'

lastName = "messi"

List – mutable sequence, it is to store a series of homogenous and heterogeneous elements

Examples:

A = [ ] #This is a blank list variable

B = [1, 21, 25, 27] # creates list of 4 numbers.

C = [2, 4, 'abc'] #lists can contain different variable types.

Tuple – immutable sequence, to store series of elements, similar to lists but immutable

Example:

myTuple = ('Lion', 'Tiger', 'Locust', 'Rabbit')

Bytes Sequences - The bytes() function returns an immutable bytes sequence and for mutable byte sequence, use byte array.

Example:

print( bytes([4,2,1]) )

Output:

b’\x04\x02\x01′

Byte Array:

Byte arrays are similar to bytes sequence but these return mutable sequences/

Examples:

a = bytearray([1,3,4,5])

print(a)

a[2] = 2

print(a)

Output:

bytearray(b’\x01\x03\x04\x05′)

bytearray(b’\x01\x03\x02\x05′)

I recommend enrolling in this Python Training Course by Intelllipaat if you are interested to learn Python.

Also, watch this video on Python Sequences:

Related questions

Browse Categories

...