zoukankan      html  css  js  c++  java
  • Python输入/输出语句

    Python输出语句print函数

    • print()函数基本使用

        打印整形数据

        打印浮点型数据

        打印字符型数据

    >>> print(12)
    12
    >>> print(12.5)
    12.5
    >>> print('B')
    B
    >>> print('WWW.baidu.com')
    WWW.baidu.com
    >>> x=12
    >>> print(12)
    12
    >>> y=12.88
    >>> print(y)
    12.88
    >>> z='www.bling.com'
    >>> print(z)
    www.bling.com
    >>> print z
    www.bling.com
    >>> print x,y,z
    12 12.88 www.bling.com
    >>> print(z,y,z)
    ('www.bling.com', 12.88, 'www.bling.com')

    • print格式化输出

      print(format(val,fomat_modifier))

        var:值

        fomat_modifier:格式字

      print(12.34567,'m.nf')

        m:表示占位数量(m),m<有效位时,左对齐;m>有效位时,右对齐

        n:表示精度(n)

      print x,y  '逗号'连接输出

    >>> print(format(12.345678,'6.2f'))
     12.35
    >>> print(format(12.345678,'6.3f'))
    12.346
    >>> print(format(12.345678,'6.9f'))
    12.345678000
    >>> print(format(12.345678,'6.0f'))
        12
    >>> print(format(12.345678,'6.3f'))
    12.346
    >>> print(format(12.345678,'6.2f'))
     12.35
    >>> print(format(12.345678,'9.2f'))
        12.35
    >>> print(format(12.345678,'3.2f'))
    12.35
    >>> print(format(12.345678,'5.2f'))
    12.35

      print()格式化输出format

        print(format(val,fomat_modifier))

        print(format(0.3456,'.2%'))

    >>> print(format(0.3456,'.2%'))
    34.56%
    >>> print(format(0.3456,'.3%'))
    34.560%
    >>> print(format(0.3456,'.6%'))
    34.560000%
    >>> print(format(0.3456,'6.3%'))
    34.560%
    >>> print(format(0.3456,'6.1%'))
     34.6%
    • Python输入函数

      re = raw_input(prompt)

      prompt:提示字符,从键盘读取数据

      re:返回值,返回字符串 

      类型转换:int()(转为整形)、float()(浮点型)

    >>> re = raw_input()
    www.bling.com
    >>> print(re)
    www.bling.com
    >>> type(re)
    <type 'str'>
    >>> age = raw_input('your age :')
    your age :22
    >>> age = age +1
    
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        age = age +1
    TypeError: cannot concatenate 'str' and 'int' objects
    >>> age = int(age)
    >>> age
    22
    >>> age = age +1
    >>> age
    23
    >>> type(age)
    <type 'int'>
    >>> age = int(raw_input('PLZ input your age :'))
    PLZ input your age :23
    >>> type(age)
    <type 'int'>
    >>> f1 = float('12.35')
    >>> type(f1)
    <type 'float'>
    >>> weight = float(raw_input('Plz input your weight :'))
    Plz input your weight :70.01
    >>> print(weight)
    70.01
    >>> type(weight)
    <type 'float'>
    >>> 
  • 相关阅读:
    字符串 CSV解析 表格 逗号分隔值 通讯录 电话簿 MD
    Context Application 使用总结 MD
    RxJava RxPermissions 动态权限 简介 原理 案例 MD
    Luban 鲁班 图片压缩 MD
    FileProvider N 7.0 升级 安装APK 选择文件 拍照 临时权限 MD
    组件化 得到 DDComponent JIMU 模块 插件 MD
    gradlew 命令行 build 调试 构建错误 Manifest merger failed MD
    protobuf Protocol Buffers 简介 案例 MD
    ORM数据库框架 SQLite 常用数据库框架比较 MD
    [工具配置]requirejs 多页面,多入口js文件打包总结
  • 原文地址:https://www.cnblogs.com/yangml/p/3849971.html
Copyright © 2011-2022 走看看