1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Sequences

Sequences

Sequences

After numbers sequences are most used datastructures.

Sequences yield a series of items individually.

The values inside the sequences can be accessed by indexing.

There are three basic sequence types in Pyhton.

#1. list

#2. tuple and

#3. range

 

Mutable vs Immutable

The objects whose value can be changed are called mutable

Examples:

List, dictionary, sets are mutable objects.

Objects whose values can not be changed once they are created are called immutable.

Examples:

strings, integer, tuple, frozenset are immutable

List

List is a kind of collection.

It is a mutable sequence.

The list can contain any type.

The values in the lists are called elements or items.

There are multiple ways to create a new list.

The simplest one is to include items in squre brackets []

Example:

list_nums =[10,20,30,40]

More on list in list section.

 

Tuple

Tuple is like a list, but its immutable sequence.

Like lists a tuple can be created in multiple ways.

The simplest way to create a tuple is to include items in brackets ()

Example:

tuple_number = (10,20,30,40)

 

range()

Range returns an object which produces a sequnce of integers.

Syntax :

range(stop) -> range object

Example:

range(10) -> returns a object which produces sequnce from 0, 9

for i in range(10):

print(i, end=” “)

output:

0 1 2 3 4 5 6 7 8 9

range(start, stop[, step]) -> range object

range(1,20) ->

Returns a object which produces sequence form 1, 19

for i in range(1,20):
print(i, end=” “)

output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

range(1,20,2) ->

Returns an object which produces sequence from 1, 19 with increment step 2

1 3 5 7 9 11 13 15 17 19

range(19,0,-2) ->

Returns an object which produces sequence from 19, 0 with decremented step 2

for i in range(19,0,-2):
print(i, end=” “)

Common sequence operations:

#1. Membership check operations

in and not in

Ex 1:

list_nums =[10,20,30,40]

10 in list_nums

returns True

60 in list_nums

returns False

 

#2. Duplication operations

* is operator which duplicates a sequence.

list_nums * 2

output:

[10, 20, 30, 40, 10, 20, 30, 40]

 

#3. Concatenation operator

+ is concatation operator,

a = [10,20,30]

b = [40,50,60]

a + b

output:

[10, 20, 30, 40, 50, 60]

 

#4.Slicing

Part or full elements of sequence can accessed by slicing

colon with [] (squre brackets) is used as slicing operator.

a = [10,20,30,40,50]

a[1:3]

[20, 30]

a[:-2]

[10, 20, 30]

a[-2:]

[40, 50]

 

#5. operational

len()

Returns the length of the sequence

len(a)

returns 3

max()

Returns maximum value in the sequence

max(a)

output:
30

min()

returns minimum value in the sequence
min(a)

output:
10

 

Even though the examples are based on list.

These holds true for all sequences.

The other sequence types are

strings, Unicode, bytearray, and buffer

List, tuple, sets, strings, unicode, bytearray are covered in later section.