i

Python Programming

Python List & Tuple

List is generally sequence of items. All items in list don’t need be of same type. While declaring list items separated by commas and must be enclosed with brackets [ [ ] ].

B = [2, 3.3, “python”]  #[] showing list storage

Slicing operator [ ] used to extract item or some range of items from a list. It start with 0.

b = [5,10,15,20,30,22,40]           #[] showing list storage

a[2] = 15

print("a[2] = ", a[2])

MUTABLE LIST : meaning value of elements of a list can be altered.

a = [1,2,3]

a[2]=4            #index start with 0

print(a)

[1, 2, 4]   #output will append 4 in index 3 (start with 0,1,2)

Tuple

It is an ordered sequence of items same as list. The only difference is that tuple is immutable that is once created cannot be modified.

Tuples are mutable: meaning value of elements of a list can be altered.

Shown with () parenthesis. Tuples ared used to write-protect data and are usually faster that lists as they cannot change dynamically. It is defined within parenthesis () where items are separted by commas.

t = (5,’program,1+3j)  # tuples () shown cannot be modified.