Python赋值运算符

Posted Hany博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python赋值运算符相关的知识,希望对你有一定的参考价值。

Python赋值运算符:

  = : 赋值运算符

  += : 加法赋值运算符

  -= : 减法赋值运算符

  *= : 乘法赋值运算符 

   /= : 除法赋值运算符

 %= : 取模赋值运算符 ,当前面的数小于后面的数时,返回前一个数本身(数大于 0)。

  **= : 幂赋值运算符

  //= : 取整赋值运算符

 注:a 符号等于 b 等价于 a 等于 a 符号 (b)

 

# = 赋值运算符
a = 4
b = 5

c = a + b
print("a = {0} , b = {1} , c = {2}".format(a,b,c))
# a = 4 , b = 5 , c = 9

# += 加法赋值运算符
a = 4
b = 5
a += b #等价于 a = a + b
print("a = {0} , b = {1} ".format(a,b))
# a = 9 , b = 5

# -= 减法赋值运算符
a = 4
b = 5
a -= b #等价于 a = a - b
print("a = {0} , b = {1} ".format(a,b))
# a = -1 , b = 5

# *= 乘法赋值运算符
a = 4
b = 5
a *= b #等价于 a = a * (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 20 , b = 5

# /= 除法赋值运算符
a = 4
b = 5
a /= b #等价于 a = a / (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 0.8 , b = 5

# %= 取模赋值运算符
a = 4
b = 5
a %= b #等价于 a = a % (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 4 , b = 5

a = 6
b = 4
a %= b #等价于 a = a % (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 2 , b = 4

# **= 幂赋值运算符
a = 4
b = 2
a **= b #等价于 a = a ** (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 16 , b = 2

# //= 取整赋值运算符,返回整数
a = 4
b = 3
a //= b #等价于 a = a // (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 1 , b = 3 

2020-02-05

以上是关于Python赋值运算符的主要内容,如果未能解决你的问题,请参考以下文章

Python运算符 ( 算术运算符 | 赋值运算符 )

零基础学python第七课,while循环赋值运算符循环嵌套

零基础学python第七课,while循环赋值运算符循环嵌套

Python中在进行赋值运算时即使两侧操作数的类型不同也不会报错?

如何在 python 中并行化以下代码片段?

Python赋值运算符