zoukankan      html  css  js  c++  java
  • python中的数字

    在编程中,通常使用数字来记录游戏得分,表示可视化数据、存储web应用信息等。

    #运算#

    1,基本运算

    >>> 2+3
    5
    >>> 1-2
    -1
    >>> 3/2
    1.5
    >>> 3*19
    57

    2,次序运算

    >>> 2+3*4
    14
    >>> (2+3)*4

    #浮点数#

     在编程中,将带有小数点的数称为浮点数

    >>> 0.2 * 0.3
    0.06

    >>> 0.2+0.1
    0.30000000000000004

    所有语言都存在这种问题,python会尽可能精确地表示结果

    使用函数来str()来避免类型错误

    >>> age = 23
    >>> message = "happy" + age + "rd Birthday!"

    这句语句会报错!

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: Can't convert 'int' object to str implicitly

    这是一个类型错误,意味着python无法识别信息!因为这里使用了一个值为(int)的变量。python知道这个变量的数值可能是23,也可能是字符2和3

    如果要想明确的指出将整个整数用作字符串,可以使用函数str()

    >>> age=23
    >>> message = "happy " + str(age) + "rd Birthday"
    >>> message
    'happy 23rd Birthday'

  • 相关阅读:
    prometheus之五:kube-state-metrics
    prometheus之四:node-exporter
    go语言基础
    EFK+kafka集群实战
    K8S 集群排错指南
    短信倒计时
    微信消息模板
    阿里大鱼
    mui下拉加载
    php无限极分类
  • 原文地址:https://www.cnblogs.com/alben-cisco/p/6802139.html
Copyright © 2011-2022 走看看