Learn Python the Hard Way

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

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

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

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

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

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


  • 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 0 to 17 (including 17)

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


This is Lesson 05

These events are based on peer2peer education

Exercise 16: Reading and Writing Files


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

# Declare variables: script, filename from argv
# argv is going to take the command line arguments
script, filename = argv

# print something
print "We 're going to erase %r." % filename
print "If you don't want that, hit CTR-C (^C)."
print "If your do want that, hit RETURN."

# Get input from user/keyboard
# This is similar to press any key

print "Opening the file ... "
# Open filename with write operation and get the file descriptor into target
## target = open ( filename, 'w' )
## target = open ( filename, 'a' )
target = open ( filename, 'w+' )

# Truncate file, this means empty the file
## print "Truncating the file. Goodbye!"
## target.truncate()

print "Now I'm going to ask you for three lines."

# Ask from user input three lines
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

# Write them into the file descriptor
target.write ( line1 )
target.write ( "\n" )
target.write ( line2 )
target.write ( "\n" )
target.write ( line3 )
target.write ( "\n" )

string = "%r\n%r\n%r\n" % ( line1, line2, line3 )
target.write ( string )

print "And finally, we close it."
# Close the fd
target.close ()

## Study drills 2.
print ""
print ""
print ""
file = open ( filename ) 
print file.read() 

Exercise 17: More Files


# from python module sys, import section argv
from sys import argv
# from python module os.path, import exists
from os.path import exists
## sys[argv], os.path[exists]

###   import foo                 # foo imported and bound locally
###   import foo.bar.baz         # foo.bar.baz imported, foo bound locally
###   import foo.bar.baz as fbb  # foo.bar.baz imported and bound as fbb
###   from foo.bar import baz    # foo.bar.baz imported and bound as baz
###   from foo import attr       # foo imported and foo.attr bound as attr

# Declare three variables from argv (command line)
script, from_file, to_file = argv

print "Copying from %s to %s " % ( from_file, to_file )

# we could do these two on one line, how ? 
## in_file = open ( from_file ) 
## indata = in_file.read()
indata = open(from_file).read()

# Print the type object of indata
print "type of indata: ", type ( indata )

# print the lenght of the file
print "The input file is %d bytes long " % len(indata)

# print if the file exist of not
print "Does the output file exist ? %r " % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."

# open file with write mode
out_file = open ( to_file, "w")

print "Alright, all done"

# close files
# in_file.close()

## Study drill 2.
out_file = open ( to_file, "w" )
out_file.write( open ( from_file ) . read ( ) )