learn python the hard way
Jerry_Mazeyu
这个作者很懒,什么都没留下…
展开
-
ex1.py 第一个程序
print "hello world!" print "hello again!"print "i like typing this."print "this is fun."print "yay! printing."print "i'd much rather you 'not'."print 'i "said" do not touch this.'原创 2018-05-03 19:45:47 · 188 阅读 · 0 评论 -
ex16.py 读写文件
#coding: utf-8# 本例旨在只做一个简单的文本编辑器,运行这个程序需要输入一个你想创建的文件名,无所谓存在与否,比如:python ex16.py test.txtfrom sys import argv # 常规操作调用script, filename = argv # 注意这个script,在所有的参数里都第一个申明它,以后在使用它时也要作这样的申明print "We'...原创 2018-05-04 22:01:57 · 313 阅读 · 0 评论 -
ex17.py 更多文件和操作
#coding: utf-8# 我们这次试图讲一个文件中的内容复制到另外一个文件中,要运行这个程序要输入两个参数,一个是已经有的txt文件,一个是新的txt文件,比如python ex17.py test.txt new_text.txt (假定我已经有了一个test.txt的情况下)from sys import argv # 常规调用argv,使程序运行参数from os.path im...原创 2018-05-04 22:02:53 · 320 阅读 · 0 评论 -
ex18.py 基本函数使用
#coding: utf-8#这是简单函数的调用,无需输入任何参数,只需要在终端打开它就好了# this one is like your scripts with argvdef print_two(*args): # *args是告诉python把所有的参数都接受进来,然后放到args列表中去。 arg1, arg2 = args print "arg1: %r, arg2: %r"...原创 2018-05-04 22:06:37 · 7025 阅读 · 0 评论 -
ex19.py 函数和变量
#coding: utf-8# 这个脚本主要是为了用各种不同的方式调用函数,并且试图让你弄懂什么是全局变量和函数内部的变量# 下面这部分是函数的申明,它里面的函数变量和后面调用的变量名是不一样的,但是这不影响,因为函数申明时就好像一个指代关系# 就好像说cheese_count, boxes_of_crackers就是函数里面的第一第二个变量,以后调用函数只要在是这个位置的变量就可以,名字完...原创 2018-05-05 16:16:43 · 161 阅读 · 0 评论 -
ex20.py 函数和文件
#coding: utf-8# 这个脚本是为了打印一些文件内容,你在运行时需要一个大于三行的文件作为参数,比如:python ex20.py test.txtfrom sys import argv # 从包中调用argv模块script, input_file = argv # 第一个变量总是script,第二个变量是要输入的文件def print_all(f): # 定义第一个函数...原创 2018-05-05 16:17:43 · 241 阅读 · 0 评论 -
ex21.py 带有返回值的函数
#coding: utf-8# 简单的函数调用,返回一个值def add(a, b): print "ADDING %d + %d" % (a, b) return a + bdef substract(a, b): print "SUBSTRACTING %d - %d" % (a, b) return a - bdef multiply(a, b): print "MUL...原创 2018-05-05 16:18:43 · 197 阅读 · 0 评论 -
ex24.py 更多的练习
#coding: utf-8# 这个程序没什么特别的,就是不断练习以前学过的东西# 第一部分:转义符 \'单引号 \\反斜杠 \n换行 \t缩进符print "Let's practice everything."print "You\'d need to know\'bout escapes with \\ that do \n newlines and \t tabs."# \'这里...原创 2018-05-05 16:20:12 · 183 阅读 · 0 评论 -
ex25.py 定义函数并在shell中调取函数
这个单元不仅仅是编写这样的python程序然后运行这么简单了,这次我们要首先在编译器里面编写一段python代码,然后到shell中去运行。 下面是python程序:#coding: utf-8# 首先我们看到这个程序和其他的程序有所不同在于它并不能运行什么,他只是申明了很多函数,我们要做的仅仅是先把它打出来# 去shell中看看他有没有错,同时在shell中调用它包含的函数。...原创 2018-05-05 16:45:58 · 279 阅读 · 0 评论 -
ex26.py 调试程序
原来有错误的程序:def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return wordsdef sort_words(words): """Sorts the words.""" return sor原创 2018-05-05 20:26:06 · 187 阅读 · 0 评论 -
ex27.txt 逻辑关系
逻辑术语and 与or 或not 非!= 不等于== 等于>= 大于等于<= 小于等于True 真False 假真值表NOT: 与之相反 not False True not True FalseOR: 有一个真即为真 True or False True True or True True False or True Tr...原创 2018-05-06 21:11:04 · 140 阅读 · 0 评论 -
ex28.py 布尔表达式练习
#coding: utf-8print True and True print 2 or "dog" #当这样的情况下返回的若是真则返回第一个操作符,假则返回第二个操作符print 1 == 1 and 1 == 2print 'test' and 'test'print 1 == 1 or 2 != 1, "\n"print True and 1 == 1print False a...原创 2018-05-06 21:12:04 · 197 阅读 · 0 评论 -
ex29.py if语句
#coding: utf-8people = 20cats = 30dogs = 15if False and True: # 看到了吗?if后面跟的是一些判断语句,如果为True就执行,否则就不执行 print "Too many cats! The world is doomed."if people > cats: print "Not many cats! The ...原创 2018-05-06 21:12:50 · 176 阅读 · 0 评论 -
ex15.py 读取文件
#coding: utf-8# 运行这个程序需要一个已经存在的文件,你需要先创建一个txt文件并在里面写入一些内容,比如python ex15.py iamcool.txtfrom sys import argv # sys是一个软件包,这句话的意思是从sys中调用出argv这个模块script, filename = argvtxt = open(filename) # 在txt(变...原创 2018-05-04 22:01:06 · 194 阅读 · 0 评论 -
ex14.py 提示和传递
from sys import argv # 运行这个程序你需要输入一个参数,也就是你的名字,比如:python ex14.py Jerryscript, user_name = argvprompt = '>' print "Hi %s, I'm the %s script." % (user_name, script)print "I'd like to ask you a...原创 2018-05-04 22:00:13 · 148 阅读 · 0 评论 -
ex2.py 注释和#号
#A comment, this is so you can read your program later.#Anything after the # is ignored by python.print "I could have code like this." #and the comment after is ignored#You can also use a comment...原创 2018-05-03 19:46:04 · 135 阅读 · 0 评论 -
ex3.py 数字和数学计算
#coding: utf-8print "I will now count my chickens:" #输入一些语句print "Hens", 25 + 30 / 6 #输出一句话,同时后面接计算出的数字,用“,”连接print "Roosters", 100 - 25 * 3 % 4 #%代表取余数符号print "Now I will count the eggs:" #输出一句...原创 2018-05-03 19:46:33 · 220 阅读 · 0 评论 -
ex4.py 变量和命名
cars = 100 # variablespace_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_ca...原创 2018-05-03 19:46:56 · 112 阅读 · 0 评论 -
ex5.py 更多的变量和打印
#coding: utf-8name = 'Jerry'age = 22 # not a lieheight = 74 # inchesweight = 180 # lbseyes = 'Black'teeth = 'White'hair = 'Black'print "Let's talk about %s." % nameprint "He's %d inches tall...原创 2018-05-03 19:47:10 · 87 阅读 · 0 评论 -
ex9.py 打印,打印,打印
#coding: utf-8# Here's some new strange stuff, remember type it exactly.days = "Mom Tue Wed Thu Fri Sat Sun" #定义一串字符串到days变量中months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" #这里注意转义符\后面加不同字母表示不同含...原创 2018-05-03 21:53:20 · 167 阅读 · 0 评论 -
ex10.py 转移符号
#coding: utf-8tabby_cat = "\tI'm tabbed in." # 引用制表符(tab),即\t代表一个制表符(大空格)persian_cat = "I'm split \non a line." # \n代表换行(new line)backslash_cat = "I'm \\ a \\ cat." # \\代表\的意思fat_cat = """I'll d...原创 2018-05-03 23:06:59 · 193 阅读 · 0 评论 -
ex6.py 字符串和文本
#coding: utf-8x = "There are %d types of people." % 10 #定义x为一串数组变量binary = "binary" #定义binary为字符串“binary”do_not = "don't" y = "Those who know %s and those tho %s." % (binary, do_not) #输出一句话,其中两个单词...原创 2018-05-03 19:47:29 · 135 阅读 · 0 评论 -
ex7.py 更多的打印
#coding: utf-8print "Mary had a little lamb." #输出字符串print "Its fleece was white as %s." % 'snow' #注意snow要加引号,否则就是一个变量,没定义过print "And everywhere that Mary went."print "." * 10 # What that do? #一个点输...原创 2018-05-03 19:47:48 · 93 阅读 · 0 评论 -
ex8.py 打印,打印
#coding: utf-8formatter = "%r %r %r %r" #在这里引号内的内容既可以表示引用格式(即为“待补空位”)也可表示纯文本print formatter % (1, 2, 3, 4) #当作引用格式处理,里面填充数字print formatter % ('one', 'two', 'three', 'four') #将字符串进行填充print formatt...原创 2018-05-03 20:57:16 · 152 阅读 · 0 评论 -
ex11.py 输入的问题
#coding: utf-8print "How old are you?",age = raw_input() # raw_input主要是输入字符串的信息,不管输入什么都是字符串,raw_input(提示信息:)print "How tall are you?",height = raw_input()print "How much do you weight?",weight =...原创 2018-05-04 11:20:28 · 98 阅读 · 0 评论 -
ex12.py 提示别人
# coding: utf-8age = raw_input("How old are you? ")height = raw_input("How tall are you? ")weight = raw_input("How much do you weight? ")print "So you are %r years old, %r cms tall,and %r kgs wei...原创 2018-05-04 21:57:19 · 107 阅读 · 0 评论 -
ex13.py 参数,解包和变量
# coding: utf-8from sys import argv# 运行这个程序你需要三个参数,随便乱编一个吧,比如python ex13.py a b c# 在第一行又一个import语句,这是你将python特性(也称作模块)引入脚本的一种方法。简单的说,这个程序不再是一个可以单独的执行的程序了,而是一个f(x),你必须输入一些参数才能运行它# argv即“参数变量”(argum...原创 2018-05-04 21:58:58 · 340 阅读 · 0 评论 -
ex30.py else和if
people = 30cars = 40buses = 15if cars > people: print "We should take the cars."elif cars < people: print "We should not take the cars."else: print "We can't decide."if buses > car...原创 2018-05-06 21:14:19 · 177 阅读 · 0 评论