Difference between revisions of "Learn Python the Hard Way 08"

From Hackerspace.gr
Jump to: navigation, search
(Created page with "{{Event |logo=Learn python the hard way.jpg |what=Learn Python the Hard Way |tagline= * Κάθε Δευτέρα, 18:00-20:00 * Συναντήσεις για εκμάθησ...")
 
Line 21: Line 21:
 
* Στις προηγούμενες συναντήσεις έχουμε καλύψει μέχρι και την:
 
* Στις προηγούμενες συναντήσεις έχουμε καλύψει μέχρι και την:
 
   
 
   
'''Exercise 17: More Files'''
+
'''Exercise 21: Functions Can Return Something'''
  
  
Line 45: Line 45:
  
  
* We have previously covered exercises till 17 (including '''Exercise 17: More Files''')  
+
* We have previously covered exercises till 21 (including '''Exercise 21: Functions Can Return Something''')  
  
  
Line 58: Line 58:
  
 
<pre>
 
<pre>
 +
 +
#!/usr/bin/python2.7
 +
 +
# from module sys, import section argv
 +
from sys import argv
 +
 +
# list of arguments from command line
 +
# script <--- argv[0], input_file <--- argv[1]
 +
# argv[0]: Is always the name of the script
 +
script, input_file = argv
 +
 +
# funtion print_all
 +
# takes argument a variable called: f
 +
def print_all ( f ):
 +
    print f.read()
 +
 +
# function rewind
 +
# takes argument a variable called: f
 +
def rewind ( f ):
 +
    f.seek ( 0 )
 +
 +
# function print_a_line
 +
# takes two arguments: line_count and f
 +
def print_a_line ( line_count, f ):
 +
    print line_count, f.readline()
 +
 +
# Variable
 +
current_file = open ( input_file )
 +
print type ( current_file )
 +
 +
# print msg
 +
print "First let's print the whole file: \n"
 +
 +
# run function, with argument: current_file
 +
print_all ( current_file )
 +
 +
print "Now let's rewind, kind of like a tape."
 +
 +
# run function, with argument: current_file
 +
rewind ( current_file )
 +
 +
print "Let's print three lines:"
 +
 +
# set variable current_line to 1
 +
current_line = 1
 +
 +
# run function print_a_line, put two arguments current_line, current_file
 +
print_a_line ( current_line, current_file )
 +
 +
# variable
 +
current_line = current_line + 1
 +
# run function print_a_line, put two arguments
 +
print_a_line ( current_line, current_file )
 +
 +
# variable
 +
current_line += 1
 +
# run function
 +
print_a_line ( current_line, current_file )
 +
 +
# WRONG - DONT DO IT !
 +
current_line =+ 1
 +
# run function
 +
print_a_line ( current_line, current_file )
 +
 +
 +
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
 +
 +
 
</pre>
 
</pre>
  

Revision as of 18:21, 20 July 2015

Learn python the hard way.jpg

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

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

Έλα εγκαίρως/νωρίς αν θες να βρεις ελεύθερη θέση :)


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

Exercise 21: Functions Can Return Something


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

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



  • Every Monday at 18.00-20:00
  • python enthusiasts will try to learn programming "the hard way" !

The meetings will be in study group format, meaning that we will all learn together and there won't be a tutor. Meetings are open to everyone - just bring your laptop! They are for people that want to start learning python, so we will cover some introductory material. The only requirement is to be punctual (so if a person comes later than 18:00 please don't ask us to cover material that we have already gone through).

Come early if you want to ensure that you'll find a space to place your laptop :)


  • We have previously covered exercises till 21 (including Exercise 21: Functions Can Return Something)


The page for the previous meeting (#07) with some notes:

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

These events are based on peer2peer education


These notes are from Lesson 08


#!/usr/bin/python2.7

# from module sys, import section argv
from sys import argv

# list of arguments from command line
# script <--- argv[0], input_file <--- argv[1]
# argv[0]: Is always the name of the script
script, input_file = argv

# funtion print_all
# takes argument a variable called: f
def print_all ( f ):
    print f.read()

# function rewind
# takes argument a variable called: f
def rewind ( f ):
    f.seek ( 0 )

# function print_a_line 
# takes two arguments: line_count and f
def print_a_line ( line_count, f ):
    print line_count, f.readline()

# Variable
current_file = open ( input_file )
print type ( current_file )

# print msg
print "First let's print the whole file: \n"

# run function, with argument: current_file
print_all ( current_file )

print "Now let's rewind, kind of like a tape."

# run function, with argument: current_file
rewind ( current_file ) 

print "Let's print three lines:"

# set variable current_line to 1 
current_line = 1 

# run function print_a_line, put two arguments current_line, current_file
print_a_line ( current_line, current_file )

# variable
current_line = current_line + 1 
# run function print_a_line, put two arguments 
print_a_line ( current_line, current_file )

# variable
current_line += 1 
# run function
print_a_line ( current_line, current_file ) 

# WRONG - DONT DO IT !
current_line =+ 1 
# run function
print_a_line ( current_line, current_file ) 


# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4