TC1014
martes, 24 de noviembre de 2015
Dictionaries
Dictionaries
are similar to list. They can store many values that can be used when called. The
difference between a dictionary and a list is that every value stored in a
dictionary as mapped to another value.
For
creating a dictionary we use the function dict( ). For stating that something
is dictionary we type the name we want to assign to it followed by the equal
sign. After the equal signs, you start typing the values between curly brackets.
For writing a value it is necessary to type the value, a colon, and the value
it is being mapped to. For separating each set of values we use commas.
As you can
see, when you try to print a dictionary, the values inside of it are printed
without an specific order. For example, if you print them again, the order will
be completely different:
Also, you can get the value is being mapped to another value. Also, you can change the value it is being mapped to:
domingo, 22 de noviembre de 2015
Tuples
A tuple is
a sequence of values that are nested into one function. These elements can be
strings, number, and even tuples. You can have many types of elements within
the same tuple, for example:
Colors = (‘red’,
‘blue’, ’yellow’ )
Numbers = (
1, 2, 3 )
List = ( 4,
(5, 6))
Other = ( ‘red’,
2, (5, 6) )
As you can
see, tuples are pretty much like a list; what differentiates them is that
tuples cannot be mutable. For creating a tuple you need to establish the name
of the tuple followed by an equal sign (which means ‘assignment’). After the
equal sign you start naming the elements included in the list. These elements
are nested inside parenthesis () and separated by commas. If the element being
established is a string, it needs to be between quotation marks.
For
printing the whole tuple you type the command print() and between the parenthesis
you type the name of the tuple. For printing just one element of the tuple you
type the command print() and between the parenthesis you type the name of the
tuple followed by the number of the element between square brackets.
NOTE: The
elements on a tuple are numbered starting from 0
For knowing
how many elements a tuple contains, you can use the command len(), between the
parenthesis you type the name of the tuple.
Also, you can add one tuple
to another and create a new tuple. For that, you need to type the name of the
tuple you are creating followed by an equal sign, followed by the name of the
first tuple and the addition sign followed by the second tuple you will adding.
Lists
A list is a
sequence of values that are nested into one function. These elements can be
strings, number, and even lists. You can have many types of elements within the
same list, for example:
Colors = [ ‘red’,
‘blue’, ’yellow’ ]
Numbers = [
1, 2, 3 ]
List = [ 4,
[5, 6]]
Other = [ ‘red’,
2, [5, 6] ]
As you can
see, for creating a list you need to establish the name on the list followed by
an equal sign (which means ‘assignment’). After the equal sign you start naming
the elements included in the list. These elements are nested inside square
brackets [] and separated by commas. If the element being established is a
string, it needs to be between quotation marks.
For
printing the whole list you type the command print() and between the
parenthesis you type the name of the list. For printing just one element of the
list you type the command print() and between the parenthesis you type the name
of the list followed by the number of the element between square brackets.
NOTE: The
elements on a list are numbered starting from 0
For knowing
how many elements a list contains, you can use the command len(), between the
parenthesis you type the name of the list.
Also, you can change an element on a list by
typing the name of the list followed by position of the element you want to
change between square brackets. After that, you place the equal sign followed
by the element you want to replace it with.
Suscribirse a:
Entradas (Atom)