zoukankan      html  css  js  c++  java
  • python 基础语法

    1. print

    3.x 中print作为函数

    print("1", "2") 即输出 1 2 

    2.x print不为函数,所以

    print "1","2" 即输出1 2

     补充一点,因为python2.7中print一定会带换行,需要输出而不带换行可以用下面这种方法。

    import sys
    
    sys.stdout.write('no cl')
    sys.stdout.write('..')

    2. input 与 raw_input

    raw_input

    raw_input() 将所有输入作为字符串看待,返回字符串类型。//姑且当成gets()

    比如:

    gender = raw_input("what's you gender?")
    print(gender)
    
    输入:male
    输出:
    male
    <type 'str'>

    input

    input() 在3.x版本下:

    input() 在输入不为整形和浮点型时,相当于eval(raw_input(prompt))

    其中 eval 将字符串str当成有效的表达式来求值并返回计算结果。比如:

    字符串转换成列表
    >>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
    >>>type(a)
    <type 'str'>
    >>> b = eval(a)
    >>> print b
    [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
    >>> type(b)
    <type 'list'>

    如果输入为整数或者浮点数,那么直接返回该数字。//可以当成输入、输出模板的input用了。

    如下:

    c = input("please input number")
    print c
    print type(c)
    
    输入:2.31
    结果:
    2.31 <type 'float'>
  • 相关阅读:
    tornado中form表单验证详解
    关于tornado中session的总结
    Linux常用命令
    css3动画属性详解 与超酷例子
    keepalive高可用的健康检查
    keepalive的nginx防火墙问题
    安装配置hadoop
    tmux的简单快捷键
    部署使用elk
    k8s搭建部署
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/8006760.html
Copyright © 2011-2022 走看看