Saturday, May 1, 2010

Points To Lose N License

Collections in Python


Previously we had seen some basic data types like Boolean, integer and text strings.

Here is how to use certain types of libraries that offers Python. Lists


Lists are ordered collections of data and are the equivalent of ArrayList or vector in Java or C #.
In Python, lists and collections in general can store any kind of data, like objects, numbers, collections, etc..

This is an example where we see how to use the lists:

 if __name__ == '__main__': # equivalent to public static void main Java 
; list = [] # initialize a list
print ("Todo lo que Guardado en escribas evening a list")
while True:
; data = raw_input (">");
if (as == "fin"):

break # if the agregar final de nuevo a list item
listado.append (data)

if listado.__len__ ()> 0:
print ("These are the input data) for
i in list:
# the comma to print the content
            # una sola fila como un System.out.print de Java
            print i , 
        
    else:
print ("Do not enter anything in the list)

Python has a feature called slicing and for selecting or obtain a partition from the list, for example:
 list = ["h", "o", "l", "a", "", "m", "u" "n "," d "," o "] 
print (list [2:5]) # 1) print ['l', 'a', '' ]
print (list [: 4]) # 2) imprime ['h', 'o', 'l', 'a']
print ( list [5]) # 3) imprime ['m', 'u', 'n', 'd', 'o']
print ( ; list [:]) # 4) imprime ['h', 'o', 'l', 'a', '', ' m ',' u ',' n ',' d ',' o ']
print (list [0:4:2]) # 5) prints ['h', 'l']
print (list [: 2]) # 6) print ['h', 'l', '', 'u', 'd']
print (list [: 3] ) # 7) prints ['h', 'a', 'u', 'o']

If '1 'is obtained a fragment of the arrangement from position 2 through 4. For
'2 ' prints the array from position 0 through 3.
In case '3 'to print the array from position 5 to the last (9).
In case '4 'prints the entire array.
* when you do not specify a number to the left of slicing (:) it defaults to the first position under the right and take the last position. For
'5 'prints from position 0 through 3 every 2 characters.
In the case '6 'prints from position 0 through 9 each 2 characters. For
'7 'prints from position 0 through 9 each 3 characters.



Tuples Tuples are handled exactly like to lists, and the most important characteristics they possess are:
-lists Unlike ([]), tuples are initialized with ().
-are immutable, ie its contents can not be modified.
"They fixed size.
-use less memory than lists.

 list = ["h", "o", "l", "a", "", "m", "u", "n", "d", "o"] 
unaTupla = tuple (list)
print (unaTupla [2:5]) # 1) print ['l', 'a', '' ]



Dictionaries Dictionaries are collections which relate to a key with a value and are the equivalent of Java HashMap.
The main difference between the dictionaries with tuples and lists, is that their values \u200b\u200bare accessed through an index, because they have no order (as the dictionaries are implemented as hash tables ), but by its key using the operator [key]. For this very reason can not apply on these slicing.

This is an example of dictionary:
 numbers = {"one": 1, "two": 2, "three": 3, 4, "four" , 5: 'five', 6: "six"} 
print (numbers ["one"]) # 1)
prints 1 print (numbers [5]) , # 2) prints five
print (numeros.keys ()) # 3) prints [4, 5, 6, 'two', 'three', 'a']
print (numeros.values \u200b\u200b()) # 4) prints ['four', 'five' , 'six', 2, 3, 1]

-In case 1, prints the value obtained from the key 'a'.
-In case 2 print the key value '5 '.
-In case 3 print all the keys in the dictionary.
-In case 4 print all values \u200b\u200bin the dictionary.

0 comments:

Post a Comment