博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3 _笨方法学Python_日记_DAY3
阅读量:6982 次
发布时间:2019-06-27

本文共 4231 字,大约阅读时间需要 14 分钟。

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 argument11 def print_one(arg1):12     print("arg1: %r" % arg1)13 14 #this takes no arguments15 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

你可能感兴趣的文章
java String长度与varchar长度匹配理解(字符和字节长度理解)
查看>>
数据採集器服务——Socket(今天才发现AES加解密代码跟贴的时候不一样,貌似乱码,不知什么情况)...
查看>>
复习笔记——操作系统
查看>>
word2vec中文类似词计算和聚类的使用说明及c语言源代码
查看>>
C++项目參考解答:求Fibonacci数列
查看>>
Mac 下搭建环境 homebrew/git/node.js/npm/vsCode...
查看>>
Python(十)之GUI编程
查看>>
基于Docker的redis集群搭建
查看>>
文件分割机
查看>>
[Winform]WebKit.Net使用
查看>>
17 HTTP编程入门
查看>>
Eclipse安装Jetty插件(Web容器)
查看>>
js使用defineProperty的一些坑
查看>>
python 识别验证码
查看>>
【转】android IDE——通过DDMS查看app运行时所占内存情况
查看>>
运维常说的 5个9、4个9、3个9 的可靠性,到底是什么???
查看>>
[SQL] 函数整理(T-SQL 版)
查看>>
Java+大数据开发——HDFS详解
查看>>
.NET Core 使用RabbitMQ
查看>>
ArcGIS AO中控制图层中要素可见状态的总结
查看>>