Você está na página 1de 4

***********************************************************************************

***********************************************************************************
Python Notes
***********************************************************************************
***********************************************************************************

###############################################################################

-------------------------------> LIST <-------------------------------------

Arrays in Python are called list:

In Python, we don't allocate pre-defined memory in the list like the way we do
in C. The memory keeps getting allocated as and when we add new element to the
list. one the values are stored in list, the values are accessed in the way
similar to an array.
###############################################################################

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1. define and print 2-d array/list in python
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>>> elist = [[1,2,3], [4,5,6], [7,8,9]]
>>>
>>> print elist[0]
[1, 2, 3]
>>> print elist[2]
[7, 8, 9]
>>> print elist[1]
[4, 5, 6]
>>> print elist[1][2]
6

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2. Using len() function print length of 2-D array>>> print elist
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print len(elist[0])
3
>>> print len(elist[1])
3
>>> print len(elist[2])
3

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3. Print selective elements of 2-D array>>> print elist
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print elist[0][1:3]
[2, 3]
>>> print elist[1][1:3]
[5, 6]
>>> print elist[2][1:3]
[8, 9]

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
4. Add values to the list using append() function:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>>> mylist = []
>>> mylist.append('google')
>>> mylist.append('aricent')
>>> mylist.append('chennai')
>>> print mylist
['google', 'aricent', 'chennai']
>>> print mylist[0]
google
>>> print mylist[1]
aricent
>>> print mylist[2]
chennai

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
4. Delete values from the list using del() function:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>>> mylist = ['tom', 'dick', 'harry']
>>> del mylist[1]
>>> print mylist
['tom', 'harry']
>>> del mylist[1]
>>> print mylist
['tom']
>>> print mylist
['tom']
>>> del mylist[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
5. Find the index corresponding to a value in the list. If the member is not
present
in the list it returns an error.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>>> print mylist
['tom', 'dick', 'harry']
>>> mylist.index('tom')
0
>>> mylist.index('dick')
1
>>> mylist.index('harry')
2
>>> mylist.index('google')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'google' is not in list

###############################################################################
----------------------------------> TUPLES <-----------------------------------
Like numbers and strings, tuples are immutable
###############################################################################

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1. This example shows the immutable property of a tuple.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>>> mylist = (1,2,3,4,5)
>>> mylist = [1,2,3,4,5]
>>> mytuple = (1,2,3,4,5)
>>>
>>>
>>> mylist[1] = 10
>>> mytuple[1] = 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2. Members of Tuples are accessed in same way lists are accessed.

Functions len(), max(), min(), sum(), sorted() can be used in the same way
as they are used on lists.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
>>> mytuple[1]
2
>>> mytuple[0]
1
>>> print mytuple
(1, 2, 3, 4, 5)

###############################################################################
-------------------------------> Dictionary <----------------------------------
In Dictionary, every member in the list would have a key using which we can
invoke correspoinding value in the list.
###############################################################################

>>> print dict


{'tun': 'tunday', 'tu': 'tuesday', 'm': 'monday', 'th': 'thursday', 'w':
'wednesday'}
>>> print['m']
['m']
>>> print['tu']
['tu']
>>> print dict['m']
monday
>>> print dict['tu']
tuesday
>>> print dict['w']
wednesday
>>> print dict['th']
thursday
>>> print dict['tu']
tuesday
>>> print dict['tun']
tunday

>>> print dict


{'tun': 'tunday', 'tu': 'tuesday', 'm': 'monday', 'th': 'thursday', 'w':
'wednesday'}
>>>
>>> dict['new_key'] = 'new_key_added'
>>> print dict
{'th': 'thursday', 'tu': 'tuesday', 'm': 'monday', 'w': 'wednesday', 'tun':
'tunday', 'new_key': 'new_key_added'}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&
Check if a particular key is present in the dictionary:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&
>>> print dict
{'th': 'thursday', 'tu': 'tuesday', 'm': 'monday', 'w': 'wednesday', 'tun':
'tunday', 'new_key': 'new_key_added'}
>>>
>>> 'th' in dict
True
>>>
>>> 'tdd' in dict
False

Você também pode gostar