Python——处理数字计算

本文介绍了Python中处理数字的方法,包括如何识别数字、设置输出格式,如使用%d和%f占位符,以及如何通过.format()进行格式化。此外,还讲解了如何利用""在长行中换行以增强代码可读性,并介绍了接收用户输入数字时需要进行的类型转换操作。
摘要由CSDN通过智能技术生成

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值