class: center, middle
live presentation
twitter, medium
"A data structure is a particular way of organizing data in a computer so that it can be used effectively."
A python list is a mutable, ordered sequence of items. As such, it can be indexed, sliced, and changed. Each element can be accessed using its position in the list. Each element or value that is inside of a list is called an item
A python list is a mutable, ordered sequence of items. As such, it can be indexed, sliced, and changed. Each element can be accessed using its position in the list. Each element or value that is inside of a list is called an item
#with the list literal []
my_list = [74,75]
names = ['yoni', 'menashe', 'marxus']
stuff = [1, 4, 2, 'Dorong']
nested = [['ori','amit.y','palgi'],['amit.m','omer.s','yoni.y']]
things = [{'name':'alon', 'age':45}]
empty = []
names = ['yoni', 'menashe', 'marxus']
names.append('alon')
print(names)
['yoni', 'menashe', 'marxus', 'alon']
len(names)
4
names[0]
'yoni'
names.append('alon')
len(names)
5
naems.count('alon')
2
--
nums = [0,1, 5, 3, 7]
nums.sort()
print(nums)
nums.reverse()
print(nums)
bool(nums) == True
bool([]) == False
names[0:1]
['yoni']
names[0:2]
['yoni', 'menashe']
is a mutable, unordered set of key-value pairs where each key must be unique. To access a given element, we must refer to it by using its key,
is a mutable, unordered set of key-value pairs where each key must be unique. To access a given element, we must refer to it by using its key,
#with the dict literal {}
my_dict = {"name":"alon","answer":42}
# Is the same as using the dict constructor
another_dict = dict(name='alon',answer=42)
person = {'name':'alon','age':45, 'is_here':True}
print(person)
person['name']
'alon'
len(person)
3
person.keys()
['name', 'age', 'is_here']
person.values(['alon', 45, True])
person['name'] = 'nisser'
person['nicknames'] = ['karpada']
print(person)
{'name': 'nisser', 'age': 45, 'is_here': True, 'nicknames': ['karpada']}
-- We can also update a dict (mutate it) with another dict
jesse = {'username': 'JOctopus', 'online': False, 'points': 723}
jesse.update({'followers': 481})
print(jesse)
{'followers': 481, 'username': 'JOctopus', 'points': 723, 'online': False}
How can we break a dictionary?
-- Missing keys
person = {'name':'alon','age':45, 'is_here':True}
person['surname']
# we'll get a KeyError
# Instead:
person.get('surname')
-- Mutable keys
person = {'name':'alon',[1,2]:'nisser'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Where does the unhashable come from?
A hashing function maps data to a consistent value
See live example with the popular md5 hash
- Note: Hashing != cryptographic or secure
- Hashing != unique values
class: center, middle
Usually we need to iterate on lists
names = ['alon','omer','alon','amit']
for item in names:
print(item)
We can also filter lists
nums = [0,1,2,3,4,5,6,7,8,9]
even_nums = [x for x in nums if x % 2 == 0]
print(even_nums)
[0, 2, 4, 6, 8]
We can also mutate the list this way
nums = [0,1,2,3,4,5,6,7,8,9]
def square(num):
return num * num
square_nums = [square(x) for x in nums]
print(square_nums)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# This also works
square_nums = [x*x for x in nums if x % 2 == 0]
print(square_nums)
[0, 4, 16, 36, 64]
person = dict(name="alon", age=45)
for key, value in person.items():
print(key, value)
- Beginners - how to code in python
- Read more about lists
- Read more about dicts
- Python docs data structures tutorial
- Do this tutorial
- Read some more (see a previous slide)
- Write a python program that gets a value (n) between 2-4 from the user and prints the "n"elements from a predefined array
#Open source rocks!
class: center, middle
#Thanks for listening!