A tuples groups multiple (possiblely with different types) objects. They can be compared to (static) structures of other languages.
In [ ]:
print(type(()))
In [ ]:
help(())
And some timing ... :-)
In [ ]:
!python -m timeit "x = (1, 'a', 'b', 'a')"
In [ ]:
a = (1, 'a', 'b', 'a')
In [ ]:
a.count('a')
In [ ]:
a.index('b')
In [ ]:
a
In [ ]:
a[2] # The 3-rd item
In [ ]:
a[2:1] # Extract the tuple from the 2-nd item to the 1-st one
In [ ]:
a[2:2] # Extract from the 2-nd item to the 2-nd item
In [ ]:
a[2:3] # Extract from the 2-nd item to the 3-rd one
In [ ]:
a[2:4] # Extract one item more
In [ ]:
a[1:] # Extract from the 1-st to the end
In [ ]:
a[:] # Extract all items (a==a[:])
In [ ]:
def return_tuple():
return (1, 'a', 2)
print(return_tuple())
In [ ]:
a = 1; b = 2
print(a, b)
(a, b) = (b, a)
print(a, b)
In [ ]:
a = (1, 'a')
print(id(a),a)
In [ ]:
a += (2,) # This creates a new instance of 'a'
print(id(a),a)
... or be changed:
In [ ]:
a[1] = 2
Tuples are inmutable!
In [ ]:
a = 1; b = 2
print('"a" is in', id(a))
t = (a, b)
print('"t" is in', id(t), 'and contains', t)
a = 3
print('"a" is in', id(a))
print('"t" is in', id(t), 'and contains', t)
A list is a data (usually dynamic) structure that holds a collection of objects, which can have different types. Internally, a list is represented as an array, therefore, lists are fast for appending and for set/pop/getting an item, and slow for removing an internal item.
In [ ]:
help([])
In [ ]:
print(type([]));
In [ ]:
!python -m timeit "x = [1, 'a', 'b', 'a']" # List creation is more expensive than tuple creation
(Tuples are about three times faster)
In [ ]:
a = [] # Empty list definition
In [ ]:
a.append('Hello')
a.append('world!')
a
Python lists can be "promiscuous":
In [ ]:
a.append(100)
a
In [ ]:
a.insert(1,'wave!')
a
In [ ]:
a.remove('Hello')
a
In [ ]:
b = ['a', 'b', 'a']; b.remove('a'); print(b)
In [ ]:
a.pop(0)
In [ ]:
print(a)
a.pop(len(a)-1) # By index, equivalent to "del a[0]"
print(a)
In [ ]:
a = []
a.append('c')
a.append('b')
a.append('a')
a
In [ ]:
a.sort()
a
In [ ]:
a.reverse()
a
In [ ]:
# Indexing
print(a[1]) # Second element
print(a[-1]) # Last element
print(a[100]) # Error
In [ ]:
a+a
In [ ]:
a*3
In [ ]:
a[::-1]
In [ ]:
x = [1,2,3,2,1] # Palindrome?
x[::-1] == x
In [ ]:
a.clear()
a
In [ ]:
a.append('Hello')
print(a, a[0])
In [ ]:
a.append(1)
a.append(('a',2))
a.append('world!')
a
In [ ]:
print(a[1:1], a[1:2], a[1:3], a[1:], a[:], a[1:3:2])
In [ ]:
[x**2 for x in range(10)]
In [ ]:
# http://stackoverflow.com/questions/31045518/finding-prime-numbers-using-list-comprehention
[x for x in range(2, 2000) if all(x % y != 0 for y in range(2, int(x ** 0.5) + 1))]
In [ ]:
l = [[x+y for x in range(10)] for y in range(10)]
l
In [ ]:
l[1][2]
In [ ]:
10 in l
In [ ]:
10 in l[1]
In [ ]:
# Lists of lists
x = [[1,2],[2,3,4],['a']]
x
In [ ]:
l = [2,3]
In [ ]:
id(l)
In [ ]:
l[1] = 4
l
In [ ]:
id(l)
Sets are implemented as hash table of (unordered) objects, therefore sets are good for get/set/delete/searching items and bad for . Sets do not support indexing, slicing, or other sequence-like behavior.
In [ ]:
a = {1, 2, 'a', (1, 2)}
a
In [ ]:
print(type(a))
In [ ]:
help(a)
In [ ]:
a.add('a')
print(a)
In [ ]:
a.add('a')
print(a)
Mutable objects can not be hashed :-(
In [ ]:
a = set()
a.add([1,2]) # Sets can not contain lists
In [ ]:
a = set() # Empty set
a.add({1,2,3}) # Sets can not contain sets
In [ ]:
a = {1,2,3}
b = {2,3,4}
a.intersection(b)
In [ ]:
a.union(b)
In [ ]:
a = set(range(1000))
print(a)
In [ ]:
%timeit '0' in a
In [ ]:
a = list(range(1000))
print(a)
In [ ]:
%timeit '0' in a
Dictionaries are sets where each element (a key) has associated an object (a value). In fact, sets can be seen as dictionaries where the elments have not associations. As sets, dictionaries are efficient for indexing by keys.
In [ ]:
help({})
In [ ]:
a = {'Macinstosh':'OSX', 'PC':'Windows', 'Macintosh-Linux':'Linux', 'PC-Linux':'Linux'}
a
In [ ]:
a['PC']
In [ ]:
'PC-Linux' in a
In [ ]:
a.keys()
In [ ]:
a.values()
In [ ]:
list(a.keys()).index("Macintosh-Linux")
In [ ]:
for i in a.keys():
print(i)
In [ ]:
a['Celullar'] = "Android"
a
In [ ]:
del a['Celullar']
a
In [ ]:
# Modifiying an entry
a.update({"PC": "Windows 10"})
a
In [ ]:
id(a)
In [ ]:
a['Macintosh-Linux'] = 'Linux for the Mac'
a
In [ ]:
id(a)
In [ ]:
for i in a:
print(i, a[i])
In [ ]:
for i in a.values():
print(i)
In [ ]:
for i in a.items():
print(i)
In [ ]:
a = b'hello'
print(type(a))
In [ ]:
print(a)
In [ ]:
chr(a[1])
In [ ]:
b = b'world!'
print('"b" is in', id(b))
c = a + b' ' + b
print(c)
In [ ]:
a = b'abc'
print(id(a))
a += b'efg'
print(id(a))
In [ ]:
%%timeit x = b''
x += b'x'
In [ ]:
%%timeit x = bytearray()
x.extend(b'x')
In [ ]:
# Array of bytes = 0
x = bytearray(10)
In [ ]:
x
In [ ]:
len(x)
In [ ]:
for i in range(len(x)):
x[i] += 1
In [ ]:
x
In [ ]:
# A byte in Python is a 0 <= value <= 255.
x[1] = -1
In [ ]:
# A bytearray can be created from a list
x = bytearray([1,2,3])
x
In [ ]:
import sys
x = bytearray(sys.stdin.read(5).encode())
x
In [ ]:
import array as arr
a = arr.array('d', [1.1, 3.5, 4.5])
print(a)
In [ ]:
a[1]
In [ ]:
a[1] = 4.0
a
In [ ]:
a[1:2]
In [ ]:
a.append(5.0)
a
In [ ]:
a.extend([5.0, 6.0, 7.0])
a
In [ ]:
a += arr.array('d', [1.1, 4.0, 4.5])
a
In [ ]:
# By value
a.remove(4.0)
a
In [ ]:
# By index
a.pop(3)
a
In [ ]:
In [ ]: