Task4 函数

一、何谓函数?

# this is one like your scripts with argv
def print_two(*args): # def即define 而print_two是函数名字
    arg1,arg2 = args
    print(f"arg1:{arg1}, arg2:{arg2}")

# ok,this *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
    print(f"arg1:{arg1}, arg2:{arg2}") #这个函数与上一函数本质相同,形式不同罢了

# this just takes one argument
def print_one(agr1):
    print(f"agr1:{agr1}")

# this one takes no arguments
def print_none(): # 定义完函数以:结束本行,另一行开始缩进
    print("I got noyhin'.")

print_two("Zed","Shaw") # 输入参数
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

1. def即define,形如上面代码的print_two是被定义的函数名字

2. 函数名称要求,不以数字开头的任意组合都可

3. 定义完函数以 : 结束本行,另一行开始缩进

4. 通过输入函数名称来调用(call)/运行(run)/使用(use)一个函数,运行时加(,结束调用该函数用),把想要的值放括号里用逗号隔开

二、函数变量的赋值方式

#定义函数
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print(f"You have {cheese_count} cheeses!")
    print(f"You have {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 ,variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

以上是给函数cheese_and_crackers几种不同的赋值方式,直接用数字,也可以变量,也可以数学运算,亦或是数学运算与变量的结合。具体方式如上代码。

ps.amount_of_cheese是全局变量(global variables),与函数变量cheese_count无关。

三、函数与文件

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)

其实具体代码我也搞不太懂emmmmmm,先复制一点大佬的东西叭,日后顿悟再回来写所思所悟所感。

在 print_all 和其他函数里的 f 是什么东西? f 是一个变量,就像你在练习 18 中函数的变量一样,只不过这次它是一个文件。文件在 Python 里面有点类似于一个老式电脑里面的磁带驱动器,或者一个 DVD 播放机。它有一个“读取头”(read head),你可以在文件里 seek (寻找)这个读取头所在的位置,然后在那里工作。每次你做 f.seek(0) 的时候你都会从移动到文件最开始,每次你做 f.readline() 的时候,你都在从文件里读取一行内容,并且把读取头移动到 \n 后面,也就是每行结束的地方。 我会在后面给你做更详细的解释。

为什么 seek(0) 没有把 current_line 设置为 0? 首先,seek() 函数处理的是字节(bytes),不是行。seek(0) 这个代码把文件移动到 0 字节(也就是第一个字节处)。其次,current_line 只是一个变量并且跟这个文件没有任何实际联系。我们是在手动累加它。

什么是 += ? 你知道在英语里我们可以把 “it is” 写成 “it's” ,或者把 “you are” 写成“you're” ,这叫缩写(contraction)。而 += 就像 = 和 + 两种运算的缩写。也就是 x = x + y 就等同于 x += y 。

readline() 是怎么知道每一行在哪儿的? readline() 里面的代码能够扫描文件的每个字节,当它发现一个 \n 字符,它就会停止扫描这个文件,然后回到它发现的地方。文件 f 就负责在每次调用 readline() 之后维持文件的当前位置,以此来保证它能阅读到每一行。

为什么文件中的行之间会有空行? readline() 函数返回文件中每行最后的 \n 。又在 print 函数的结尾加上一个 end = " " 来避免给每行加上两个\n。

四、函数返回东西

def add(a,b):
    print(f"ADDING {a} + {b}")
    return a + b

def subtract(a,b):
    print(f"SUBTRACTING {a} - {b}")
    return a - b

def multiply(a,b):
    print(f"MULTIPLYING {a} * {b}")
    return a * b

def divide(a,b):
    print(f"DIVIDING {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(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {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?")

运行结果如下:

 从中不难看出,python是从里往外(inside out)计算的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值