Monday, March 19, 2012

Quick Review to Lists and Tuples.

Lists and Tuples

This Post is a Quick Review to Lists and Tuples, as some readers had confusion differentiating Lists and Tuples. And as it is an important part of Python, so please make the Things clear by practicing it and understanding it as well.

* What is a List?

List is a group of elements and can store elements of any data type. With List we can do Slicing, Appending and many more as discussed earlier.

* How to identify if a given method is List or not?

First, In List, elements are placed within square brackets, and next we have the Python in-built function type(argument) to reveal the type of element the argument holds.

An Example of List with different functions:

>>> a = ['mon', 'tue', 'wed', 12, 12.23]
>>> a
['mon', 'tue', 'wed', 12, 12.23]
>>> type(a)
<type 'list'>
>>> # type() should be something new for you here, basically it returns you the data type
>>> type(a[1])
<type 'str'>
>>> type(a[3])
<type 'int'>
>>> type(a[4])
<type 'float'>
>>> a.append('thurs')
>>> a
['mon', 'tue', 'wed', 12, 12.23, 'thurs']
>>> a.pop(4)
12.23
>>> a
['mon', 'tue', 'wed', 12, 'thurs']
>>> a.remove('wed')
>>> a
['mon', 'tue', 12, 'thurs']
>>> a.append('thurs')
>>> a.append('thurs')
>>> a
['mon', 'tue', 12, 'thurs', 'thurs', 'thurs']
>>> a.count('thurs')
3
>>> len(a)
6
>>> a
['mon', 'tue', 12, 'thurs', 'thurs', 'thurs']
>>> a.insert(2, 'wed')
>>> a
['mon', 'tue', 'wed', 12, 'thurs', 'thurs', 'thurs']
>>> a.reverse()
>>> a
['thurs', 'thurs', 'thurs', 12, 'wed', 'tue', 'mon']
>>> a.sort()
>>> a
[12, 'mon', 'thurs', 'thurs', 'thurs', 'tue', 'wed']
>>> a.extend('sun')
>>> a
[12, 'mon', 'thurs', 'thurs', 'thurs', 'tue', 'wed', 's', 'u', 'n']
>>> a.index('s')
7
>>> a[1:6]
['mon', 'thurs', 'thurs', 'thurs', 'tue']
>>> a
[12, 'mon', 'thurs', 'thurs', 'thurs', 'tue', 'wed']


Okay Played Enough with Lists, now lets Play with Tuples.

* What is a Tuple?

A Tuple is a collection of objects which can hold elements of different Data Type.

* How is it different from List?

Well, Tuple is very Robust, we cannot play with different functions as we did with Lists. The only basic thing that we can do with a Tuple is check the number of times an element exists in a Tuple and check the index number of an element. And Things Like The Length of The Tuple and Slicing

*How to identify if a given method is Tuple or not?

First, In Tuple, elements are placed within round brackets, and next we have the Python in-built function type(argument) to reveal the type of element the argument holds.

Example of Tuple with different functions:

>>> tup = ('mon', 'tue', 'wed', 12, 12.23, 'thurs', 'thurs', 'thurs')
>>> tup
('mon', 'tue', 'wed', 12, 12.23, 'thurs', 'thurs', 'thurs')
>>> type(tup)
<type 'tuple'>
>>>
>>> tup.count('thurs')
3
>>> tup.index('wed')
2
>>> len(tup)
8
>>> tup[1:4]
('tue', 'wed', 12)
>>> tup
('mon', 'tue', 'wed', 12, 12.23, 'thurs', 'thurs', 'thurs')
>>>

Try Playing with the Other Functions and check what Python says. :)

Well that was a Quick Review of Lists and Tuples, there is one more known as Dictionary, which will be discussed on the next post.

I hope This makes all the confusion clear between Lists and Tuples.

Thank You!

0 comments:

Post a Comment