zoukankan      html  css  js  c++  java
  • 融入python-6-赋值

    变量赋值

    x,y,z = 1,2,3

    灵活使用,可以减少代码量

    key,value = dict.popitem()

    *星号的使用可以代表多个值

    #正常

    x,y,z = 1,2,3  

    #参数列表数目不匹配

    x,y,z = 1,2,3,4
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    ValueError: too many values to unpack (expected 3)
    x,y,z = 1,2
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    ValueError: not enough values to unpack (expected 3, got 2)

    #星号来收集

    x,y,*z = 1,2,3,4
    z
    [3, 4]

    #两个星号不可以!

    *x,y,*z = 1,2,3,4,5,6
    File "<input>", line 1
    SyntaxError: two starred expressions in assignment

    链式赋值

    x = y = z = '你好,中国'

    运算赋值

    +=  -=  *=  /=

    x
    1
    x += 3
    x
    4
    x -= 1
    x
    3
    x *= 7
    x
    21
    x /= 9
    x
    2.3333333333333335

    字符串运算

    x = "你好"
    x
    '你好'
    x += ",悟空。"
    x
    '你好,悟空。'
    x -= "哈哈"
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: unsupported operand type(s) for -=: 'str' and 'str'
    x -= "空。"
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: unsupported operand type(s) for -=: 'str' and 'str'
    x *= "哈哈"
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: can't multiply sequence by non-int of type 'str'
    x *= 2
    x
    '你好,悟空。你好,悟空。'
    x /= 3
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: unsupported operand type(s) for /=: 'str' and 'int'
  • 相关阅读:
    手机怎么知道5G基站的存在?(小区搜索和SSB简介)
    Python中*args,**kwargs两个参数的作用?
    python之jupyter的安装
    国内安装python库速度慢的解决办法
    MOSFET:金属-氧化物半导体场效应晶体管
    C# 小知识点汇总
    ajax和form和七个中间件
    BBS功能分析
    MVC和MTV
    自关联和auth模块
  • 原文地址:https://www.cnblogs.com/qianheng/p/13211908.html
Copyright © 2011-2022 走看看