Python——处理数字计算

来源:互联网 发布:阿里云退款 编辑:程序博客网 时间:2024/05/22 05:24

1、Python怎么识别数字?

width=20   #不加任何形式的引号,自动识别为数字,而不是上节的字符串

height=15
area=width*height  

print(area)


2、设置输出格式(设定占位符%d、%f)

(1)

width=20
height=15
area=width*height/2
print("The area is %.2f : " % area)    #很显然不能使用“+”来连接字符串和数字
                                                       

(2)

print("The area is %6d : " % area)     #整数输出,默认空格填充,也可设置为用0 填充

(3)

print("The area is {0:f} " .format(area))  #.format()

print("Here the three numbers! The first number is {0:d}. Then is {1:4d} and {2:d}".format(7,8,9))


3、"\"的作用

print("Here the three numbers! + \
       The first number is {0:d}. Then is {1:4d} and {2:d}".format(7,8,9))     #Python中使用“\”来确认一整个长行的内容被编辑在了多行,以便代码阅读


4、接收输入数字,需要使用类型转换函数

salary = input("please enter your salary :")
bonus = input("please enter your bonus :")
payAmount = float(salary) + float(bonus)          #若不转换,Python则会认为是字符串salary和bonus相连接
print(payAmount)



0 0
原创粉丝点击