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

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:30-20:30 * Συναντήσεις για εκμάθηση ...")
 
 
Line 30: Line 30:
 
|location=38.01694322164, 23.731269990513
 
|location=38.01694322164, 23.731269990513
 
}}
 
}}
 +
 +
 +
lists & range
 +
 +
<pre>
 +
εκτός ύλης
 +
 +
list1 = []
 +
 +
blue = "blue"
 +
 +
var3 = [ 'one', 10.0, ['a', 'b'], True, [1 ,2 ,3 ], 'file1', [5, 6, 7], blue ]
 +
 +
 +
var1=[ 1, 2, 3, 4 ]
 +
var2= [ [1,2], [3,4], [5,6] ]
 +
 +
list2 = [ list1, var1, var2, var3 ]
 +
 +
 +
 +
 +
# pydoc2 list
 +
# pydoc2 format
 +
 +
# pydoc2 range
 +
 +
 +
python2
 +
In []: range(10,0,-2)
 +
Out[]: [10, 8, 6, 4, 2]
 +
 +
In []: range(0,6,2)
 +
Out[]: [0, 2, 4]
 +
 +
In []: range(6)
 +
Out[]: [0, 1, 2, 3, 4, 5]
 +
 +
In []: range(10,-4,-2)
 +
Out[]: [10, 8, 6, 4, 2, 0, -2]
 +
 +
 +
</pre>
 +
 +
Exercise 32: Loops and Lists
 +
 +
<pre>
 +
#!/usr/bin/python2
 +
 +
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
 +
 +
 +
# False --> 0 (integer)
 +
the_count = [1, 2, 3, 4, 5, False]
 +
 +
# True ---> True (string)
 +
fruits = ['apples', 'oranges', 'pears', 'apricots', True]
 +
 +
# True --> boolean
 +
change = [ 1, 'pennies', 2 , 'dimes', 3, 'quarters', True]
 +
 +
# this first kind of for-loop goes through a list
 +
for number in the_count:
 +
print "This is count %d" % number
 +
# Den exo idea ti einai auto !
 +
# Telika kati katalava !!
 +
print "This is count {0}".format(number)
 +
print type(number)
 +
print ""
 +
# end of loop
 +
 +
print " - "
 +
 +
# same as above
 +
for fruit in fruits:
 +
print "A fruit of type: %s" %fruit
 +
 +
# also we can go through mixed lists too
 +
# notice we have to use %r since we don't know what's in it
 +
 +
for i in change:
 +
print "I got %r" % i
 +
print type(i)
 +
print ""
 +
 +
# We can also build lists, first start with an empty ony
 +
elements = []
 +
 +
# then use the range function to do 0 to 5 counts
 +
for i in range(0,6):
 +
print "Adding %d to the list. "% i
 +
# append is a function that lists understand
 +
elements.append(i)
 +
 +
# or with more pythonic way !
 +
elements = range(0,6)
 +
elements.append(6)
 +
 +
# append a list [7,8]
 +
ll = [7,8]
 +
elements.append(ll)
 +
 +
# Now we can print them out too
 +
for i in elements:
 +
print "Element was: %d" % i
 +
 +
</pre>
 +
 +
Exercise 33: While Loops
 +
 +
<pre>
 +
 +
#!/usr/bin/python2
 +
 +
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
 +
 +
i = 0
 +
numbers = []
 +
 +
# for i in range(6):
 +
while i < 6:
 +
print "At the top i is %d" % i
 +
numbers.append(i)
 +
 +
i += 1
 +
# i = i + 1
 +
 +
print "Numbers now: ", numbers
 +
print "At the bottom i is %d" % i
 +
 +
print "The numbers: "
 +
 +
for num in numbers:
 +
print num
 +
 +
 +
# attemp
 +
print " - "
 +
numbers = []
 +
i=0
 +
blue = True
 +
while blue:
 +
print numbers
 +
numbers.append(i)
 +
if ( i == 6 ):
 +
blue = False
 +
i += 1
 +
print " - "
 +
 +
# study drill #1
 +
print "The start"
 +
 +
def func1 (i,end, numbers):
 +
if i < end:
 +
numbers.append(i)
 +
print numbers
 +
i += 1
 +
