Day3

  • 习题  13:  参数、解包、变量
from sys import argvscript, first, second, third = argvprint("The script is called:",script)
print("Your first variable is:",first)
print("Your second variable is:",second)
print("Your third variable is:",third)#运行power shell
#cd E:\py        #似乎是只能进到一级文件夹
#python 13.py first 2nd 3rd

powershell 运行结果

第 3 行将 argv “解包(unpack)”,与其将所有参数放到同一个变量下面,我们将
每个参数赋予一个变量名: script, first, second, 以及 third。这也许看上
去有些奇怪, 不过”解包”可能是最好的描述方式了。它的含义很简单:“把 argv
中的东西解包,将所有的参数依次赋予左边的变量名”。

  • 习题  14:  提示和传递

 1 from sys import argv
 2
 3 script,user_name = argv
 4 prompt = '> '
 5
 6 print("Hi %s ,I'm the %s script." % (user_name,script))
 7 print("I'd like to ask you a few questions.")
 8 print("Do you like me %s?" % user_name)
 9 likes = input(prompt)
10
11 print("Where do you live %s?" % user_name)
12 lives = input(prompt)
13
14 print("What kind computer do you have?")
15 computer = input(prompt)
16
17 print("""
18 Alright, so you said %s about liking me.
19 You live in %s. Not sure where what is.
20 And you have a %s computer.Nice.
21 """ % (likes, lives, computer))

注意:

print("%r  %s" % (12, 25))

多个格式化字符,记得要在print 的括号里,而且还要一个括号括起来

  • 习题  15:  读取文件

 1 from sys import argv
 2
 3 script, filename = argv
 4
 5 txt = open(filename)
 6
 7 print("Here's your file %r:" % filename)
 8 print(txt.read())
 9
10 print("Type the filename again:")
11 file_again = input("> ")
12 txt_again = open(file_again)
13
14 print(txt_again.read())

  • 习题  16:  读写文件

 1 from sys import argv
 2
 3 script, filename = argv
 4
 5 print("We are going to erase %r." % filename)
 6 print("If you don't want that, hit CTRL-C(^C).")
 7 print("If you do want that, hit RETURN.")
 8
 9 input("?")
10
11 print("Opening the file...")
12 target = open(filename, 'w')#以写模式打开文件,其实会新建一个文件,若原来有,则会被这个覆盖
13
14 print("Truncating the file. Goodbye!")
15 target.truncate()
16
17 print("Now I'm going to ask you for three lines.")
18
19 line1 = input("line 1:")
20 line2 = input("line 2: ")
21 line3 = input("line 3: ")
22
23 print("I'm going to write these to the file.")
24
25 target.write(line1)
26 target.write("\n")
27 target.write(line2)
28 target.write("\n")
29 target.write(line3)
30 target.write("\n")   #用一行写出来:
31 #target.write(line1+"\n"+line2+'\n'+line3)
32 print("And finally, we close it.") 33 target.close()

  • 习题  17:  更多文件操作

 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 input1 = open(from_file)
10 indata = input1.read()
11
12 print("Does the output file exist? %r" % exists(to_file))
13 print("Ready,hit RETURN to continue, CTRL-C to abort.")
14 input()
15
16 output = open(to_file,'w')
17 output.write(indata)
18
19 print("Alright, all done.")
20
21 output.close()
22 input1.close()

第一次报错因为17行,open的时候没有指定以写模式打开,记住,open(file,'w') 要指定打开模式

  • 习题  18:  命名、变量、代码、函数

 1 #this one is like your script with argv
 2 def print_two(*args):
 3     arg1, arg2 = args
 4     print("arg1: %r, arg2: %r" % (arg1, arg2))
 5
 6 #ok, that *args is actually pointless, we can just do this
 7 def print_two_again(arg1, arg2):
 8     print("arg1: %r, arg2: %r" % (arg1,arg2))
 9
10 #this just takes one argument
11 def print_one(arg1):
12     print("arg1: %r" % arg1)
13
14 #this takes no arguments
15 def print_none():
16     print("I got nothin'.")
17
18
19 print_two("Zed","Shaw")
20 print_two_again("MI","YO")
21 print_one("only one")
22 print_none()
23
24 def print_three(a,b,c):
25     print("%s %r %r" % (a, b, c))
26 print_three("a",'b','c')

arg1: 'Zed', arg2: 'Shaw'
arg1: 'MI', arg2: 'YO'
arg1: 'only one'
I got nothin'.
a 'b' 'c'

1. 首先我们告诉 Python 创建一个函数,我们使用到的命令是 def ,也就是“定义
(define)”的意思。
2. 紧接着 def 的是函数的名称。本例中它的名称是 “print_two”,但名字可以随便取,
就叫 “peanuts” 也没关系。但最好函数的名称能够体现出函数的功能来。
3. 然后我们告诉函数我们需要 *args (asterisk args),这和脚本的 argv 非常相似,
参数必须放在圆括号 () 中才能正常工作。
4. 接着我们用冒号 : 结束本行,然后开始下一行缩进。
5. 冒号以下,使用 4 个空格缩进的行都是属于 print_two 这个函数的内容。 其中
第一行的作用是将参数解包,这和脚本参数解包的原理差不多。
6. 为了演示它的工作原理,我们把解包后的每个参数都打印出来,这和我们在之前脚
本练习中所作的类似。

