楼主: libgy8
3899 13

Learn Python the Hard Way 3rd Edition [推广有奖]

  • 0关注
  • 0粉丝

已卖:529份资源

大专生

96%

还不是VIP/贵宾

-

威望
0
论坛币
7437 个
通用积分
25.4977
学术水平
4 点
热心指数
4 点
信用等级
2 点
经验
571 点
帖子
38
精华
0
在线时间
72 小时
注册时间
2009-11-24
最后登录
2019-4-10

楼主
libgy8 发表于 2014-11-15 02:49:53 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  • Learn Python the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code, Third Edition, Video-Enhanced Edition
  • By: Zed A. Shaw

  • Publisher: Addison-Wesley Professional

  • Pub. Date: October 1, 2013

  • Print ISBN-10: 0-321-88491-4

  • Print ISBN-13: 978-0-321-88491-6

  • Web ISBN-10: 0-13-312431-2

  • Web ISBN-13: 978-0-13-312431-6

  • Pages in Print Edition: 320

  • Subscriber Rating: [9 Ratings] Subscriber Reviews

  • 本帖隐藏的内容

    Learn Python the Hard Way, 3rd Edition.pdf (3.94 MB, 需要: 50 个论坛币)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Edition dition editio python Learn python

已有 1 人评分论坛币 学术水平 热心指数 收起 理由
crystal8832 + 24 + 2 + 2 奖励积极上传好的资料

总评分: 论坛币 + 24  学术水平 + 2  热心指数 + 2   查看全部评分

本帖被以下文库推荐

沙发
lonestone(未真实交易用户) 在职认证  发表于 2014-11-15 05:51:16 来自手机
  1. 1    age = raw_input("How old are you? ")
  2. 2    height = raw_input("How tall are you? ")
  3. 3    weight = raw_input("How much do you weigh? ")
  4. 4
  5. 5    print "So, you're %r old, %r tall and %r heavy." % (
  6. 6        age, height, weight)
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

藤椅
fjrong(未真实交易用户) 在职认证  发表于 2014-11-15 06:10:59 来自手机
  1. 1    from sys import argv
  2. 2
  3. 3    script, first, second, third = argv
  4. 4
  5. 5    print "The script is called:", script
  6. 6    print "Your first variable is:", first
  7. 7    print "Your second variable is:", second
  8. 8    print "Your third variable is:", third

  9. On line 1 we have what’s called an “import.” This is how you add features to your script from the Python feature set. Rather than give you all the features at once, Python asks you to say what you plan to use. This keeps your programs small, but it also acts as documentation for other programmers who read your code later.
  10. The argv is the “argument variable,” a very standard name in programming that you will find used in many other languages. This variable holds the arguments you pass to your Python script when you run it. In the exercises you will get to play with this more and see what happens.
  11. Line 3 “unpacks” argv so that, rather than holding all the arguments, it gets assigned to four variables you can work with: script, first, second, and third. This may look strange, but “unpack” is probably the best word to describe what it does. It just says, “Take whatever is in argv, unpack it, and assign it to all these variables on the left in order.”
  12. After that, we just print them out like normal.
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

板凳
bndnsuy(未真实交易用户) 发表于 2014-11-16 10:45:25
  1. 1    from sys import argv
  2. 2
  3. 3    script, user_name = argv
  4. 4    prompt = '> '
  5. 5
  6. 6    print "Hi %s, I'm the %s script." % (user_name, script)
  7. 7    print "I'd like to ask you a few questions."
  8. 8    print "Do you like me %s?" % user_name
  9. 9    likes = raw_input(prompt)
  10. 10
  11. 11   print "Where do you live %s?" % user_name
  12. 12   lives = raw_input(prompt)
  13. 13
  14. 14   print "What kind of computer do you have?"
  15. 15   computer = raw_input(prompt)
  16. 16
  17. 17   print """
  18. 18   Alright, so you said %r about liking me.
  19. 19   You live in %r.  Not sure where that is.
  20. 20   And you have a %r computer.  Nice.
  21. 21   """ % (likes, lives, computer)

  22. Notice though that we make a variable prompt that is set to the prompt we want, and we give that to raw_input instead of typing it over and over. Now if we want to make the prompt something else, we just change it in this one spot and rerun the script.
  23. Very handy.
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

报纸
zombiemummy(未真实交易用户) 发表于 2014-11-18 14:03:44
  1. 1    from sys import argv
  2. 2
  3. 3    script, filename = argv
  4. 4
  5. 5    txt = open(filename)
  6. 6
  7. 7    print "Here's your file %r:" % filename
  8. 8    print txt.read()
  9. 9
  10. 10   print "Type the filename again:"
  11. 11   file_again = raw_input("> ")
  12. 12
  13. 13   txt_again = open(file_again)
  14. 14
  15. 15   print txt_again.read()

  16. A few fancy things are going on in this file, so let’s break it down real quick:
  17. Lines 1–3 should be a familiar use of argv to get a filename. Next we have line 5 where we use a new command open. Right now, run pydoc open and read the instructions. Notice how like your own scripts and raw_input, it takes a parameter and returns a value you can set to your own variable. You just opened a file.
  18. Line 7 we print a little line, but on line 8 we have something very new and exciting. We call a function on txt. What you got back from open is a file, and it’s also got commands you can give it. You give a file a command by using the . (dot or period), the name of the command, and parameters. Just like with open and raw_input. The difference is that when you say txt.read() you are saying, “Hey txt! Do your read command with no parameters!”
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

地板
DreamSinger(未真实交易用户) 发表于 2014-11-18 15:03:19
  1. 1    from sys import argv
  2. 2
  3. 3    script, filename = argv
  4. 4
  5. 5    print "We're going to erase %r." % filename
  6. 6    print "If you don't want that, hit CTRL-C (^C)."
  7. 7    print "If you do want that, hit RETURN."
  8. 8
  9. 9    raw_input("?")
  10. 10
  11. 11   print "Opening the file..."
  12. 12   target = open(filename, 'w')
  13. 13
  14. 14   print "Truncating the file.  Goodbye!"
  15. 15   target.truncate()
  16. 16
  17. 17   print "Now I'm going to ask you for three lines."
  18. 18
  19. 19   line1 = raw_input("line 1: ")
  20. 20   line2 = raw_input("line 2: ")
  21. 21   line3 = raw_input("line 3: ")
  22. 22
  23. 23   print "I'm going to write these to the file."
  24. 24
  25. 25   target.write(line1)
  26. 26   target.write("\n")
  27. 27   target.write(line2)
  28. 28   target.write("\n")
  29. 29   target.write(line3)
  30. 30   target.write("\n")
  31. 31
  32. 32   print "And finally, we close it."
  33. 33   target.close()

  34. That’s a large file—probably the largest you have typed in. So go slow, do your checks, and make it run. One trick is to get bits of it running at a time. Get lines 1–8 running, then five more, then a few more, and so on, until it’s all done and running.
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

7
lystc(未真实交易用户) 发表于 2014-11-18 15:25:25 来自手机
  1. 1   from sys import argv
  2. 2   from os.path import exists
  3. 3
  4. 4   script, from_file, to_file = argv
  5. 5
  6. 6   print "Copying from %s to %s" % (from_file, to_file)
  7. 7
  8. 8   # we could do these two on one line too, how?
  9. 9   in_file = open(from_file)
  10. 10  indata = in_file.read()
  11. 11
  12. 12  print "The input file is %d bytes long" % len(indata)
  13. 13
  14. 14  print "Does the output file exist? %r" % exists(to_file)
  15. 15  print "Ready, hit RETURN to continue, CTRL-C to abort."
  16. 16  raw_input()
  17. 17
  18. 18  out_file = open(to_file, 'w')
  19. 19  out_file.write(indata)
  20. 20
  21. 21  print "Alright, all done."
  22. 22
  23. 23  out_file.close()
  24. 24  in_file.close()

  25. You should immediately notice that we import another handy command named exists. This returns True if a file exists, based on its name in a string as an argument. It returns False if not.
