Tuesday, August 21, 2012

Exercises 1-5: Starting Zed's tutorial and the basics of Python

I decided the learn Python as I had the perfect application for it. I did a quick Google for something like 'Learn You a Haskell for Great Good' (one of the best tutorials I've ever done) but for Python. Instead, Zed Shaw's Python tutorial came up and after reading the introduction, I thought this would be perfect. I'm a fan of tutorials that make minimal assumptions about how much you know. I tried to learn Fortran by following a tutorial something like 'Fortran for C/Perl programmers' and found that although there was a lot less I could glide through, there was also some things that I didn't end up understanding properly because they were presumably assumed knowledge and as a result I never really stuck with Fortran.

http://learnpythonthehardway.org/book/

Free online! If I make my way through it I'll probably buy the book - US$15 is fairly inexpensive price to pay to learn a new language.

In any case, here are my source files for the first five chapters (after having completed the exercise, so they are more like answers). In some of the first few I did exactly as he told me not to and copy-pasted the lines but meh - I think I learned all the lessons. I suppose we shall see.

ex1.py:
1:  #print "Hello World!"  
2:  #print "Hello Again"  
3:  #print "I like typing this."  
4:  #print "This is fun."  
5:  #print 'Yay! Printing.'  
6:  #print "I'd much rather you 'not'."  
7:  #print 'I "said" do not touch this.'  
8:  print 'another line'  

ex2.py:
1:  # A comment, this is so you can read your program later.  
2:  # Anything after the # is ignored by python.  
3:  print "I could have code like this." # and the comment after is ignored  
4:  # You can also use a comment to "disable" or comment out a piece of code:  
5:  # print "This won't run."  
6:  print "This will run."  

ex3.py:
1:  print "Hens", 25+30/6 # order of ops, and commas result in a single space char  
2:  print "Cocks", 100-25*3%4 # same, but with mult & mod (mult first)  
3:  print "Eggs:"  
4:  print 3+2+1-5+4%2-1/4.0+6 # order of ops, this time showing mod before +  
5:              # and that 1/4 is integer div  
6:  print "3+2<5-7 ?"  
7:  print 3+2<5-7  
8:  print "What is 3 + 2?", 3 + 2  
9:  print "What is 5 - 7?", 5 - 7  
10:  print "Oh, that's why it's False."  
11:  print "Is it greater?", 5 > -2  
12:  print "Is it greater or equal?", 5 >= -2  
13:  print "Is it less or equal?", 5 <= -2  

ex4.py:
1:  cars = 100  
2:  space_in_car = 4  
3:  drivers = 30  
4:  passengers =90  
5:  cars_not_driven = cars - drivers  
6:  cars_driven = drivers  
7:  passenger_cap = cars_driven * space_in_car  
8:  mean_passengers_per_car = passengers / cars_driven  
9:  print "There are", cars, "cars available."  
10:  print "There are only", drivers, "drivers available."  
11:  print "There will be", cars_not_driven, "empty cars today."  
12:  print "We can transport", passenger_cap, "people today."  
13:  print "We have", passengers, "to carpool today."  
14:  print "We need to put about", mean_passengers_per_car, "in each car."  

ex5.py:
1:  my_name = 'poop'  
2:  my_age = 25 # not a lie  
3:  my_height = 180 # cm  
4:  my_weight = 90 # kg  
5:  my_eyes = 'Blue'  
6:  my_teeth = 'White'  
7:  my_hair = 'Brown'  
8:  print "Let's talk about %s." % my_name  
9:  print "He's %d cm tall." % my_height  
10:  print "He's %r kg heavy." % my_weight  
11:  print "Actually that's not too heavy."  
12:  print "He's got %r eyes and %s hair." % (my_eyes, my_hair)  
13:  print "His teeth are usually %s depending on the coffee." % my_teeth  
14:  # this line is tricky, try to get it exactly right  
15:  print "If I multiply %d, %d, and %d I get %d (kg.cm.years)." % (  
16:    my_age, my_height, my_weight, my_age * my_height * my_weight)  

2 comments:

  1. for ex4, did anyone get this:

    PS C:\PyLearn> python .\ex4.py
    Traceback (most recent call last):
    File ".\ex4.py", line 1, in
    c
    NameError: name 'c' is not defined
    PS C:\PyLearn>

    ReplyDelete
    Replies
    1. Hey mate,

      Definitely post your code so I know for sure, but it sounds like you might have a line at the top of your code with something like:
      #! C:\Python.exe

      You'll need that "#!" or else python will think its a variable.

      Delete