美文网首页
"Learn Python the Hard Way"学习笔记4

"Learn Python the Hard Way"学习笔记4

作者: los_pollos | 来源:发表于2017-10-25 22:05 被阅读0次

Exercise 17 更多文件操作

from sys import argv
from os.path import exists

script, from_file, to_file = argv

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

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

print "The input file is %d bytes long" % len(indata)

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

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()

执行结果:


output17.png

Exercise 18 名称,变量,代码,函数

#this one is like your scripts with argv
def print_two(*args):
    arg1, arg2 = args
    print "arg1: %r, arg2: %r" % (arg1, arg2)

#ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
    print "arg: %r, arg2: %r" % (arg1, arg2)
    
#this just takes one argument
def print_one(arg1):
    print "arg1: %r" % arg1

#this one take no arguments
def print_none():
    print "I got nothin'."


print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

Exercise 19 函数和变量

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print "You have %d cheeses!" % cheese_count
    print "You have %d boxes of crackers!" % boxes_of_crackers
    print "Man that's enough for a party!"
    print "Get a blanket.\n"


print "We can just give the function numbers directly:"
cheese_and_crackers(20,30)


print "OR, we can use variables from our scripts:"
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese,amount_of_crackers)


print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)


print "And we can combine the two, variable and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

Exercise 20 函数和文件

from sys import argv

script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind(current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

Each time you do f.seek(0),you're moving to the start of the file.
执行结果:


output20.png

Exercise 21 函数返回值

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b
    
def substract(a, b):
    print "SUBSTRACTING %d - %d" % (a, b)
    return a - b 
   
def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b 
    
def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b 
    
    
print "Let's do some math with just functions!"

age = add(30, 5)
height = substract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)


# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."

what = add(age, substract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"

Exercise 22 到目前为止学了什么?

命令行
pwd,cd ~,mkdir,cd ../../,ls,rmdir,pushd,popd,New-Item,cp,mv,more,cat,rm,exit
Exercise 0-22
print,#,%s,%r,%d,""",\n,\t,\\,raw_input,from ... import ...,argv,open,txt.read()
trunate,write,close,def,seek,return

Exercise 23 读一些代码

去bitbucket.org,github.com,gitorious.org读项目代码

相关文章

网友评论

      本文标题:"Learn Python the Hard Way"学习笔记4

      本文链接:https://www.haomeiwen.com/subject/comqpxtx.html