Difference between revisions of "Learn Python the Hard Way 20151116"
From Hackerspace.gr
(Created page with "{{Event |logo=Learn python the hard way.jpg |what=Learn Python the Hard Way |tagline=* Κάθε Δευτέρα, 18:30-20:30 * Συναντήσεις για εκμάθηση ...") |
|||
Line 30: | Line 30: | ||
|location=38.01694322164, 23.731269990513 | |location=38.01694322164, 23.731269990513 | ||
}} | }} | ||
+ | |||
+ | |||
+ | |||
+ | <pre> | ||
+ | |||
+ | #!/usr/bin/python2 | ||
+ | |||
+ | class Thing(object): | ||
+ | def test(hi): | ||
+ | print "hi" | ||
+ | |||
+ | a = Thing() | ||
+ | |||
+ | a.test("hello") | ||
+ | |||
+ | --------------------------------------------------------------------------- | ||
+ | TypeError Traceback (most recent call last) | ||
+ | <ipython-input-3-de9b2a649c54> in <module>() | ||
+ | ----> 1 a.test("hello") | ||
+ | |||
+ | TypeError: test() takes exactly 1 argument (2 given) | ||
+ | |||
+ | |||
+ | # The Fix is to add self as an argument in function test | ||
+ | |||
+ | # class Thing inherit from object | ||
+ | class Thing(object): | ||
+ | # In python we need to add self | ||
+ | def test(self, hi): | ||
+ | print hi | ||
+ | |||
+ | a = Thing() | ||
+ | |||
+ | a.test("hello") | ||
+ | |||
+ | hello | ||
+ | |||
+ | |||
+ | |||
+ | - - - - - | ||
+ | |||
+ | |||
+ | |||
+ | Spades = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] | ||
+ | Hearts = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] | ||
+ | Diamonds = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] | ||
+ | Clubs = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] | ||
+ | |||
+ | Cards = [ Spades, Hearts, Diamonds, Clubs] | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | The exercise: | ||
+ | |||
+ | <pre> | ||
+ | #!/usr/bin/python2 | ||
+ | |||
+ | ten_things2 = "Apples Oranges Crows Telephone Light Sugar" | ||
+ | ten_things = "Apples Oranges Crows Telephone Light Sugar Apples Oranges Crows Telephone Light Sugar" | ||
+ | |||
+ | print "Wait there are not 10 things in that list. Let's fix that." | ||
+ | |||
+ | # pydoc2 string | ||
+ | # split(s, sep=None, maxsplit=-1) | ||
+ | # a list variable | ||
+ | stuff = ten_things.split(' ') | ||
+ | |||
+ | # a list variable | ||
+ | more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"] | ||
+ | |||
+ | # an alternative way | ||
+ | # while len(stuff) != 10: | ||
+ | # while len(stuff) is not 10: | ||
+ | while len(stuff) <> 10: | ||
+ | if len(stuff) > 10: | ||
+ | stuff.pop() | ||
+ | else: | ||
+ | # pydoc2 list | ||
+ | # remove and return item at index (default last) | ||
+ | # tin 1h fora: next_one = the last item of more_stuff, Boy | ||
+ | next_one = more_stuff.pop() | ||
+ | print "Adding: ", next_one | ||
+ | # Add next_one to the end of list stuff | ||
+ | stuff.append(next_one) | ||
+ | print "There are %d items now." % len(stuff) | ||
+ | |||
+ | print "There we go: ", stuff | ||
+ | |||
+ | print "Let's do some things with stuff." | ||
+ | |||
+ | # ten_things = "Apples Oranges Crows Telephone Light Sugar Apples Oranges | ||
+ | # Crows Telephone Light Sugar" | ||
+ | print stuff[1] | ||
+ | print stuff[-1] # whoa! fancy | ||
+ | print stuff.pop() | ||
+ | # pydoc2 str | ||
+ | """ | ||
+ | | join(...) | ||
+ | | S.join(iterable) -> string | ||
+ | | | ||
+ | | Return a string which is the concatenation of the strings in the | ||
+ | | iterable. The separator between elements is S. | ||
+ | """ | ||
+ | print ' '.join(stuff) # what? cool! | ||
+ | print ''.join(stuff) # what? cool! | ||
+ | print ':ebal '.join(stuff) # what? cool! | ||
+ | print "" | ||
+ | print stuff | ||
+ | # pydoc2 slice | ||
+ | # slice(start, stop[, step]) | ||
+ | print stuff[3:5] | ||
+ | print "#".join(stuff[3:5]) # super stellar! | ||
+ | |||
+ | </pre> |
Latest revision as of 20:25, 16 November 2015
external link |
Starts | Organizer |
---|---|---|
Mon 16 Nov 2015 18:30 | ||
Ends | Event Owner | |
Mon 16 Nov 2015 20:30 | User:Ebal |
- Κάθε Δευτέρα, 18:30-20:30
- Συναντήσεις για εκμάθηση προγραμματισμού σε python.
Θα είναι υπό την μορφή study group, δηλαδή όλοι θα μαθαίνουμε παρέα και δεν θα υπάρχει κάποιος "δάσκαλος". Οι συναντήσεις είναι ανοιχτές σε όλες και όλους - αρκεί να φέρεις το laptop σου! Απευθύνονται σε ανθρώπους που θέλουν να ξεκινήσουν με python κι άρα θα είναι αρκετά εισαγωγικές. Η μοναδική δέσμευση είναι να τηρηθεί αυστηρά το ωράριο (δηλαδή εάν κάποιος ή κάποια αργήσει, να μην μας ζητήσει να γυρίσουμε πίσω για να καλύψουμε κάτι που έχουμε ήδη πει).
- Θα ακολουθήσουμε την παρακάτω ύλη: http://learnpythonthehardway.org/book/
- Στις προηγούμενες συναντήσεις έχουμε καλύψει μέχρι και την:
Exercise 37: Symbol Review
http://learnpythonthehardway.org/book/ex37.html
notes on previous event:
https://www.hackerspace.gr/wiki/Learn_Python_the_Hard_Way_20151109
#!/usr/bin/python2 class Thing(object): def test(hi): print "hi" a = Thing() a.test("hello") --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-de9b2a649c54> in <module>() ----> 1 a.test("hello") TypeError: test() takes exactly 1 argument (2 given) # The Fix is to add self as an argument in function test # class Thing inherit from object class Thing(object): # In python we need to add self def test(self, hi): print hi a = Thing() a.test("hello") hello - - - - - Spades = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] Hearts = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] Diamonds = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] Clubs = [1,2,3,4,5,6,7,8,9,10,'J','Q','K'] Cards = [ Spades, Hearts, Diamonds, Clubs]
The exercise:
#!/usr/bin/python2 ten_things2 = "Apples Oranges Crows Telephone Light Sugar" ten_things = "Apples Oranges Crows Telephone Light Sugar Apples Oranges Crows Telephone Light Sugar" print "Wait there are not 10 things in that list. Let's fix that." # pydoc2 string # split(s, sep=None, maxsplit=-1) # a list variable stuff = ten_things.split(' ') # a list variable more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"] # an alternative way # while len(stuff) != 10: # while len(stuff) is not 10: while len(stuff) <> 10: if len(stuff) > 10: stuff.pop() else: # pydoc2 list # remove and return item at index (default last) # tin 1h fora: next_one = the last item of more_stuff, Boy next_one = more_stuff.pop() print "Adding: ", next_one # Add next_one to the end of list stuff stuff.append(next_one) print "There are %d items now." % len(stuff) print "There we go: ", stuff print "Let's do some things with stuff." # ten_things = "Apples Oranges Crows Telephone Light Sugar Apples Oranges # Crows Telephone Light Sugar" print stuff[1] print stuff[-1] # whoa! fancy print stuff.pop() # pydoc2 str """ | join(...) | S.join(iterable) -> string | | Return a string which is the concatenation of the strings in the | iterable. The separator between elements is S. """ print ' '.join(stuff) # what? cool! print ''.join(stuff) # what? cool! print ':ebal '.join(stuff) # what? cool! print "" print stuff # pydoc2 slice # slice(start, stop[, step]) print stuff[3:5] print "#".join(stuff[3:5]) # super stellar!