Learn Python the Hard Way

From Hackerspace.gr
Revision as of 20:17, 27 June 2015 by Ebal (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Learn python the hard way.jpg

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

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

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


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


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

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



  • 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 10 (including 10)


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

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


This is Lesson 04 (intro session)

These events are based on peer2peer education




Exercise 14


# From sys module, import argv
from sys import argv

# declare variables by argv,
# so script would be argv[0]
# and user_name = argv[1]
script, user_name, age = argv
# declare variable prompt to '>'
# prompt = '> '
# prompt = '[PLZ answer to me]> '
prompt = "=> "

# print phrase with two variables
print "Hi %s, I'm the %s script. " % ( user_name, script)
# print question
print "I'd like to ask you a few questions."
# print a question and display variable user_name
print "Do you like me %s?" % user_name
likes = raw_input (prompt) 

# ebal, Mon, 22 Jun 2015 18:26:07 +0300
print "Where do you live %s ? " % user_name
lives = raw_input (prompt) 

print "What kind of computer do you have ?" 
computer = raw_input (prompt)

print "You are %s year old ! " % age

age = raw_input ( "How many years to you work on computers, " + prompt )

print """
Algright, so said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
You are working %s years with computers!!!
""" % (likes, lives, computer, age)

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


   ## output 

> python2 14.py ebal 35 
Hi ebal, I'm the 14.py script. 
I'd like to ask you a few questions.
Do you like me ebal?
=> perphaps
Where do you live ebal ? 
=> nowhere
What kind of computer do you have ?
=> atari
You are 35 year old ! 
How many years to you work on computers, => 15 

Algright, so said 'perphaps' about liking me.
You live in 'nowhere'. Not sure where that is.
And you have a 'atari' computer. Nice.
You are working 15  years with computers!!!



Exercise 15

# from module sys import argv section
from sys import argv

# command line arguments
script, filename = argv

txt = open ( filename ) 

print "Here's your file %r: " % filename

print txt.read()

txt.close()

print "Type the filename again:"

print txt.read()

file_again = raw_input("> ")
txt_again = open ( file_again )
print txt_again.read () 

print "The lines  of the file %r " %  open ( "ex15_sample.txt" ).read ()

print ""

print "The lines  of the file %s " %  open ( "ex15_sample.txt" ).read ()

   ## output 
# python2 dir(txt)



>>> txt = open ('ex15_sample.txt')
>>> 
>>> txt.read() 
'This is stuff I typed into a file.\nIt is really cool stuff.\n'
>>> 
>>> text = txt 
>>> 
>>> text.read() 
''
>>> txt.read () 
''
>>> 
>>> id(txt)
139631426941552
>>> id(text)
139631426941552
>>> text = txt 
>>> txt = open ('ex15_sample.txt')
>>> text.read()
''
>>> txt.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\n'
>>> txt.read()
''
>>> txt = open ('ex15_sample.txt')
>>> id(text)
139631426941552
>>> id(txt)
139631426941408
>>> txt.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\n'
>>> id(txt)
139631426941408
>>> id(txt)
139631426941408
>>> txt.read()
''
>>> text.read()
''
>>> txt == text 
False
>>> txt = open ('ex15_sample.txt')
>>> dir(txt)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>> txt = open ('ex15_sample.txt')
>>> text.read()
''
>>> txt.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\n'
>>> txt.seek(0)
>>> txt.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\n'
>>> txt.close()
>>> txt.seek(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>>