加分题:

1. 函数定义是以 def 开始的吗?                                                        yes
2. 函数名称是以字符和下划线 _ 组成的吗?                                      yes
3. 函数名称是不是紧跟着括号 ( ?                                                     yes
4. 括号里是否包含参数?多个参数是否以逗号隔开?                         yes
5. 参数名称是否有重复?(不能使用重复的参数名)                         no
6. 紧跟着参数的是不是括号和冒号 ): ?                                             yes
7. 紧跟着函数定义的代码是否使用了 4 个空格的缩进 (indent)?       yes
8. 函数结束的位置是否取消了缩进 (“dedent”)?                                yes

转载于:https://www.cnblogs.com/mrfri/p/8450529.html

python3 _笨方法学Python_日记_DAY3相关推荐

  1. python3 _笨方法学Python_日记_DAY4

    Day4 习题  19:  函数和变量 1 def cheese_and_crackers(cheese_count, boxes_of_crackers): 2 print("You ha ...

  2. python数值运算答案_笨方法学Python 习题3:数字和数学计算

    数字和数学计算 print("I will now count my chickens") print("Hens",25+30/6) print(" ...

  3. python38使用_笨方法学Python 习题38:列表的操作

    列表的操作: 这里先复习一下之前遇见过的函数:split()通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符 str.split(str="", nu ...

  4. 笨方法学python第二版_笨方法学Python(2)

    习题 15: 读取文件习题 16: 读写文件 'w' 是什么意思? 它只是一个特殊字符串,用来表示文件的访问模式.如果你用了 'w' 那么你的文件就是写入(write)模式.除了 'w' 以外,我们还 ...

  5. python26章_笨方法学Python-26章练习题

    源地址存在问题 新的练习地址为: https://learnpythonthehardway.org/book/exercise26.txt 具体代码如下: def break_words(stuff ...

  6. python求15 17 23 65 97的因数_笨方法学python,Lesson15,16,17

    Exercise 15 代码 from sys import argv script, filename = argv txt = open(filename) print "Here is ...

  7. 笨方法学python习题4

    变量和命名 #笨方法学python_习题4#定义变量 cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90#计算 cars_not_ ...

  8. 《 笨方法学 Python 》_ 目录

    < 笨方法学 Python >(第 3 版)书中代码是 Python 2 版本,本着学习 Python 3 的目的,用 Python 3 完成本书的习题,代码也已上传到 Github. 作 ...

  9. 笨方法学python6-10

    习题6:字符串和文本 作者提示: 1.尝试破坏代码 然后自己去修复. 代码 type_of_people = 10 #变量赋予 x=f"There are {type_of_people} ...

最新文章

  1. 青少年编程竞赛交流群周报(第039周)
  2. lisp直线连接圆象限电_圆并不难,为什么很多考生就是学不会?
  3. jmeter展示内存cpu_基于Docker的jmeter弹性压测(2)监控
  4. 欧几里得算法和扩展欧几里得算法(Euclidean_Algorithm and Extended_Euclidean_Algorithm)
  5. wxWidgets:多文档界面实例
  6. mysql从innodb转到MyIsam的count查询效率极大提升
  7. 使用Java ThreadLocals的意外递归保护
  8. Cannot find class [xxx] for bean with name ‘‘ defined in class
  9. 最大似然估计、MAP、贝叶斯估计
  10. Python爬虫之(九)数据提取-XPath
  11. 自定义view imageviw
  12. Golang快速入门
  13. 优质编程网站推荐(适合学习和查资料)
  14. 滴滴竟然已经投资了这么多公司?
  15. 微软中国2023校招【内推】全面开启!
  16. 三种POSS材料(乙烯基POSS、氨基POSS和苯基POSS)
  17. 入门学习编程培训有哪些科目课程适合?
  18. 银河移民PHP面试,移民香港,我真的“后悔死了”
  19. STM32CubeMX-5.0.0使用遇到的问题
  20. 国仁老猫:怎么制作抖音100W播放量的作品;首选需要精准定位。

热门文章

  1. 一线互联网技术:Java工程师架构知识系统化汇总,面完45K!
  2. Unity Log重新定向
  3. 利用脚本生成GUID
  4. Windows 活动目录(AD)服务器系统升级到2012之活动目录角色迁移(三)
  5. web存储中cookie、session区别
  6. C2:抽象工厂 Abstract Factory
  7. 【JAVA零基础入门系列】Day2 Java集成开发环境IDEA
  8. Linux----函数中变量的作用域、local关键字。
  9. 安卓 画板 学习笔记
  10. 下午就要考试啦~~附上自己做的考试范围