func1(i,end,numbers)
 +
 +
func1(0,6,[])
 +
 +
print "The end"
 +
 +
# func1 ( 1, 6, [0] )
 +
# func1 ( 2, 6, [0, 1] )
 +
# func1 ( 3, 6, [0, 1, 2] )
 +
 +
 +
print " - "
 +
 +
def func1 (i, end, vima, numbers):
 +
if i <> end and i >= 0:
 +
numbers.append(i)
 +
print numbers
 +
i += vima
 +
func1(i, end, vima, numbers)
 +
 +
func1(0, 8, 2, [])
 +
 +
func1(8, 0, -2, [])
 +
 +
func1(7, -2, -2, [])
 +
 +
print " - "
 +
 +
 +
</pre>

Latest revision as of 19:42, 12 October 2015

Learn python the hard way.jpg

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

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


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

Exercise 31: Making Decisions

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


notes on previous event:

https://www.hackerspace.gr/wiki/index.php?title=Learn_Python_the_Hard_Way_20151005





lists & range

εκτός ύλης

list1 = []

blue = "blue"

var3 = [ 'one', 10.0, ['a', 'b'], True, [1 ,2 ,3 ], 'file1', [5, 6, 7], blue ]


var1=[ 1, 2, 3, 4 ]
var2= [ [1,2], [3,4], [5,6] ] 

list2 = [ list1, var1, var2, var3 ] 




# pydoc2 list
# pydoc2 format

# pydoc2 range


python2
In []: range(10,0,-2)
Out[]: [10, 8, 6, 4, 2]

In []: range(0,6,2)
Out[]: [0, 2, 4]

In []: range(6)
Out[]: [0, 1, 2, 3, 4, 5]

In []: range(10,-4,-2)
Out[]: [10, 8, 6, 4, 2, 0, -2]


Exercise 32: Loops and Lists

#!/usr/bin/python2

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


# False --> 0 (integer)
the_count = [1, 2, 3, 4, 5, False]

# True ---> True (string)
fruits = ['apples', 'oranges', 'pears', 'apricots', True]

# True --> boolean
change = [ 1, 'pennies', 2 , 'dimes', 3, 'quarters', True]

# this first kind of for-loop goes through a list
for number in the_count:
	print "This is count %d" % number
	# Den exo idea ti einai auto !
	# Telika kati katalava !! 
	print "This is count {0}".format(number)
	print type(number)
	print ""
	# end of loop

print " - "

# same as above
for fruit in fruits:
	print "A fruit of type: %s" %fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it

for i in change:
	print "I got %r" % i
	print type(i)
	print ""

# We can also build lists, first start with an empty ony
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0,6):
	print "Adding %d to the list. "% i
	# append is a function that lists understand
	elements.append(i)

# or with more pythonic way !
elements = range(0,6)
elements.append(6)

# append a list [7,8]
ll = [7,8]
elements.append(ll)

# Now we can print them out too
for i in elements:
	print "Element was: %d" % i

Exercise 33: While Loops


#!/usr/bin/python2

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

i = 0
numbers = []

# for i in range(6):
while i < 6:
	print "At the top i is %d" % i
	numbers.append(i)
	
	i += 1
	# i = i + 1
	
	print "Numbers now: ", numbers
	print "At the bottom i is %d" % i

print "The numbers: "
	
for num in numbers:
	print num	


# attemp
print " - "
numbers = []
i=0
blue = True
while blue:
	print numbers
	numbers.append(i)
	if ( i == 6 ):
		blue = False
	i += 1
print " - "

# study drill #1
print "The start"

def func1 (i,end, numbers):
	if i < end: 
		numbers.append(i)
		print numbers
		i += 1
		func1(i,end,numbers)

func1(0,6,[])

print "The end"

# func1 ( 1, 6, [0] )
# func1 ( 2, 6, [0, 1] )
# func1 ( 3, 6, [0, 1, 2] )


print " - "

def func1 (i, end, vima, numbers):
	if i <> end and i >= 0: 
		numbers.append(i)
		print numbers
		i += vima
		func1(i, end, vima, numbers)

func1(0, 8, 2, [])

func1(8, 0, -2, [])

func1(7, -2, -2, [])

print " - "