This blog assumes that you have a Linux machine with Python already installed.
If you don't, then you can use an AWS cloud based image that has Python. For guidance refer to my blog
I am using Python 2.7 for this tutorial.
LISTS
Lists are Python's arrays.
1. They are MUTABLE; meaning the contents of a list can be changed.
2. They can hold objects of different data types
Example Program-4:
*******************************************************************
#!/bin/python
list1 = [12,45,"Mahatma",'Gandhi',5.6]
print "\n list1 = ", list1
# The list reference starts at 0
print "\n list1[0] =",list1[0]
# This is how you can change the value of a list
list1[2] = 'Mohan Das'
print "\n Updated list1 = ", list1
# This is how you remove an object from the list
# using pop() you can remove 1 item
list1.pop(1)
print "\n Updated list1 after deletion of an item = ", list1
#using del() you can delete a range of items
del list1[1:3]
print "\n Updated list1 after deletion of a few more items = ", list1
# length of the list can be obtains with len()
print "\n Current length of list1 = ",len(list1)
# Extend the list as follows
list1 = list1+[7.6,"Soccer",9]
print "\n list1 after extension = ", list1
# Check this out
list1 = list1 * 3
print "\n latest list1=", list1, "\n"
EXERCISE-2
Write a program that
1. Has two lists.
2. List1 has 5 objects.
3. List2 has 6 objects.
4. Concatenate both lists to form List3
5. Delete the last object from List3.
6. Print List3 and its current length.
7. Delete the middle 3 items from List3.
8. Print List3 and its current length.
9. Compare List1 and List3 and print results.
9. Compare List1 and List3 and print results.
No comments:
Post a Comment