Neo Anderson's Blog

Python相关tip

字数统计: 267阅读时长: 1 min
2018/11/15
  • 乘运算符 - 两个数相乘或是返回一个被重复若干次的字符串

1
2
print ("=" * 80)

  • 在python中,不可变对象是共享的,创建可变对象永远是分配新地址

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    def x(a):
    """
    id()查看内存地址
    """
    print(id(a))
    a.pop()
    print(a)
    print(id(a))
    a = a +[3]
    print(a)
    print(id(a))
    x(11)
  • 函数参数类型

    • key 位置参数
    • 默认参数 : 必须不可变类型
    • *key 可变参数
    • **key 关键字参数
    • (key1,key2, * ,key1,key2) 命名关键字参数时,要特别注意,如果没有可变参数,就必须加一个作为特殊分隔符。如果缺少,Python解释器将无法识别位置参数和命名关键字参数:
  • “IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block

    • 出现该错误不要慌, 一般是代码缩进有异常. 在报错位置,调整代码便可
  • TypeError: Can’t convert ‘int’ object to str implicitly

    • 与字符串链接,需要保证所有变量类型是字符串(str())
CATALOG
  1. 1. 乘运算符 - 两个数相乘或是返回一个被重复若干次的字符串
  2. 2. 在python中,不可变对象是共享的,创建可变对象永远是分配新地址
  3. 3. 函数参数类型
  4. 4. “IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block
  5. 5. TypeError: Can’t convert ‘int’ object to str implicitly