Mbed Technologies
  Learn, Develop, Compete


    Introduction: Welcome to Mbed Blogs!. The periodically submitted blogs in this webpage will discuss more interesting topics on Python programming and its applications in embedded automation. Each blog is written with specific focus, interest, to experiment and dip into different important features of Python programming. We encourage students and professionals to follow the steps exactly written in the blogs, write, modify and compile the programs to gain maximum knowledge out of it. The blogs will be equally useful to both fresh engineering graduates as well as experienced software/firmware professionals.

    Blog Title: In this blog we will study and experiment about the list data type in Python programming. All list features are illustrated over this blog and next blog.

by Admin, Posted on January 19, 2018 at 9:00 PM

     Blog Background     
 Learning Requirement: Basic and Mandatory Requirement of Python Programming.
 Audience Eligibility: Undergraduate engineering students or experienced professionals looking to freshen up programming skills.
 Difficulty Level: Low to Medium.
     Compilation Instructions     
 On windows download and install python 3.4.0 from here

 You can verify your python installation using the following windows command prompt output In windows get a command prompt and execute the commands as shown in the following screen output
C:\Python34> python
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AM D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

 Download and install PyScripter from here
 Edit the Python files using PyScripter and Store your python files in a windows directory
 To make sure PyScripter is using python 3.4, open PyScriptor and go to Tools menu bar and all python 3.4 installations should be listed there.
 To execute a python program write the program using PyScriptor and then click on Run Menu.
  Step 1: To begin with, Let's write a simple python program to define list and name it as list1.py as shown below. Lists are oderderd collection of arbitrary objects or data types. So you can collect different data types using a list. Lists are defined by having square brackets. Each element or value that is inside of a list is called an item. In our list1.py program list Ls contains 3 items of the string data type. List Lm contains items from mixed data type such as a string data type and number data type. List Le is an empty list where as list L4 is defined as another list with 4 items in the list and each is of number data type. List Lss is a list having another list nested in it.


#!/usr/bin/python

def main():
    Ls = ['abc','def','ghi']
    print ('printing a list with string data type')
    print (Ls)
    Lm = ['abc',1]
    print ('printing a list with string and number data type')
    print (Lm)
    Le = []
    print ('printing an empty list')
    print (Le)
    L4 = [10,11,12,13]
    print ('printing a list of 4 items')
    print (L4)
    Lss = ['str',['str1','str2']]
    print ('printing a list with nested sublist')
    print (Lss)

if __name__ == '__main__':
    main()

            

  Step 2: Let's compile the program we wrote in the last step. The output of the program is as shown below.


>>>
printing a list with string data type
['abc', 'def', 'ghi']
printing a list with string and number data type
['abc', 1]
printing an empty list
[]
printing a list of 4 items
[10, 11, 12, 13]
printing a list with nested sublist
['str', ['str1', 'str2']]
>>>
             

  Step 3:Let's think about how we can access items from a given list. Lets write another program and name it as list2.py as shown below. We are accessing elements by using integers. Note that the first element accessing starts from index 0 not from index 1. So if total items in a list is five index to access first item is zero and index to access the last item is 4. In our program list2.py we are accessing the first item of list Ls using index 0 and last item by using index 2. In python list we can also access items using negative indexing so for accessing the last item in list Ls we need index -3 and for accessing 2nd item in list Ls we need index -2 and so on. Next we will experiment accessing items from a nested list. List Lss in our program has nested list and the first item from second list inside Lss can be accessed by using Lss[1][0]. Similarly a 3X3 matrix is defined in python by using list Lm. For example to access the second item in the last list defined inside list Lm we use LM[2][2]. To illustrate the slicing feature we have used list Lv which is sliced from original list Ls. the 1:2 indexing strats from item 1 upto item 2 and excluding item 2. If the slicing starts with a : symbol then it will include all items upto the number after : symbol and excluding the number. So for getting a list to have only first 2items of list Ls we slice using Ls[:2]. However if slicing starts with a number and ends with a : symbol it will extract all items starting with the number till the end of the list. In our program list Ln is sliced from the second item till the end using 1:. One last thing we can use with list is a stride. We declared list num having 11 items. By using 1:9:2 the stride number is 2. The stride value tells the compiler to consider only every alternate item. Number 9 tells compiler up to what index item to consider and excluding the index item. If we use only stride like ::3 as illustrated in list num2 we can extract starting from index 0 and till the end of the list.


#!/usr/bin/python

def main():
    Ls = ['abc','def','ghi']
    print ('Accessing element from list Ls')
    print (Ls[0],Ls[2],Ls[-2],Ls[-3])
    Lss = ['str',['str1','str2']]
    print ('Accessing items from a list with nested sublist')
    print (Lss[0],Lss[1][0])
    Lm = [[10,11,12],[20,21,22],[30,31,32]]
    print ('Accessing items from a 3X3 matrix')
    print (Lm[1],Lm[1][0],Lm[2][2])
    Lv = (Ls[1:2])
    print ('printing List after slicing')
    print (Lv)
    Lr = (Ls[:2])
    print ('printing List after one more slicing')
    print (Lr)
    Ln = (Ls[1:])
    print ('Printing List Ln')
    print (Ln)
if __name__ == '__main__':
    main()


            

  Step 4:The output of program list2.py we wrote in last step is shown below.


>>>
Accessing element from list Ls
abc ghi def abc
Accessing items from a list with nested sublist
str str1
Accessing items from a 3X3 matrix
[20, 21, 22] 20 32
printing List after slicing
['def']
printing List after one more slicing
['abc', 'def']
Printing List Ln
['def', 'ghi']
>>>
            

  Step 5:Lists have no fixed size where as size of the string is fixed. To illustrate this feature lets write another program name it as list3.py as shown below. List Ls has 3items and hence its length is 3 as obtained using len in-built function. Also we can get the content of ist item from list using min function and last item using the max function. You can see in the beginning of the program we declared two lists Ls and Lt where Ls is having 3 string items and Lt having 3 number items. We can concatenate lists like we did in our program for two lists Ls and Lt and finally made a list Lf using + operator. Similarly we can use the repetition feature of list using the * operator. We used * operator for repetition of items of list Ls 4times.


#!/usr/bin/python

def main():
    Ls = ['abc','def','ghi']
    print ('Checking length of list Ls')
    print (len(Ls),min(Ls),max(Ls))
    Lt = [1,2,3]
    print ('Checking length of list Lt')
    Lf = Ls + Lt
    print (Lf)
    print ('Checking length of list Lf')
    print (len(Lf))
    Lm = Ls * 4
    print (Lm)
    print ('Checking length of list Lm')
    print (len(Lm))
if __name__ == '__main__':
    main()

            

  Step 6: The output of the program list3.py we wrote in last step is shown below. Things to observe is that we could concatenate and repeat the items of list and increase the list size at the end.


>>>
Checking length of list Ls
3 abc ghi
Checking length of list Lt
['abc', 'def', 'ghi', 1, 2, 3]
Checking length of list Lf
6
['abc', 'def', 'ghi', 'abc', 'def', 'ghi', 'abc', 'def', 'ghi', 'abc', 'def', 'ghi']
Checking length of list Lm
12
>>>
Checking length of list Ls
3 abc ghi
Checking length of list Lt
['abc', 'def', 'ghi', 1, 2, 3]
Checking length of list Lf
6
['abc', 'def', 'ghi', 'abc', 'def', 'ghi', 'abc', 'def', 'ghi', 'abc', 'def', 'ghi']
Checking length of list Lm
12
>>>