Learn Python the Hard Way

From Hackerspace.gr
Jump to: navigation, search
Learn python the hard way.jpg

external link
Starts Organizer
Mon 23 Nov 2015 18:30
Ends Event Owner
Mon 23 Nov 2015 20:30 User:Ebal
  • Κάθε Δευτέρα, 18:30-20:30
  • Συναντήσεις για εκμάθηση προγραμματισμού σε python.

Θα είναι υπό την μορφή study group, δηλαδή όλοι θα μαθαίνουμε παρέα και δεν θα υπάρχει κάποιος "δάσκαλος". Οι συναντήσεις είναι ανοιχτές σε όλες και όλους - αρκεί να φέρεις το laptop σου! Απευθύνονται σε ανθρώπους που θέλουν να ξεκινήσουν με python κι άρα θα είναι αρκετά εισαγωγικές. Η μοναδική δέσμευση είναι να τηρηθεί αυστηρά το ωράριο (δηλαδή εάν κάποιος ή κάποια αργήσει, να μην μας ζητήσει να γυρίσουμε πίσω για να καλύψουμε κάτι που έχουμε ήδη πει).


  • Στις προηγούμενες συναντήσεις έχουμε καλύψει μέχρι και την:

Exercise 38: Doing Things to Lists

http://learnpythonthehardway.org/book/ex38.html


notes on previous event:

https://www.hackerspace.gr/wiki/Learn_Python_the_Hard_Way_20151116





Exercise 39: Dictionaries, Oh Lovely Dictionaries

a.


39 notes

ebal@mylaptop 39> python2
Python 2.7.10 (default, Sep  7 2015, 13:51:49) 
[GCC 5.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> things = ['a', 'b', 'c', 'd']
>>> print things[1]
b
>>> things[1] = 'z'
>>> print things[1]
z
>>> things
['a', 'z', 'c', 'd']
>>> stuff = { 'name':'Zed"} 
  File "<stdin>", line 1
    stuff = { 'name':'Zed"} 
                           ^
SyntaxError: EOL while scanning string literal
>>> stuff = { 'name':'Zed'} 
>>> stuff = { 'name':'Zed', 'age':39, 'height': 6 * 12 +2 } 
>>> 
>>> stuff
{'age': 39, 'name': 'Zed', 'height': 74}
>>> 
>>> 
>>> stuff['name'] 
'Zed'
>>> stuff[1] 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> stuff[][1] 
  File "<stdin>", line 1
    stuff[][1] 
          ^
SyntaxError: invalid syntax
>>> stuff[1][] 
  File "<stdin>", line 1
    stuff[1][] 
             ^
SyntaxError: invalid syntax
>>> stuff[1][1] 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> stuff[1,1] 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: (1, 1)
>>> stuff[0][1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 0
>>> stuff[0]['name']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 0
>>> stuff['name']
'Zed'
>>> stuff
{'age': 39, 'name': 'Zed', 'height': 74}
>>> 
>>> stuff['city'] = "San Francisco"
>>> stuff
{'city': 'San Francisco', 'age': 39, 'name': 'Zed', 'height': 74}
>>> stuff['city']
'San Francisco'
>>> stuff['city'] = "LA"
>>> stuff
{'city': 'LA', 'age': 39, 'name': 'Zed', 'height': 74}
>>> stuff[0] = "Athens"
>>> stuff
{0: 'Athens', 'city': 'LA', 'age': 39, 'name': 'Zed', 'height': 74}
>>> stuff[False] = 'True'
>>> stuff
{0: 'True', 'city': 'LA', 'age': 39, 'name': 'Zed', 'height': 74}
>>> stuff[True] = 'True'
>>> stuff
{0: 'True', 'city': 'LA', 'name': 'Zed', True: 'True', 'age': 39, 'height': 74}
>>> stuff[True] = 'False'
>>> stuff
{0: 'True', 'city': 'LA', 'name': 'Zed', True: 'False', 'age': 39, 'height': 74}
>>> stuff[1]  
'False'
>>> stuff[0]  
'True'
>>> del stuff['name']
>>> stuff
{0: 'True', 'city': 'LA', True: 'False', 'age': 39, 'height': 74}
>>> stuff[-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: -1
>>> stuff[1]
'False'
>>> stuff
{0: 'True', 'city': 'LA', True: 'False', 'age': 39, 'height': 74}
>>> del stuff[1]
>>> stuff
{
	0: 'True', 
	'city': 'LA', 
	'age': 39, 
	'height': 74
}




b.

#!/usr/bin/python2

# create a mapping of state to abbreviation
states = {
	'Oregon'		: 'OR',
	'Florida'		: 'FL',
	'California'	: 'CA',
	'New York'		: 'NY',
	'Michigan'		: 'MI',
	}

print "states are:"
print type(states)
print id(states)
print states

states2 = states
print id(states2)
print len(states2)

states2 = {'name','ebal'}
print id(states2)


# Create a basic set of states and some cities in them
cities = {
	'CA': 'San Fransisco',
	'MI': 'Detroit',
	'FL': 'Jacksonville',
	}
	
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'

print cities

# print out some cities
print '#' * 15
print "NY state has: ", cities['NY']
print "OR state has: ", cities['OR']

# print some states
print "-" * 10 
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida's abbreviation is: " , states['Florida']

# do it by using the state then cities dict
print '-' * 10 
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]

# print every state abbreviation
# {'California': 'CA', 'Michigan': 'MI', 'New York': 'NY', 'Florida': 'FL', 'Oregon': 'OR'}

"""
 |  items(...)
 |      D.items() -> list of D's (key, value) pairs, as 2-tuples
 |  
"""

print '-' * 10 
for state, abbrev in states.items():
	print "%s is abbreviatd %s" % (state,abbrev)

# print every city in state
# {'FL': 'Jacksonville', 'CA': 'San Fransisco', 'MI': 'Detroit', 'OR': 'Portland', 'NY': 'New York'}
print '-' * 10 
for abbrev, city in cities.items():
	print "%s has the city %s" % (abbrev,city)

# no do both at the same time
print '-' * 10 
for state, abbrev in states.items():
	# 1st iteration 
	# state = 'California'
	# abbrev = 'CA', 
	# cities [ 'CA' ] --> San Fransisco
	print "%s state is abbreviated %s and has city %s" % (
		state, abbrev, cities[abbrev] )

print '-' * 10 
# safely get a abbreviation by state that might not be there
"""
pydoc2 dict
 |  get(...)
 |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
 |
 |      dictionary.get(key) d --> None
 |      dictionary.get(key, d = 'Does not exist' --> d
 
"""
state = states.get('Texas')
print state

if state is None:
	print "Sorry, no Texas."

if not state:
	print "Sorry, no Texas."

# get a city with a default value
city = cities.get('TX', 'Does not exist')
print "The city for the state 'TX' is %s" % city