欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

PythonThinking

时间:2023-04-19

T4总结:
创建函数n
函数与变量:传入参数
函数与文件操作?
函数return实现四则运算
input

#创建函数、参数个数# this one is like your scripts with argvdef print_two(*args):#?这里类似指针不太清楚 arg1, arg2 = args print(f"arg1: {arg1}, arg2: {arg2}")# ok, that *args is actually pointless, we can just do thisdef print_two_again(arg1, arg2): print(f"arg1: {arg1}, arg2: {arg2}")# this just takes one argumentdef print_one(arg1): print(f"arg1: {arg1}")# this one takes no argumentsdef print_none(): print("I got nothin'.")print_two("Zed","Shaw")print_two_again("Zed","Shaw")print_one("First!")print_none()

arg1: Zed, arg2: Shawarg1: Zed, arg2: Shawarg1: First!I got nothin'.

***args 中的 * 是什么作用?**这是告诉 Python 取所有的参数给函数,然后把它们放在 args 里放成一列,很像你之前学的 argv ,只不过这个是为函数设置的。这种不常用,除非有特殊需要。

#函数和变量:传入参数def cheese_and_crackers(cheese_count,boxes_of_crackers): print("you have",cheese_count,"cheeses!") print("you have",boxes_of_crackers,"boxes of crackers!n")print("We can just give the function numbers directly:")cheese_and_crackers(20,30)print('or,we can use variables from our scripy:')amount_of_cheese=10amount_of_crackers=50cheese_and_crackers(amount_of_cheese,amount_of_crackers)print("we can even do math inside too:")# print("!",amount_of_cheese)cheese_and_crackers(10+20,5+6)print("And we can combine two,variables and math:/n")cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)# print("Let's input")# amount_of_cheese=int(input())# amount_of_crackers=int(input())# cheese_and_crackers(amount_of_cheese,amount_of_crackers)

#函数和变量 HDW exe19def cheese_and_crackers(cheese_count,boxes_of_crackers): print("you have",cheese_count,"cheeses!") print("you have",boxes_of_crackers,"boxes of crackers!n")print("We can just give the function numbers directly:")cheese_and_crackers(20,30)print('or,we can use variables from our scripy:')amount_of_cheese=10amount_of_crackers=50cheese_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 two,variables and math:/n")cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)# print("Let's input")# amount_of_cheese=int(input())# amount_of_crackers=int(input())# cheese_and_crackers(amount_of_cheese,amount_of_crackers)

We can just give the function numbers directly:you have 20 cheeses!you have 30 boxes of crackers!or,we can use variables from our scripy:you have 10 cheeses!you have 50 boxes of crackers!we can even do math inside too:you have 30 cheeses!you have 11 boxes of crackers!And we can combine two,variables and math:/nyou have 110 cheeses!you have 1050 boxes of crackers!

1.实现input输入

2.变量传递给函数是暂时的:
在函数中在函数中创建建 amount_of_cheese 这个个变量会改量会改变 cheese_count 这个个变量量吗?? 不会的,这些变量是相互独立并存在于函数之外的。它们之后会传递给函数,而且是“暂时版”,只是为了让函数运行。当函数退出之后,这些暂时的变量就会消失,

可以在函数里调用函数

前置复习:
?需要查阅资料

close - 关闭文件,就像编辑器中的 “文件->另存为”一样。read - 读取文件内容。你可以把读取结果赋给一个变量。readline - 只读取文本文件的一行内容。truncate - 清空文件。清空的时候要当心。write(‘stuff’) - 给文件写入一些“东西”。seek(0) - 把读/写的位置移到文件最开头。

#函数和文件 HDW exe20from sys import argvscript, input_file = argvdef 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 = 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)

File "", line 7 print(f.read()) ^IndentationError: expected an indented block

#函数return实现四则运算 e21def add(a, b): print("ADDING %d + %d"%(a,b))# print(f"ADDING {a} + {b}")这种写法可用于替换 print(f"ADDING {a} + {b}") return a + bdef subtract(a, b): print(f"SUBTRACTING {a} - {b}") return a - bdef multiply(a, b): print(f"MULTIPLYING {a} * {b}") return a * bdef divide(a, b): print(f"DIVIDING {a} / {b}") return a / bprint("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}n")#可知return赋值给了age、height# 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))))#嵌套,一级一级算就OKprint("That becomes: ", what, "Can you do it by hand?n")print("Let's iuput")num1 = int(input("输入第一个数字: "))num2 = float(input("输入第二个数字: "))age=add(num1,num2)print(age)

Let's do some math with just functions!ADDING 30 + 5ADDING 30 + 5SUBTRACTING 78 - 4MULTIPLYING 90 * 2DIVIDING 100 / 2Age: 35, Height: 74, Weight: 180, IQ: 50.0Here is a puzzle.DIVIDING 50.0 / 2MULTIPLYING 180 * 25.0SUBTRACTING 74 - 4500.0ADDING 35 + -4426ADDING 35 + -4426.0That becomes: -4391.0 Can you do it by hand?Let's iuput输入第一个数字: 1输入第二个数字: 2ADDING 1 + 2ADDING 1 + 2.03.0

Python3.6新增了一种f-字符串格式化
格式化的字符串文字前缀为’f’和接受的格式字符串相似str.format()。它们包含由花括号包围的替换区域。替换字段是表达式,在运行时进行评估,然后使用format()协议进行格式化。

#以上只是概述,看看PYHDW函数讲解如何展开

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。