[关闭]
@BruceWang 2018-01-16T16:47:55.000000Z 字数 9966 阅读 1691

笨办法学python 粗略笔记(learn python the hard way)

python


  1. # _*_ coding: utf_8 _*_
  2. '''
  3. ### ex1
  4. print("ex1 ." * 5 )
  5. print('this is my first print report')
  6. print('Hello World')
  7. print('I don\'\t like typing')
  8. print('\n'.join(' '.join('{}*{}={}'.format(i, j, i*j) for i in range(1, j+1)) for j in range(1, 10)))
  9. ### ex2
  10. print("ex2 ." * 5 )
  11. print('I could have code like this.')
  12. print('this will run.')
  13. print('倒着读代码是一个好的排除错误的方法')
  14. ### ex3
  15. print("ex3 ." * 5 )
  16. print('I will now count my chickens:')
  17. print("Hens,", 25+30/6)
  18. print('Roster,', 100-25*3%4)
  19. print('now, I will count the eggs:')
  20. print(3+21+5-5+4%2-1/4+6)
  21. print("Is it true that 3+2<5-7?")
  22. print(3+2 < 5-7)
  23. print('What is 3+2', 3+2)
  24. print("What is 5-7", 5-7)
  25. print("Oh, that's why it's false.")
  26. print("Is it greater? ", 5>-2)
  27. print("Is it less or equal?", 5<=-2)
  28. print("this is float or int divide, 5%2, 5/2, 5//2: ", 5%2, 5/2, 5//2)
  29. ### ex4
  30. print("ex4 ." * 5 )
  31. print("这是python 编写技巧:")
  32. print("1、在每一行都加上注释, 2、倒着读.py文件, 3、朗读你的.py文件,将每个字母都读出来")
  33. cars = 100
  34. space_in_a_car = 4.0
  35. drivers = 30
  36. passengers = 90
  37. cars_not_driven = cars - drivers
  38. cars_driven = drivers
  39. carpool_capacity = cars_driven * space_in_a_car
  40. average_passengers_per_car = passengers / cars_driven
  41. print("there are", cars, "cars available.")
  42. print("there are only", drivers, "drivers available.")
  43. print("there will be", cars_not_driven, "empty_cars_today")
  44. print("we can transport ", carpool_capacity, "people today")
  45. print("we have", passengers, "to carpool_capacity.")
  46. print("we need to put about", average_passengers_per_car, "in each car.")
  47. ### ex5
  48. print("ex5 ." * 5 )
  49. my_name = 'steve wang'
  50. my_age = 35
  51. my_weight = 128
  52. my_height = 175
  53. my_eyes = 'Blue'
  54. my_teeth = 'White'
  55. my_hair = 'Black'
  56. print("Let's talk about %s." % my_name)
  57. print("He's %d inches tall." % my_height)
  58. print("He's %d pounds heavy." % my_weight)
  59. print("Actually, that's not too heavy")
  60. print("He's got %s eyes and %s hair." % (my_eyes, my_hair))
  61. print("He's teeth are usually %s depending on the coffe." % my_teeth)
  62. print("If Iass %d, %d, and %d I get %d." % (
  63. my_age, my_height, my_weight, my_age + my_height +my_weight))
  64. print("""
  65. # python 格式化字符
  66. # %% 输出百分号
  67. # %d 输出整数
  68. # %c 字符和ASCII码
  69. # %s 字符串
  70. # %u 无符号整数(10)
  71. # %o 无符号整数(16)
  72. # %e 浮点数字(科学记数法)
  73. # %f 浮点数字(小数点)
  74. # %n 存储输出地字符数量放入参数列表中的下一个变量中
  75. # m.n. 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)
  76. # %r 一般用在debug时候
  77. """)
  78. print ('%s, %s'%('one', 'two'))
  79. # one, two
  80. print ('%r, %r'%('one', 'two'))
  81. # 'one', 'two'
  82. ### ex6
  83. print("ex6 ." * 5 )
  84. # round 函数 round(3.32342,2) #3.32 .
  85. round(3.66666, 3)
  86. x = "There are %d types of people." % 10
  87. binary = "binary"
  88. do_not = "don't"
  89. y = "Those who know %s and those who %s." % ( binary, do_not)
  90. print(x)
  91. print(y)
  92. print("I said: %r." % x)
  93. print("I said: %s." % y)
  94. hilarious = False
  95. joke_evaluation = "Isn't that joke so funny?! %r"
  96. print(joke_evaluation % hilarious)
  97. w = "This is the left side of ..."
  98. e = "a string with a right side."
  99. print(w + e)
  100. ### ex7
  101. print("ex7 ." * 5 )
  102. print("Mary had a little lamb")
  103. print("Its fleece was white as %s." % 'snow')
  104. print("." * 10)
  105. end1 = "C"
  106. end2 = "h"
  107. end3 = "e"
  108. end4 = "e"
  109. end5 = "s"
  110. end6 = "e"
  111. print("加逗号来将很长的代码输出来,直接回车也可以")
  112. print(end1 + end2 + end3 + end4 + end5
  113. + end6)
  114. ### ex8
  115. print("ex8 ." * 5 )
  116. formatter = "%r %r %r %r "
  117. print( formatter % (1,2,3,4) )
  118. print( formatter % ("one", "two", "three", "four") )
  119. print( formatter % (formatter, formatter, formatter, formatter) )
  120. print( formatter % (
  121. "I had this thing",
  122. "That you could type up right.",
  123. "But it didn't sing.",
  124. "So I said goodnight 中文测试.")
  125. )
  126. ### ex9
  127. print("ex9 ." * 5 )
  128. days = " Mon Tue Wed Thu Fri Sat Sun "
  129. months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
  130. print("Here are the days:", days)
  131. print("Here are the months:", months)
  132. print("""
  133. There's something going on here.
  134. With the three double-quotes.
  135. We'll be able to type as much as we like.
  136. Even 4 lines if we want, or5, or 6.
  137. """)
  138. print("可以使用\"\"\" \来打印很多行")
  139. ### ex 10
  140. print("ex10 ." * 5 )
  141. print("打印输出多行有两种方法:")
  142. print("""
  143. 1. 使用\\n的方法
  144. 2.使用\"""\
  145. """)
  146. tabby_cat = "\tI'm tabbed in ."
  147. persian_cat = "I'm split\non a line."
  148. backslash_cat = "I'm \\ a \\ cat."
  149. fat_cat = """
  150. I'll do a list:
  151. \t* Cat food
  152. \t* Fishes
  153. \t* Catnip\n\t* Grass
  154. """
  155. print(tabby_cat)
  156. print(persian_cat)
  157. print(backslash_cat)
  158. print(fat_cat)
  159. print("""
  160. \t 是水平制表符
  161. \v 是纵向制表符
  162. \\ 是打印出反斜杠的
  163. \' 是打印单引号的
  164. \" 是打印双引号的
  165. \a 响铃符
  166. \b 是退格符
  167. \f 是换页符
  168. \r 是回车的符号
  169. """)
  170. '''
  171. '''
  172. while True:
  173. for i in ["\"", "-", "|", "\\", "|"]:
  174. print("%r\r" % i,)
  175. break
  176. '''
  177. '''
  178. ### ex11
  179. print("ex11 ." * 5 )
  180. print("如何把需要的数据读入:")
  181. print("How old are you? ")
  182. age = input()
  183. print("How tall are you? "),
  184. height = input()
  185. print("How much do you weight?"),
  186. weight = input()
  187. print("So, you are %r old, %r tall, %r heavy."
  188. % (age, height, weight))
  189. ### ex12
  190. print("ex12 ." * 5 )
  191. age = input("How old are you?")
  192. height = input("How tall are you?")
  193. weight = input("How weight are you?")
  194. print("So, you are %r old, %r tall and %r heavy"
  195. % (age, height, weight))
  196. print("try to execute the order: python -m pydoc input: " )
  197. print("try to understand os sys file open modules' meanings")
  198. print("""just go to the webside
  199. http://blog.csdn.net/sinat_36458870/article/details/76020881
  200. """)
  201. ###ex13
  202. print("ex13 ." * 5)
  203. print("""
  204. now we will try to write a script that can accept a script
  205. """)
  206. from sys import argv
  207. script, first, second, third = argv
  208. print("""
  209. script, first, second, third = argv
  210. this line's function is unpack the first, second, third, to argv
  211. """)
  212. print("please input your age:")
  213. age = input()
  214. print("your are %r age" % age)
  215. print("The script is called: ", script)
  216. print("!!!!!!!!the up line is important, try to understand it ")
  217. print("first :", first)
  218. print("the second script:", second)
  219. print("third script:", third)
  220. ###ex14
  221. print("ex14 ." * 5)
  222. from sys import argv
  223. script, user_name = argv
  224. prompt = '%%%%'
  225. print("Hi, %s, i'm the %s script." % (user_name, script))
  226. print("I'd like to ask you a few questions")
  227. print("Do you like me %s ?" % user_name)
  228. likes = input(prompt)
  229. print("where do you live %s" % user_name)
  230. lives = input(prompt)
  231. print("what kind of computer do you have?")
  232. computer = input(prompt)
  233. print("""
  234. alright, so you said %r about liking me.
  235. you live in %r. not sure where that is.
  236. and you have a %r computer. Nice.
  237. """ % (likes, lives, computer))
  238. '''
  239. ###ex15
  240. '''
  241. print("ex15 ." * 5)
  242. print("this exercise aims to open and close file")
  243. print("ex15 is exactly challenge,please type it one more time")
  244. from sys import argv
  245. script, filename = argv # 这行用argv 导入变量名(filename)
  246. py = open(filename) # 把 filename打开并且赋值给py, 这里py 返回的是一个 file object东西
  247. print("Here is your file %r:" % filename) # 输入你的文件名
  248. print(py.read()) # py.read就是告诉系统hey, read , 把py这个文件给我读出来
  249. py.close()
  250. print("Type the filename again:")
  251. file_again = input(">")
  252. py_again = open(file_again)
  253. print(py_again.read())
  254. py_again.close()
  255. ###ex16
  256. print("ex16 ." * 5)
  257. print("this exercise aims to open, read, readline, write close a file.")
  258. from sys import argv
  259. script, filename = argv
  260. print("we're going to erase %r." % filename)
  261. print("if you don't want that, hit Ctrl + C.")
  262. print("if you want that, hit RETURN")
  263. input("?")
  264. print("opening file ...")
  265. target = open(filename, 'w')
  266. print("Truncating the file. Goodbye!")
  267. print("Now, I'm going to ask you for three lines")
  268. l1 = input("line1: ")
  269. l2 = input("line2: ")
  270. l3 = input("line3: ")
  271. print("I'm going to write these lines to the file.")
  272. target.write(l1)
  273. target.write("\n")
  274. target.write(l2)
  275. target.write("\n")
  276. target.write(l3)
  277. print("And finally, we close it.")
  278. target.close()
  279. from sys import argv
  280. script, filename = argv
  281. print("we are going to read %r" % filename)
  282. f = open(filename, 'r')
  283. print(f.readlines())
  284. f.close()
  285. ###ex17
  286. print("ex17 ." * 5)
  287. from sys import argv
  288. from os.path import exists
  289. script, from_file, to_file = argv
  290. print("copying from %s to %s " % (from_file, to_file))
  291. # in_file = open(from_file)
  292. # indata = in_file.read()
  293. indata = open(from_file).read()
  294. print("The input file is %d bytes long" % len(indata))
  295. print("dose the output file exists ? %r " % exists(to_file))
  296. print("ready, hit RETURN to continue, CTRL-C to abort.")
  297. input()
  298. # out_file = open(to_file, 'w')
  299. # out_file.write(indata)
  300. out_file = open(to_file, 'w').write(indata)
  301. print("alright, all done")
  302. # out_file.close()
  303. # in_file.close()
  304. '''
  305. # from sys import argv
  306. # script, from_file, to_file = argv
  307. # indata = open(from_file).read()
  308. # out_file = open(to_file, 'w').write(open(from_file).read())
  309. # print("done")
  310. '''
  311. ###ex18
  312. print("ex18 ." * 5)
  313. # the one is like your scripts with argv
  314. def print_two(*args):
  315. arg1, arg2 = args
  316. print("arg1: %r, arg2,%r" % (arg1, arg2))
  317. # ok that *args is actually pointless, we can just do this
  318. def print_two_again(arg1, arg2):
  319. print("arg1: %r, arg2: %r" % (arg1, arg2))
  320. # this just takes one argument
  321. def print_one(arg1):
  322. print("arg1: %r" % arg1)
  323. # this one takes no arguments
  324. def print_none():
  325. print("we got nothing")
  326. print_two(1, 2)
  327. print_two('1', '2')
  328. print_two_again(1, 2)
  329. print_one(1)
  330. print_none()
  331. '''
  332. ###ex19
  333. '''
  334. print("ex19 ." * 5)
  335. def cheese_and_crackers(cheese_count, boxes_of_crackers):
  336. print("you have %d cheeses!" % cheese_count)
  337. print("you have %d boxes of crackers!" % boxes_of_crackers)
  338. print("man, that's enough for party!")
  339. print("get the blanket.\n")
  340. print("we can just give the function numbers directly:")
  341. cheese_and_crackers(20, 30)
  342. print("OR, we can use variables from our script:")
  343. amount_of_cheese = 10
  344. amount_of_crackers = 50
  345. cheese_and_crackers(amount_of_cheese, amount_of_crackers)
  346. print("we can even do math inside too:")
  347. cheese_and_crackers(10+20, 5+6)
  348. print("and we can combine the two, variables and math:")
  349. cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
  350. def cheese_and_crackers(cheese_count, boxes_of_crackers):
  351. print("you have %d cheeses!" % cheese_count)
  352. print("you have %d boxes of crackers!" % boxes_of_crackers)
  353. print("man, that's enough for party!")
  354. print("get the blanket.\n")
  355. a = int(input("input what you need"))
  356. b = int(input("input num_of argv"))
  357. cheese_and_crackers(a, b)
  358. '''
  359. ###ex20
  360. '''
  361. print("ex20 ." * 5)
  362. from sys import argv
  363. script, input_file = argv
  364. t = open(input_file, 'w')
  365. l1 = input("l1:")
  366. l2 = input("l2:")
  367. l3 = input("l3:")
  368. t.write(l1)
  369. t.write(l2)
  370. t.write(l3)
  371. print("writing done")
  372. t.close()
  373. def print_all(f):
  374. print(f.read)
  375. def rewind(f):
  376. f.seek(0)
  377. def print_a_line(line_count, f):
  378. print(line_count, f.readline())
  379. current_file = open(input_file)
  380. print("first let's print the whole file:\n")
  381. print_all(current_file)
  382. print("let's rewind, kind of like a tape.")
  383. rewind(current_file)
  384. print("let's print three lines:")
  385. current_line = 1
  386. print_a_line(current_line, current_file)
  387. current_line = current_line + 1
  388. print_a_line(current_line, current_file)
  389. current_line = current_line + 1
  390. print_a_line(current_line, current_file)
  391. ###ex21
  392. print("ex21 ." * 5)
  393. print("函数可以返回东西")
  394. def add(a, b):
  395. print("ADDING %d + %d" % (a, b))
  396. return a+b
  397. def subtract(a, b):
  398. print("SUBTRACTING %d - %d" % (a, b))
  399. return a-b
  400. def multiply(a, b):
  401. print("MULTIPLYING %d * %d" % (a, b))
  402. return a*b
  403. def divide(a, b):
  404. print("DIVIDING %d / %d" % (a, b))
  405. return a/b
  406. print("Let's do some math with just functions")
  407. age = add(30, 5)
  408. height = subtract(78, 4)
  409. weight = multiply(90, 2)
  410. iq = divide(100, 2)
  411. print("Age: %d, height: %d, weight: %d, iq: %d"
  412. % (age, height, weight, iq))
  413. # A puzzle for the extra credit, type it anyway
  414. print("Here is a puzzle")
  415. what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
  416. print("That becomes:", what, "Can you do it by hand")
  417. ###ex22
  418. print("ex22 ." * 5)
  419. print("函数可以返回东西")
  420. ...

转载和疑问声明

如果你有什么疑问或者想要转载,没有允许是不能转载的哈
赞赏一下能不能转?哈哈,联系我啊,我告诉你呢 ~~
欢迎联系我哈,我会给大家慢慢解答啦~~~怎么联系我? 笨啊~ ~~ 你留言也行
你关注微信公众号1.听朕给你说:2.tzgns666,3.或者扫那个二维码,后台联系我也行啦!
tzgns666

终于写完了,赞赏一下下嘛!

(爱心.gif) 么么哒~么么哒~么么哒
爱心从我做起,贫困山区捐衣服,为开源社区做贡献!

码字不易啊啊啊,如果你觉得本文有帮助,三毛也是爱!真的就三毛,呜呜。。。

我祝各位帅哥,和美女,你们永远十八岁,嗨嘿嘿~~~

weiChat
Alibaba

我祝各位帅哥,和美女,你们永远十八岁,嗨嘿嘿~~~

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注