11 2012 档案
python 零起点(4)
摘要:函数和文件 1 from sys import argv 2 3 script, input_file = argv 4 5 def print_all(f): 6 print f.read() 7 8 def rewind(f): 9 f.seek(0)10 11 def print_a_line(line_count, f):12 print line_count, f.readline()13 14 current_file = open(input_file)15 16 print "First let's print the whole file:\n".
阅读全文
pyhton 零起点(3)
摘要:每次用vim 都要:set nu 挺麻烦的 干脆就直接设置配置文件了 vi ~/.vimrc 添加set nu 就不用那么麻烦了读取文件 1 # linux.txt 文本里面有三行字 2 from sys import argv 3 4 script, filename = argv 5 #打开文件 6 txt = open(filename) 7 8 print "Here's your file %r: " % filename 9 print txt.read() #输出文本内容10 #raw_input打开11 print "Type the fi
阅读全文
python 零起点(2)
摘要:用户输入 1 print "How old are you?", 2 age = raw_input() 3 print "How tall are you?", 4 height = raw_input() 5 print "How much do you weight?", 6 weight = raw_input() 7 8 print "so you're %r old, %r tall and %r heavy." % (age,height,weight) 9 10 #如果把%r 换成 %d 竟
阅读全文
python 零起点(1)
摘要:第一个程序 输出hello world1 #这是一个注释 2 print "Hello world!"数字和数学运算 + - * / % > <1 #输入 'linuxroot 19'2 print "linuxroot", 10 + 18 / 23 4 #输出 False 19 比15大 5 print 10 + 9 < 10 + 56 7 #模运算 取余数 结果是 18 print 5 % 2变量 1 # 下面定义了一些变量 2 3 my_age = 19 4 5 my_name = 'linuxroot
阅读全文
P26
摘要:1 def break_words(stuff): 2 """This function will break up words for us.""" 3 words = stuff.split(' ') 4 return words 5 6 def sort_words(words): 7 """Sorts the words.""" 8 return sorted(words) 9 10 def print_first_word(words):11 &qu
阅读全文
p20 函数和文件
摘要:1 from sys import argv 2 3 script, input_file = argv 4 5 def print_all(f): 6 print f.read() 7 8 def rewind(f): 9 f.seek(0)10 11 def print_a_line(line_count, f):12 print line_count, f.readline()13 14 current_file = open(input_file)15 16 print "First let's print the whole file:\n"17 18..
阅读全文
p18,19 函数
摘要:函数 print_two 的问题是:它并不是创建函数最简单的方法。在 Python 函数中我们可以跳过整个参数解包的过程,直接使用 () 里边的名称作为变量名。这就是 print_two_again 实现的功能。接下来的例子是 print_one ,它向你演示了函数如何接受单个参数。最后一个例子是 print_none ,它向你演示了函数可以不接收任何参数。 1 # this one is like your scripts with argv 2 def print_two(*args): 3 arg1, arg2 = args 4 print "arg1: %r, arg2...
阅读全文
P17 更多文件操作
摘要:exists。这个命令将文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False 1 from sys import argv 2 from os.path import exists 3 4 script, from_file, to_file = argv 5 6 print "Copying from %s to %s" % (from_file, to_file) 7 8 # we could do these two on one line too, how? 9 in_file = open(from_file)10 indata =
阅读全文
p16 读写文件
摘要:如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数)。你应该记住的命令如下:close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。 read – 读取文件内容。你可以把结果赋给一个变量。 readline – 读取文本文件中的一行。 truncate – 清空文件,请小心使用该命令。 write(stuff) – 将stuff写入文件。 16.py 1 from sys import argv 2 3 script, filename = argv 4 5 print "We're going to erase %r."
阅读全文
浙公网安备 33010602011771号