Learn Python the Hard Way

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

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

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


Η σελίδα της προηγούμενης συνάντησης με μερικές σημειώσεις:

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


Notes

Στο σημερινό μάθημα, είδαμε πως μπορούμε να εισάγουμε ένα πρόγραμμα στην python μέσω της import

Exercise 25: Even More Practice

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

ακολουθήσαμε "αυστηρά" τον κώδικα του παραπάνω μαθήματος και παρακάτω είναι μερικές σημειώσεις από αυτά που συζητήσαμε εκτός ύλης.

Τυχόν λάθη, βαραίνουν εμένα!

scope

scope of a variable
Είδαμε πως μια μεταβλητή χωρίς να δηλωθεί μπορεί να είναι global στις συναρτήσεις, αλλά δεν μπορεί να αλλάξει τιμή.

""" 
    scope of variable
    you can read a global variable
    but you cant change it inside a variable 
"""

[~]> python2
Python 2.7.10 (default, May 26 2015, 04:16:29) 
[GCC 5.1.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 3
>>> y = 4
>>> print x+y
7
>>> def funcA(): 
...     return x+y
... 
>>> funcA()
7
>>> def funcB():
...     x = 4 
...     y = 6 
...     return (x+y)
... 
>>> funcB()
10
>>> funcA()
7

import

ex25 extra notes:

Το όνομα ενός αρχείου που εισάγουμε στον κώδικά μας μέσω της import δεν μπορεί να ξεκινά με αριθμό.


# import module
""" The name of the import (file/module/package) in python must not start with number. """


reload module

Στην python2 μπορούμε να φορτώσουμε ένα module από την αρχή -εάν έχουμε κάνει αλλαγές στον κώδικα. Στο παρακάτω παράδειγμα φαίνεται η προσθήκη μιας επιπλέον συνάρτησης print_print

https://docs.python.org/2/library/functions.html?highlight=reload#reload


# reload(module)


[~]> python2 
Python 2.7.10 (default, May 26 2015, 04:16:29) 
[GCC 5.1.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25

>>> reload (ex25)
<module 'ex25' from 'ex25.pyc'>
>>> dir ( ex25 ) 
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'break_words', 'print_first_and_last', 'print_first_and_last_sorted', 'print_first_word', 'print_last_word', 'sort_sentence', 'sort_words']


""" Σε αυτό το σημείο, προσθέσαμε στον πηγαίο κώδικα μια ακόμα συνάρτηση """
""" Μετά το reload,θα δείτε και την έξτρα συνάρτηση με όνομα print_print """

>>> reload (ex25)
<module 'ex25' from 'ex25.py'>
>>> dir ( ex25 ) 
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'break_words', 'print_first_and_last', 'print_first_and_last_sorted', 'print_first_word', 'print_last_word', 'print_print', 'sort_sentence', 'sort_words']
>>> 


list Vs tuple

# list Vs tuple


""" String """
>>> sentence = "All good things come to those who wait."
>>> type (sentence)
<type 'str'>


""" list """
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> type(words)
<type 'list'>


""" tuple """
>>> aaaaa=('1', '2')
>>> type(aaaaa)
<type 'tuple'>

help

With help we can create a manual page for every function that have docstrings (""") take a look on the code above

""" help """

[~]> python2 
Python 2.7.10 (default, May 26 2015, 04:16:29) 
[GCC 5.1.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> help(ex25)

Help on module ex25:

NAME
    ex25

FILE
    /home/ebal/Desktop/python/25/ex25.py

FUNCTIONS
    break_words(stuff)
        This function will break up words for us
    
    print_first_and_last(sentence)
        prints the first and last words of the sentence
    
    print_first_and_last_sorted(sentence)
        sorts the words then prints the first and last one


from MODULE import FUNCTION


Import only a function from a module !


# from MODULE import FUNCTION

[~]> python2 
Python 2.7.10 (default, May 26 2015, 04:16:29) 
[GCC 5.1.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ex25 import sort_words
>>> sentence = "All good things come to those who wait."
>>> sort_words(sentence.split(' '))
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']


pydoc2

What pop does in a list

[~]> pydoc2 list

 |  pop(...)
 |      L.pop([index]) -> item -- remove and return item at index (default last).
 |      Raises IndexError if list is empty or index is out of range.


locals

https://docs.python.org/2/library/functions.html?highlight=locals#locals


[~]> python2 
Python 2.7.10 (default, May 26 2015, 04:16:29) 
[GCC 5.1.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import ex25

>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'ex25': <module 'ex25' from 'ex25.pyc'>, '__doc__': None, '__package__': None}