基于Pyhton3.6
习题21
def add(a, b):
print("ADDING %d + %d" % (a, b))
return a + b
def subtract(a, b):
print("SUBTARACTING %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")
习题22
| from...import | 导入包 |
| print("%s, %d", %(str, number)) | 打印 |
| input("prompt") | 输入 |
| open(filename, ‘w’) | 打开文件, 并且写入 |
|
read() readline() |
读取文件内容 按行读取 |
|
def fun(para1, para2): return |
定义函数 返回 |
本文介绍了使用Python进行基本数学运算的方法,包括加减乘除,并通过定义函数来执行这些操作。同时,回顾了Python中常用的函数,如从模块导入功能、打印、输入、文件操作及自定义函数。
3309

被折叠的 条评论
为什么被折叠?



