PYTHON PART - 2


Python Lists : Python lists are data structures. They are written in a square closed bracket in which elements are seperated by a comma.In lists, we can write different types of elements can be included which can be a numerical value or a variable.
eg :
L = ["orange", 4, 200, 'apple']
Now look at the following cases :
1) A = B = []
or
2) A = []
B = A
In 1) and 2) , A and B both point to the same lists
Displaying or accessing lists :
You can display or access list through print function :
eg :

~$ python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list = [1,2,'abc']
>>> print list
[1, 2, 'abc']
>>> print list[0]
1
>>> print list[1:2]
[2]
>>> print list[2:2]
[]
>>> print list[1:3]
[2, 'abc']
>>> print list[1]
2
>>> print list[2]
abc





From the above image it is clear that counting starts from left starts with 0 and from right it starts with -1.

Now see the below image :






From the above image operations on list must be clear, One more thing is that it is not necessary is to use function 'print' to display list, you can also follow other way i.e you can write :
listname and it will be displayed.
Methods are as follows :
1) list.append(x) : adds x to end of the list.
2) list.remove(x): Remove the first item from the list whose value is x or shows an error if there is no such item.
3) list.pop([i]) : This operation removes the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
4) list.count(x): Return the number of times x appears in the list.
5) list.sort() :Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
6) list.reverse() : Reverse the elements of the list, in place.
7) list.extend(L) :Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
4) del.list[x] : It deletes the element at index x.
list operations and functions:
list functions are cmp(list1, list2), len(list), max(list), min(list) and operations are such as list1+list2, len([1,2,3)], [1,2,3]*3 etc

these are shown in the following image :




No comments:

Post a Comment

SAY HELLO!!