Learn Python The Hard Way

来源:转载

Learn Python The Hard Way

This book’s job is to teach you the three most essential skills that a beginning programmer needs to know: reading and writing, attention to detail, and spotting differences

Exercise 0: The Setup

If someone tells you to stop at a specific exercise in this book or to skip certain ones, you should ignore that person. Anyone trying to hide knowledge from you, or worse, make you get it from them instead of through your own efforts, is trying to make you depend on them for your skills. Don’t listen to them and do the exercises anyway so that you learn how to educate yourself.

Exercise 1: A Good First Program

How do I get my country’s language characters into my file?
Make sure you type this at the top of your file: # -- coding: utf-8 --.

some methods useful

Here’s how you memorize things:

Tell yourself you will do it. Don’t try to find tricks or easy ways out of it, just sit down and do it.
Write what you want to memorize on some index cards. Put one half of what you need to learn on one side, then another half on the other side.
Every day for about 15-30 minutes, drill yourself on the index cards, trying to recall each one. Put any cards you don’t get right into a different pile, just drill those cards until you get bored, then try the whole deck and see if you improve.
Before you go to bed, drill just the cards you got wrong for about 5 minutes, then go to sleep.

some references useful
Unix Bash References
The shell you’ve been using is called Bash. It’s not the greatest shell but it’s everywhere and has a lot of features so it’s a good start. Here’s a short list of links about Bash you should go read:

Bash Cheat Sheet
http://cli.learncodethehardway.org/bash_cheat_sheet.pdf created by Raphael and CC licensed.
Reference Manual
http://www.gnu.org/software/bash/manual/bashref.html

Exercise 2: Comments and Pound Characters
#

Exercise 3: Numbers and Math

+ plus- minus/ slash* asterisk% percent< less-than> greater-than<= less-than-equal>= greater-than-equal

Exercise 4: Variables And Names

cars = 100space_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_car = passengers / cars_drivenprint "There are", cars, "cars avaliable."print "There are only", drivers, "drivers avaliable."print "There will be", cars_not_driven, "empty cars today."print "We can transport", carpool_capacity, "people today."print "We have", passengers, "to carpool today."print "We need to put about", average_passengers_per_car, "in each car."

Exercise 5: More Variables and Printing

my_name = 'Zed A. Shaw'my_age = 35 # not a liemy_height = 74 # inchesmy_weight = 180 # lbsmy_eyes = 'Blue'my_teeth = 'White'my_hair = 'Brown'print "Let's talk about %s." % my_nameprint "He's %d inches tall." % my_heightprint "He's %d pounds heavy." % my_weightprint "Actually that's not too heavy."print "He's got %s eyes and %s hair." % (my_eyes, my_hair)print "His teeth are usually %s depending on the coffee." % my_teeth# this line is tricky, try to get it exactly rightprint "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight)

String Formatting Operations

The %s specifier converts the object using str(), and %r converts it using repr().

For some objects such as integers, they yield the same result, but repr() is special in that (for types where this is possible) it conventionally returns a result that is valid Python syntax, which could be used to unambiguously recreate the object it represents.

Here’s an example, using a date:

>>> import datetime>>> d = datetime.date.today()>>> str(d)'2011-05-14'>>> repr(d)'datetime.date(2011, 5, 14)'

Types for which repr() doesn’t produce Python syntax include those that point to external resources such as a file, which you can’t guarantee to recreate in a different context.

function learning
round(number[, ndigits]

分享给朋友:
您可能感兴趣的文章:
随机阅读: