一、作业内容: 习题18-26
习题18.
跳过自己最难理解的17题,终于愉快地进入新篇章,向18题进军,此刻必须自我自娱自乐一下,作为激励自己的一颗朱古力豆,告诉自己艰巨的任务任重而道远。
到这一习题将接触到函数(function)了,它能做三件事,不要忘记哦:
1.给代码片段命名,就跟变量给字符串和数字命名一样。
2.可以接受参数,就跟脚本接受argv(这个argv在我这个Python小白这折磨的我不要不要的,终于懂了)一样。
- 通过1,2,可以创建自己的“微型脚本”或者“小命令”。
def()为定义函数。
# this one is like your scripts with argv
def print_two(*args):
arg1, arg2 = args
print("arg1:%r, arg2:%r" % (arg1,arg2))
#ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
print("arg1: %r, arg2: %r" % (arg1, arg2))
# this just takes one argument
def print_one(arg1):
print("arg1: %r" % arg1)
# this one takes no arguments
def print_none():
print("I got nothing.")
print_two("Zed", "Shaw")
print_two_again("Zed", "Shaw")
print_one("First!")
print_none()
# 1. 函数定义是以def命令开始;
# 2. 函数名称是可以随意取名,并且名称最好能体现出函数功能,但要以字母开始,常见以字母数字及下划线组成。
# 3. 函数名称要以()将参数括起来,并且用:结尾。
# 4. 接下来函数定义下的代码缩进4个空格,在解包过程中,参数之间需要用,隔开
# 5. *在程序中泳衣表示python让它把函数的所有参数都接受进来,然后放到名字叫args的列表中去。
结果:
arg1:'Zed', arg2:'Shaw'
arg1: 'Zed', arg2: 'Shaw'
arg1: 'First!'
I got nothing.
要记住几个特殊功能:
运行函数(run)、调用函数(call)、使用函数(use).
习题19
函数和变量
注意函数中的变量和脚本中的变量是没有联系的。
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print("You have %d cheeses!" % cheese_count)
print("You have %d boxes of crackers!" % boxes_of_crackers)
print("Man that's enough for a party!")
print("Get a blanket.\n")
print("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)
print("Or, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
print("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)
print("And we can combine the two, variable and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
结果:
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
Or, we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two, variable and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.
学习了这一节感觉挺有意思,但是自己的脑洞还不够打开,总之比之前的零基础,还是有那点意思了。
习题20
函数和文件,在这里又要面临函数又要面临文件,这里一定要斟酌明白各个函数的含义,还要弄清变量之间的关系,耐人寻味啊!!!
from sys import argv
script, input_file = argv
def print_all(f):
print(f.read())
def rewind(f):
f.seek(0)
def print_a_line(line_count, f):
print(line_count, f.readline())
current_file = open(input_file)
print("First let's print the whole file:\n")
print_all(current_file)
print("Now let's rewind, kind of like a tape.")
rewind(current_file)
print("Let's print three lines:")
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
结果:
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex20.py ex8.txt
First let's print the whole file:
formatter = "%r %r %r %r"
print(formatter % (1, 2, 3, 4))
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % ("I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."))
Now let's rewind, kind of like a tape.
Let's print three lines:
1 formatter = "%r %r %r %r"
2
3 print(formatter % (1, 2, 3, 4))
Process finished with exit code 0
注意:f.seek(0)指回到文件的开始,seek()函数处理的对象是字节而非行,所以seek(0)只不过是转到文件的0 byte。
符号x+=y表示x=x+y。
习题21
def add(a, b):
print("ADDING %d + %d" % (a, b))
return a+b
def subtract(a, b):
print("SUBTRACTING %d - %d" % (a, b))
return a-b
def multiply(a, b):
print("MULTIPLYING %d * %d" % (a, b))
return a*b
def divide(a, b):
print("DIVIDING %d // %d" %(a, b))
return a//b
print("Let's do some math with just functions!")
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print("Age: %d, Height: %d, Weight: %d. IQ: %d" % (age, height, weight, iq))
# A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print("That becomes:", what, "can you do it by hand?")
结果:
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex21.py
Let's do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLYING 90 * 2
DIVIDING 100 // 2
Age: 35, Height: 74, Weight: 180. IQ: 50
Here is a puzzle.
DIVIDING 50 // 2
MULTIPLYING 180 * 25
SUBTRACTING 74 - 4500
ADDING 35 + -4426
That becomes: -4391 can you do it by hand?
Process finished with exit code 0
习题22:
自我复习小结
# 总结符号列表:
# 1. str 字符串
# 常用()括起来,其内部可以使用‘’:所有的空白,即空格和制表符都照原样保留;“”:原理同单引号;
# ‘’‘’‘’,指示一个多行的字符串,在其中还可以自由使用单引号和双引号。
# 2. 转义符
# \在字符串中有单引号或双引号,为了便于识别使用\, eg: 'what\'s your name?'
# \\用于表示转义,同时,在一个字符串中,行末的单独一个\表示字符串在下一行继续,而不是开始一个新的行。
# eg: "This is the first sentence.\
# This is the second sentence."
# 正则表达式用户的注释需要用自然字符串处理,否则要用很多反斜杠。'\\l'或r'\l'。
# 3. 标识符
# 变量是标识符的例子。命名标识符的规则:
# a) 第一个字符必须是字母或者_;
# b) 其他部分可以字母、下划线或数字(0-9)组成;
# c)标识符对字母大小写可识别,大写小写各不同;
# d)有效标识符例子:i、_my_name、alb_c3等;
# e)无效标识符例子:2things、this is spaced out和my-name等。
# 4. 数据类型
# 变量可以处理不同类型的值,称为数据类型,基本类型是字符串和数,用type()标识
# 5. 物理行和逻辑行
# 物理行就是编写程序是所看见的;逻辑行是python看见的单个语句。如果一个物理行中有多个逻辑行,需要用(;)
# 来特别地标明。;标识一个逻辑行/语句的结束。在实际使用中最好每一物理行写一个逻辑行,让代码更易读。
# 可以多个物理行中写一个逻辑行,被称为明确的连接行。
# 6. 缩进
# 行首空白称为缩进,用来决定逻辑行的缩进层次,从而用来决定语句的分组。
# 7. 运算符和表达式
# + 加
# - 减
# * 乘
# ** 幂 eg:3**4(即3*3*3*3)
# / 除
# //取整除
# % 取除法的余数8%3=2(即8除以3余2)
# <<左移
# >>右移
# < 小于
# > 大于
# <= 小于等于
# >= 大于等于
# == 等于
# !=不等于
# not 非
# and 与
# or 或
# (注:not, and, or 是布尔表达式判断真假条件的)
# 运算优先级()在先,数字式满足数学运算优先规律。
# 8. 控制流
# if语句
# while语句
# for语句
# break语句
# continue 语句
# while True:
# s = input('Enter something:')
# if s == 'quit':
# break
# if len(s)< 3:
# continue
# print('Input is of sufficient length')
#注意如何使用这些语句。其中continue 语句被用来告诉python跳过当前循环块中剩余语句,然后继续进行下一轮循环。
# 9. 函数
# def 函数名(变量名,当多个参数时,用逗号隔开):
# 当函数不需要参数时,直接写为 def 函数名():
# 参数有形参和实参之分,第一次定义的为形参,后面将实参提供给函数,通过是用变量调用函数。
# eg:
# def printMax(a, b):
# if a > b:
# print(a, 'is maximum')
# else:
# print(b, 'is maximum')
# printMax(3, 4)#直接把数即实参,提供给函数。
# x=5#调用函数
# y=7
# printMax(x, y)#使实参x的值赋给形参a,实参y的值赋给形参b。
# 10. 局部变量
# 当在函数内部声明变量时,它们与函数外具有相同名称的其他变量没有任何关系。
# 变量名称对函数来说是局部的,这成为变量的作用域。所有变量的作用域是它们被定义的块,
# 从它们的名称被定义的那点开始。如果要告诉python这个变量名是全局的,使用global()语句完成。
#eg:
# def func(x):
# print('x is', x )
# x = 2
# print('Changed local x to', x)
# x =50
# func(x)
# print('x is still', x)
#
#
# def func():
# global x
# print('x is', x)
# x=2
# print('Changed local x to', x)
# x = 50
# func()
# print('Value of x is', x)
#注意默认参数值和关键参数的概念,默认参数值用形参名=默认值,从而给形参指定默认参数值。
# def func(a, b=5)是有效的,而def func(a=5, b)是无效的。如果你的某个函数有许多参数,
# 而你只想指定其中的一部分,那么你可以通过命名来为这些参数赋值,这被称作关键参数:即使用名字(关键字)而不是位置来给函数指定实参。
# 11. return语句
# 用来从一个函数返回即跳出函数。也可选从函数返回一个值。其中函数someFunction没有使用return语句,可以用pass语句在
# python中表示一个空的语句块。
# 12. 模块
# 模块基本就是一个包含了所有你定义的函数和变量的文件。为了在其他程序中重用模块,模块的文件名必须以.py为扩展名。
# import sys
# print('The command line arguments are:')
# for i in sys.argv:#(sys.argv变量时一个字符串的列表,包含了命令行参数的列表,即使用命令行传递给你的程序的参数)
# print(i)
#
# print('\n\nThe PYTHONPATH is', sys.path,'\n')
习题24
print("Let's practice everything.")
print('You\'d need to know\'bout escapes with \\ that do \n newlines and \t tabs.')
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print("________________")
print(poem)
print("________________")
five = 10 - 2 + 3 - 6
print("This should be five: %s" % five)
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
print("With a starting point of: %d" % start_point)
print("We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates))
start_point = start_point / 10
print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))
结果:
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex24.py
Let's practice everything.
You'd need to know'bout escapes with \ that do
newlines and tabs.
________________
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation
where there is none.
________________
This should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000 jars, and 50 crates.
We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crates.
Process finished with exit code 0
网友评论