复制代码

8
vtmc(未真实交易用户) 发表于 2014-12-3 09:17:34
  1. 1   # this one is like your scripts with argv
  2. 2   def print_two(*args):
  3. 3       arg1, arg2 = args
  4. 4       print "arg1: %r, arg2: %r" % (arg1, arg2)
  5. 5
  6. 6   # ok, that *args is actually pointless, we can just do this
  7. 7   def print_two_again(arg1, arg2):
  8. 8       print "arg1: %r, arg2: %r" % (arg1, arg2)
  9. 9
  10. 10  # this just takes one argument
  11. 11  def print_one(arg1):
  12. 12      print "arg1: %r" % arg1
  13. 13
  14. 14  # this one takes no arguments
  15. 15  def print_none():
  16. 16      print "I got nothin'."
  17. 17
  18. 18
  19. 19  print_two("Zed","Shaw")
  20. 20  print_two_again("Zed","Shaw")
  21. 21  print_one("First!")
  22. 22  print_none()

  23. Let’s break down the first function, print_two, which is the most similar to what you already know from making scripts:
  24. 1. First we tell Python we want to make a function using def for “define.”
  25. 2. On the same line as def, we then give the function a name. In this case, we just called it print_two, but it could be peanuts too. It doesn’t matter, except that your function should have a short name that says what it does.
  26. 3. Then we tell it we want *args (asterisk args), which is a lot like your argv parameter but for functions. This has to go inside () parentheses to work.
  27. 4. Then we end this line with a : colon and start indenting.
  28. 5. After the colon all the lines that are indented four spaces will become attached to this name, print_two. Our first indented line is one that unpacks the arguments the same as with your scripts.
  29. 6. To demonstrate how it works, we print these arguments out, just like we would in a script.
  30. Now, the problem with print_two is that it’s not the easiest way to make a function. In Python we can skip the whole unpacking args and just use the names we want right inside (). That’s what print_two_again does.
  31. After that, you have an example of how you make a function that takes one argument in print_one.
  32. Finally you have a function that has no arguments in print_none.
复制代码

9
woodynicolas(未真实交易用户) 发表于 2014-12-4 07:30:25
  1. 1   def cheese_and_crackers(cheese_count, boxes_of_crackers):
  2. 2       print "You have %d cheeses!" % cheese_count
  3. 3       print "You have %d boxes of crackers!" % boxes_of_crackers
  4. 4       print "Man that's enough for a party!"
  5. 5       print "Get a blanket.\n"
  6. 6
  7. 7
  8. 8   print "We can just give the function numbers directly:"
  9. 9   cheese_and_crackers(20, 30)
  10. 10
  11. 11
  12. 12  print "OR, we can use variables from our script:"
  13. 13  amount_of_cheese = 10
  14. 14  amount_of_crackers = 50
  15. 15
  16. 16  cheese_and_crackers(amount_of_cheese, amount_of_crackers)
  17. 17
  18. 18
  19. 19  print "We can even do math inside too:"
  20. 20  cheese_and_crackers(10 + 20, 5 + 6)
  21. 21
  22. 22
  23. 23  print "And we can combine the two, variables and math:"
  24. 24  cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

  25. This shows all the different ways we’re able to give our function cheese_and_crackers the values it needs to print them. We can give it straight numbers. We can give it variables. We can give it math. We can even combine math and variables.
  26. In a way, the arguments to a function are kind of like our = character when we make a variable. In fact, if you can use = to name something, you can usually pass it to a function as an argument.
复制代码

10
Nicolle(未真实交易用户) 学生认证  发表于 2015-10-11 21:08:50

Exercise 20. Functions and Files

提示: 作者被禁止或删除 内容自动屏蔽

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-26 22:09