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 in this blog and previous 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: Lists can be updated and items can also be deleted from the list. Lets write another program list4.py as shown below. We deleted the first item of list Ls using del in-built function. So the modified list has only two items. We can append another item in the existing list Ls using .append function and the item will be added at the end of the list. We can add a new item in the list at any index using the .insert function which takes two arguments, the first argument as the number indicating where to add in the existing list and the second the item content. In our program after we added 'mno' in the position having index 1. If we want to remove the last item from list we can use pop function as shown in the program. If we want to reverse the list items without changing the list item contents we can use the reverse function as illustrated in our program.

#!/usr/bin/python

def main():
    Ls = ['abc','def','ghi']
    del[Ls[0]]
    print ('Checking modified list Ls')
    print (Ls)
    Ls.append('jkl')
    print ('Checking modified list after append is Ls')
    print (Ls)
    Ls.insert(1,'mno')
    print ('Checking modified list after insert is Ls')
    print (Ls)
    Ls.pop()
    print ('Checking modified list after pop is Ls')
    print (Ls)
    Ls.reverse()
    print ('Checking modified list after reverse is Ls')
    print (Ls)


if __name__ == '__main__':
    main()


            

  Step 2: The output of the program list4.py we wrote in last step is shown below.

>>>
Checking modified list Ls
['def', 'ghi']
Checking modified list after append is Ls
['def', 'ghi', 'jkl']
Checking modified list after insert is Ls
['def', 'mno', 'ghi', 'jkl']
Checking modified list after pop is Ls
['def', 'mno', 'ghi']
Checking modified list after reverse is Ls
['ghi', 'mno', 'def']
>>>

  Step 3: Next we look at the feature lists are mutable unlike strings. To illustrate this feature lets write another program and name it as list5.py as shown below. We defined a list having 3 string items and we modified strings using the index.

#!/usr/bin/python

def main():
    Lk = ['abc','def','ghi']
    Lk[0] = 'ABC'
    Lk[-2] = 'DEF'
    print ('Checking modified list Lk')
    print (Lk)


if __name__ == '__main__':
    main()


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

>>>
Checking modified list Lk
['ABC', 'DEF', 'ghi']
>>>