python 材料科学与工程_#python-练习

1.平方根

1 num = float(input("输入一个数字:"))2 num_sqrt = num ** 1/2#定义平方根公式3 print ('%0.3f的平方根为%0.3f'% (num, num_sqrt)) # or print('{0}的平方根为{1}'.format(num, num_sqrt))

2.复数的平方根

1 importcmath2

3 num = float(input("输入一个数字:"))4 num_sqrt =cmath.sqrt(num)5 print ('{0}的平方根为{1:0.3f} + {2:0.3f}'.format(num, num_sqrt.real, num_sqrt.imag))

#虚部是imag!每次打成image

3.二元一次方程求解

1 importcmath2

3 a = float(input(''))4 b = float(input(''))5 c = float(input(''))6 d = b * b - 4*a*c7 sol1 = (-b + cmath.sqrt(d))/(2*a)8 sol2 = (-b - cmath.sqrt(d))/(2*a)9 print('结果为{0}和{1}'.format(sol1, sol2))

4.三角形面积

1 a = float(input(' '))2 b = float(input(' '))3 c = float(input(' '))4 p = (a + b + c)/2 #三角形的半周长

5 s = (p * (p-a) * (p-b) * (p-c)) **1/2 #海伦公式

6 print ("三角形面积{:0.5f}".format(s))7 print ("三角形面积%0.5f"%s)

5.

-使用临时值

1 x = input ('输入:')2 y = input ('输入:')3

4 temp =x5 x =y6 y =temp7

8 print("x交换后{}".format(x))9 print("y交换后{}".format(y))

-不使用临时值

1 x = input ('输入:')2 y = input ('输入:')3

4 x, y =y, x5

6 print("x交换后{}".format(x))7 print("y交换后{}".format(y))

6.if语句进行判断Pozitive, Zero, Negative

1 num = float(input('输入:'))2

3 if num >0:4 print("Pozitive")5 elif num ==0:6 print("Zero")7 else:8 print("Negative")

# 使用if嵌套

if num >= 0:

if num == 0:

print('Zero')

else:

print('Pozitive')

else:

print('Negative')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值