Python (三)

作者: smileJiuer | 来源:发表于2018-10-29 09:47 被阅读0次

    缩进

    if True:
        print('Answer')
        print('True')
    else:
        print('Answer')
    
        # 以下代码没有按照规定的缩进编程,运行时会报错
      print('false')
    
    缩进错误的报错

    反斜杠换行

    total = 1 + \
        2 + \
        3
    print(total)                             # result: 6
    
    days = ['Monday', 'Tuesday', 'Wednesday',
            'Thursday', 'Friday']
    print(days)
    
    # 语句中包含 [], {} 或 () 括号就不需要使用多行连接符。
    

    输出

    1>>输出变量

      import sys; x = 'fjsojfi'; print(x + '\n')
    
    // fjsojfi
    // 多条语句可在同行书写,以分号分隔
    

    python数据类型

    Numbers(数字)
    • int(有符号整型)
    • long(长整型[也可以代表八进制和十六进制])
    • float(浮点型)
    • complex(复数)
      复数由实数部分和虚数部分构成,可以用 a + bj,或者 complex(a,b) 表示, 复数的实部 a 和虚部 b 都是浮点型。
    String(字符串)
    List(列表)
    Tuple(元组)
    >>> tuple = ('fjsodjf', '131', 1111)
    >>> print(tuple)
    ('fjsodjf', '131', 1111)
    >>> tuple[2]
    1111
    >>> tuple[2] = 'fjsof'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    
    # tuple 元组类型不支持赋值,相当于只读列表
    
    Dictionary(字典)
    >>> dict = {}
    >>> dict[0] = 'one'
    >>> dict[1] = 'two'
    >>> dict
    {0: 'one', 1: 'two'}
    >>>
    
    # 字典当中的元素是通过键来存取的,而不是通过偏移存取。相当于JS中的对象
    
    # 字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
    

    相关文章

      网友评论

        本文标题:Python (三)

        本文链接:https://www.haomeiwen.com/subject/amkltqtx.html