In [2]:
x = (1,2,3,0,2,1)
In [3]:
x
Out[3]:
In [4]:
x = (0, 'hola', (1,2))
In [5]:
x
Out[5]:
In [14]:
len(x)
Out[14]:
Una vez que guardan en una tupla datos, pueden accesarlos con indices en brackets. En python los indices comienzan en cero.
In [6]:
x[0]
Out[6]:
In [7]:
x[1]
Out[7]:
In [8]:
x[2]
Out[8]:
In [11]:
x[0:3]
Out[11]:
In [12]:
x[0:2]
Out[12]:
Que tienen de malo las tuplas y porque necesitariamos algomas? Son inmutables.
In [13]:
x[1] = 'adios'
In [15]:
id(x)
Out[15]:
In [16]:
x = (0, 'adios', (1,2))
In [17]:
id(x)
Out[17]:
In [18]:
x = [1,2,3]
In [19]:
x[0] = 'hola'
In [20]:
x
Out[20]:
In [21]:
x.append('nuevo valor')
In [22]:
x
Out[22]:
In [28]:
x.insert(2, 'valor intermedio')
In [31]:
x.pop(0)
Out[31]:
In [32]:
x
Out[32]:
Que es mas rapido, tuplas o listas?
In [37]:
import timeit
timeit.timeit('x = (1,2,3,4,5,6)')
Out[37]:
In [38]:
timeit.timeit('x=[1,2,3,4,5,6]')
Out[38]:
Atencion para los usuarios de R: referencia o asignacion?
In [57]:
x = [1,2,3] # asignacion
In [63]:
y = [0, x] # referencia
In [64]:
y
Out[64]:
In [65]:
x[0] = -1 # asignamos otra lista a x
In [66]:
y
Out[66]:
In [68]:
dir_tel = {'juan': 553423523, 'pedro': 243252345, 'itam':'is fun'}
In [71]:
dir_tel['juan']
Out[71]:
In [72]:
dir_tel['itam']
Out[72]:
In [73]:
dir_tel.keys()
Out[73]:
In [74]:
dir_tel.values()
Out[74]:
In [77]:
A = {1,2,3}
B = {2,3,4}
In [78]:
A | B # union
Out[78]:
In [79]:
A & B # interseccion
Out[79]:
In [80]:
A - B # diferencia de conjuntos
Out[80]:
In [81]:
B - A
Out[81]:
In [82]:
A ^ B # diferencia simetrica
Out[82]:
In [83]:
range(1000)
Out[83]:
In [85]:
for i in range(5): # for (i in 0:(n-1))
print(i)
In [90]:
for i in range(10):
if i % 2 == 0: # operador de modulo (residuo tras division)
print(str(i) + ' par') # str convierte numero en string
else:
print(str(i) + ' impar')
In [91]:
i = 0
while i < 10:
print(i)
i = i + 1
In [92]:
True and True
Out[92]:
In [93]:
True and False
Out[93]:
In [94]:
True or True
Out[94]:
In [95]:
True or False
Out[95]:
In [96]:
2 > 3 or 1 < 2
Out[96]:
In [97]:
(2 > 3) and (1 < 2)
Out[97]:
In [125]:
class Person:
def __init__(self, first, last):
self.first = first # campos
self.last = last
def greet(self, add_msg = ""): # mas metodos
print('hello ' + self.first + ' ' + add_msg)
In [126]:
juan = Person('juan', 'dominguez')
In [127]:
type(juan)
Out[127]:
In [128]:
juan.first
Out[128]:
In [129]:
juan.last
Out[129]:
In [130]:
pedro = Person('pedro', 'gonzalez')
In [131]:
pedro.first
Out[131]:
In [132]:
pedro.last
Out[132]:
In [133]:
pedro.greet('my friend')
In [ ]:
In [ ]: