Thursday, December 24, 2020

Lists and Tuples in Python

 

Lists and Tuples in Python

 

 

* A sequence is a datatype that represents a group of elements

* The purpose of any sequence is to store and process a group of elements

* In Python ; strings , lists , tuples and dictionaries are very important type of sequence datatypes

* All sequences allow some common operations like indexing and slicing

 

====================================================================

 

Q) What is a List ?

* A list is similar to an array that consists of a group of elements or items

* As an array , a list can also store elements

* But there is a major difference between an array and a list

* An array can store only one type of element whereas a list can store different types of elements

* Therefore , lists are more versatile and useful than an array

* Lists are the most used among all the datatypes in Python programs

 

====================================================================

 

* Let us consider a case of taking a List in our daily lives ... example , if we take marks of a student in 5

subjects such as :

 

80 , 85 , 82 , 94 , 86

 

then one can see that all of these belong to the same datatype that is an integer type . Therefore , one can

represent such elements as an array . But we need more information about the student , like the roll

number of the student , name , gender , marks etc of the student . In totality , the full information about the

student looks such as this :

 

[ 101,Andy,M,80 , 85 , 82 , 94 , 86 ]

 

Here .. one can see that there are various types of data . Here , the roll number shown for the person is

an integer i.e. 101 . Gender ('M') is a character and the marks shown are again in the form of integers

 

[ 80 ,85 , 82 , 94 , 86 ]

 

In general .. this type of information is stored and processed . This type of information cannot be stored

in an array as the data is heterogeneous in nature and an array stores only homogeneous type of data that

means an array stores only one type of elements .

 

Therefore , in order to store such type of data one needs to use a list datatype . A list can store different types of elements . In order to store the student's information , one can create a list in the following manner :

 

student = [101,'Andy','M',80,85,82,94,86]

 

One may observe that the elements of the 'student' list are stored within square braces [] . One can create

an empty list without any elements by writing empty square braces such as :

 

empty_list = [ ] # this is an empty list

 

Therefore , one can create a list by embedding the elements inside a pair of square braces [ ] . The elements in the list are separated by a comma ( , ) . In order to view the elements of a list as a whole , one can pass the list name to the print() function as in the following manner :

 

print(student)

 

The list appears in the manner below :

[101,'Andy','M',80 , 85 , 82 , 94 , 86]

 

Indexing and slicing operations are common operations that can be done over lists . Indexing represents accessing elements by their position number in the list . The position numbers start from 0 onwards and are written inside square braces as :

 

student[0] , student[1] etc .

 

This means that student[0] represents 0th element , student[1] represents 1st element and so forth . For example , in order to print the student's name one can write :

 

print(student[1])

Output

Andy

 

Slicing represents extracting a piece of the list by mentioning the starting and the ending position numbers . The general format of slicing is [ start : stop : stepsize ]. By default , 'start' will be 0 , 'stop' will be the last element and 'stepsize' value happens to be 1 .

 

Example - student[0:3:1] represents a piece of the list containing 0th to 2nd element of the list .

print(student[0:3:1])

output

[101,'Andy','M']

 

The above statement can also be written in another format as given :

print(student[:3:])

output

print(101,'Andy','M')

 

===================================================================

Python program to create lists with different types of elements

===================================================================

# create lists with integer numbers

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

print(' Total List = ',num)

print(' First = %d , Last = %d ' %(num[0],num[4]))

# create a list with strings

names = ["Andy","Barry","Carrie","Derry"]

print(' Total List =',names)

print(' First = %s , Last =%s' %(names[0],names[3]))

# create a list with different data type of elements

x = [ 10 , 20 , 30 , "Garry" , " Barry " ]

print(' Total List = ', x)

print(' Total List = ', x)

print(' First = %d , Last = %s ' %(x[0],x[5])))

 

Output

Total List = [10,20,30,40,50]

First = 10 , Last = 50

Total List = ["Andy","Barry","Carrie","Derry" ]

First = Andy , Last = Derry

Total List = [ 10 , 20 , 30 , "Garry" , " Barry " ]

First = 10 , Last = Barry

 

No comments:

Post a Comment