【知识梳理】

【典例分析】
【例题1】
题目:随便输出点啥吧。
1 print("life is short,why not python?")

做题心得:这也太好用了吧!!!好用到哭!!!不过运行起来确实比c慢。
【例题2】
题目:通过用户输入两个数字,并计算两个数字之和。
1 a = input("") 2 b = input("") 3 sum = int(a) + int(b) 4 print("{}".format(sum))

做题心得:唔感觉对输入输出流还不是很了解。
【例题3】
题目:通过用户输入一个数字,并计算这个数字的平方根。
1 import cmath 2 3 num = float(input("")) 4 #实数根 5 #sqrt = num ** 0.5 6 sqrt = cmath.sqrt(num) 7 print("{0}\t+\t{1}i".format(sqrt.real,sqrt.imag))

做题心得:学到了类头文件import,任意次方**,以及平方复根cmath.sqrt()。
【例题4】
题目:通过用户输入数字,并计算二次方程。
1 import cmath 2 3 a = float(input("")) 4 b = float(input("")) 5 c = float(input("")) 6 7 d = b ** 2 - 4 * a * c 8 9 x1 = (-b + cmath.sqrt(d))/(2*a) 10 x2 = (-b - cmath.sqrt(d))/(2*a) 11 12 print("{0}\t{1}".format(x1,x2))

做题心得:有、、上手了哦
【例题5】
题目:用户输入三角形三边长度,并计算三角形的面积。
1 a = float(input("")) 2 b = float(input("")) 3 c = float(input("")) 4 5 p = a + b + c 6 7 S = (p * (p - a) * (p - b) * (p - c)) ** 0.5 8 9 print("{0}".format(S))

做题心得:format万能不报错输出!以后就跟它混了!
【例题6】
题目:圆的面积。
1 def circle(r): 2 PI = 3.141529653589793238522623383279502884 3 return PI * r * r 4 print("{}".format(circle(5)))

做题心得:学会了定义函数哦。
浙公网安备 33010602011771号