Learn Python the Hard Way

From Hackerspace.gr
Jump to: navigation, search
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

Exercise 21: Functions Can Return Something

#!/usr/bin/python2.7

# def name_of_function (arguments):
    # return value


def ret_str ( a , b ): 
    msg = "%d + %d" % ( a, b )
    # return a value
    return msg

# define function add 

def add ( a , b ): 
    print "ADDING %d + %d " % ( a, b )
    # return a value
    return a + b 

def subtract ( a , b ) :
    print "SUBTRACTING %d - %d " % ( a, b )
    return a - b 

def multiply ( a , b):
    print "MULTIPLYING %d * %d " % ( a, b ) 
    return a * b 

def divide ( a , b ) :
    print "DIVIDING %d / %d " % ( a, b )
    return a / b

print "Let's do some math with just functions!"

# Declare age the return value of function add ( with arguments 30, 5 ) 
age = add ( 30 ,5 )
height = subtract ( 78 , 4 )
weight = multiply ( 90, 2 ) 
iq = divide ( 180, 2.0 )

# print message with arguments 
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % ( age, height, weight, iq )

## A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."

# declare a variable what, the return  of functions
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"

# add two numbers, input from command line
# convert input to integer 
# nested functions, inside out
add ( int ( raw_input ( " ? ") ) , int ( raw_input(" ? ") ) )
 
# test2
what = divide ( age, multiply ( height, subtract ( age, add ( iq, 2 ) ) ) ) 
# print a variable inside print/string without declare the type!!!
print "That becomes: ", what, "Can you do it by hand?"
print "That becomes: %r Can you do it by hand? " % what 

# Print a msg from ret_str function
print ret_str ( 10, 100 )

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