Python 中的赋值运算符

在本节中,我们将讨论 Python 编程语言中的赋值运算符。在进入主题之前,让我们简单介绍一下 Python 中的运算符。运算符是在编程语言中用于操作数之间执行逻辑和数学运算的特殊符号。操作员对其进行运算的值称为操作数。有不同类型的运算符算术、逻辑、关系、赋值和按位等。

Python 有一个赋值运算符,有助于为左侧变量赋值或表达式。赋值运算符表示为赋值语句和赋值表达式中使用的“=”符号。在赋值运算符中,右侧的值或操作数被赋给左侧的操作数。

以下是赋值运算符的示例:


x = 8                  # here 8 is assigned to the x (left side operand)
y = 20                  # here 8 is assigned to the x (left side operand)
c = a + b - 5            # here the value of expression a + b - 5 is assigned to c

赋值运算符的类型

以下是 Python 中不同类型的赋值运算符:

  1. 简单赋值运算符(=)
  2. 加法和相等运算符(+=)
  3. 加减运算符(-=)
  4. 星号和等号运算符(*=)
  5. 除与等运算符(/=)
  6. 模数和相等运算符(%=)
  7. 双除等运算符(//=)
  8. 指数赋值运算符(**=)
  9. 按位“与”运算符(&=)
  10. 按位或运算符(|=)
  11. 按位异或赋值运算符(^=)
  12. 按位右移赋值运算符(> > =)
  13. 按位左移赋值运算符(< < =)

赋值运算符(=)

简单赋值运算符将右侧操作数表达式或值赋给左侧操作数。

语法


C = A + B

示例:


# Program to demonstrate the simple Assignment Operator.
a = 5 # assign value
b = a # assign the expression to the left operand
# print result
print( "Output = ", b)

输出:

Output = 5

加法和赋值运算符(+=)

运算符将右侧操作数或值添加到左侧操作数,然后将结果赋给左侧操作数。

语法


A += B or A = A + B

示例:


# Program to demonstrate the Add and Assignment Operators in Python. 
a = 5 # assign value
b = 3
# a = a + b assign the expression to the left operand 
a += b 
# print result
print( "Output = ", a)

输出:

Output = 9

减法和赋值运算符(-=)

运算符从左侧操作数中减去右侧操作数或值,并将该值存储到左侧操作数中。

语法


C -= A or C = C - A

示例:


# Program to demonstrate the Subtract and Assign Operators in Python. 
a = 5 # assign value
b = 3
# a = a - b or a -= b assign the expression to the left operand 
a -= b 
# print result
print( "Output = ", a)

输出:

Output = 2

乘法和赋值运算符(*=)

运算符将右侧操作数或值乘以左侧操作数,并将乘积存储到左侧操作数。

语法


A *= B or A = A * B

示例:


# Program to demonstrate the Multiply and Assign Operators in Python. 
a = 15 # assign value
b = 4
# a = a * b, or a *= b assign the expression to the left operand 
a *= b 
# print result
print( "Output = ", a)

输出:

Output = 60

除法和赋值运算符(/=)

运算符将左操作数除以右操作数,然后将结果赋给左操作数。

语法


B /= A or B = B / A

示例:


# Program to demonstrate the Divide and Assign Operators in Python. 
a = 80 # assign value
b = 4
# a = a / b or a /= b assign the expression to the left operand 
a /= b 
# print result
print( "Output = ", a)

输出:

Output = 20.0

模数和赋值运算符(%=)

运算符将左侧操作数除以右侧操作数或值,并将余数放在左侧操作数上。

语法


B %= A or B = B % A

示例:


# Program to demonstrate the Modulus and Assign Operators in Python. 
a = 80 # assign value
b = 6
# a = a % b or a %= b assign the expression to the left operand 
a %= b 
# print result
print( "Output = ", a)

输出:

Output = 2

楼层划分和分配运算符(//=)

底板除法运算符将左侧操作数除以右侧操作数或值,然后将底板(值)赋给左侧操作数。

语法


B //= A or B = B // A

示例:


# Program to demonstrate the Floor division and Assign Operators in Python. 
a = 131 # assign value
b = 6
# a = a // b or a //= b assign the expression to the left operand 
a //= b 
# print result
print( "Output = ", a)

输出:

Output = 21

指数和赋值运算符(**=)

指数赋值运算符用于使用两个操作数获取指数值,并将结果赋给左操作数。

语法


B **= A or B = B ** A

示例:


# Program to demonstrate the exponent (**) and Assign Operators in Python.
a = 4 # assign value
b = 3
# a = a ** b or a **= b assign the expression to the left operand 
a **= b 
# print result
print( "Output = ", a)

输出:

Output = 64

按位 And (&)和赋值运算符(&=)

按位 And (&)和赋值运算符用于对(左和右)操作数进行运算,并将结果赋给左操作数。

语法


B &= A

示例:


# Program to demonstrate the Bitwise And (&) and Assign Operators in Python. 
a = 6 # assign value
b = 13 
#   0110  (6 in binary)
# & 1101  (13 in binary)
#  ________
#   0100 = 4 (In decimal)

# a = a & b or a &= b assign the expression value to the left operand 
a &= b   
# print result
print( "Output = ", a)

输出:

Output = 4

按位或和赋值运算符(|=)

按位“或”和“赋值”运算符用于对(左和右)操作数进行运算,并将结果存储到左操作数中。

语法


B |= A

示例:


# Program to demonstrate the Bitwise OR and Assign Operators in Python. 
a = 6 # assign value
b = 13 
#   0110  (6 in binary)
# | 1101  (13 in binary)
#  ________
#   0100 = 4 (In decimal)

# a = a | b or a |= b assign the expression values to the left operand 
a |= b   
# print result
print( "Output = ", a)

输出:

Output = 15

按位异或和赋值运算符(^=)

按位异或和赋值运算符对(左和右)操作数进行运算,并将结果赋给左操作数。

语法


B ^= A

示例:


# Program to demonstrate the Bitwise XOR and Assign Operators in Python. 
a = 6 # assign value
b = 13 

# a = a | b or a |= b assign the expression values to the left operand 
a ^= b   
# print result
print( "Output = ", a)

输出:

Output = 11

按位右移和赋值运算符(> > =)

运算符将指定数量的位或操作数向右移动,并将值赋给左操作数。

语法


B >>= A

示例:


# Program to demonstrate the Bitwise Right shift and Assign Operators in Python. 
a = 6 # assign value
b = 2 

# a = a >> b or a >>= b assign the expression value to the left operand 
a >>= b   
# print result
print( "Output = ", a)

输出:

Output = 1

按位左移和赋值运算符(< < =)

运算符将指定数量的操作数向左移动,并将结果赋给左操作数。

语法


B <<= A

示例:


# Program to demonstrate the Bitwise Left shift and Assign Operators in Python. 
a = 6 # assign value
b = 2 

# a = a >> b or a >>= b, assign the expression value to the left operand 
a <<= b   
# print result
print( "Output = ", a)  

输出:

Output = 24

原创文章,作者:简单一点,如若转载,请注明出处:https://www.506064.com/n/130351.html

(0)
简单一点的头像简单一点
上一篇 2024-10-03
下一篇 2024-10-03

相关推荐

  • js网页背景雪花(js实现雪花飘落特效)

    1、淘宝里首页飘雪花 怎么弄啊 js代码放在哪里啊 本人小白 希望大神详解….. 2、利用JS给网页添加雪花飘落的效果 3、怎么让网页上飘着雪花 div class=&…

    编程 2024-10-03
  • c语言要学习,c语言学习网站

    本文目录一览: 1、学习C语言要些什么基础? 2、c语言如何学习 3、c语言程序设计学什么 4、学习C语言需要掌握哪些基本知识? 学习C语言要些什么基础? 简单学习C语言的基础: …

    编程 2024-10-04
  • Linux安装步骤与注意事项

    一、导轨的安装步骤和注意事项 1、 安装前需检查导轨是否匹配特定设备型号,如不能匹配需选择适当导轨。 2、 安装时需将设备正确固定在适当的安装位置。 3、 导轨安装前需测试设备的稳…

    编程 2024-10-04
  • 了解如何使用PHP的filter_var函数进行数据验证和过滤

    在开发Web应用程序时,数据经常被传输和处理。为了确保数据的准确性和安全性,PHP提供了filter_var函数,通过该函数可以轻松地验证和过滤用户输入的数据。 一、使用filte…

    编程 2024-10-14
  • JupyterPython:统一交互式编程环境的领导者

    一、多语言支持 JupyterPython不仅支持Python语言,而且还能够支持其他几十种语言、如R、Julia、Scala等。所以,我们可以在同一个工具中轻松切换不同的语言环境…

    编程 2024-10-04
  • Accessor详解

    一、Accessor介绍 Accessor即存取器,是一种用于访问和设置对象属性的方式。它允许开发者在获取对象属性值或设置对象属性值时,执行特定的操作,比如进行数据校验、数据过滤、…

    编程 2024-10-03
  • 如何安装sklearn库

    一、Python怎么安装sklearn库 在Python中安装sklearn库需要遵循以下步骤: 1. 为了确保可以正确安装sklearn库,建议首先升级pip工具。可以使用以下命…

    编程 2024-10-03
  • 求出最大公约数c语言,求最大公约数c语言算法

    本文目录一览: 1、c语言如何求最大公约数和最小公倍数 2、C语言程序设计如何求最大公约数? 3、如何用C语言求两个数的最大公约数的三种算法 4、c语言求两个数的最大公约数 5、C…

    编程 2024-10-04
  • Segnet:基于深度学习的语义分割网络

    一、Segnet的概述 Segnet是一个基于深度学习的语义分割网络,用于从图像中分离出不同的语义对象。它采用了encoder-decoder结构,使用了卷积、下采样和上采样等技术…

    编程 2024-10-04
  • Python表达式编写流程

    一、Python表达式简介 Python表达式是由一些数字、操作符、变量和函数等组合而成,通过计算并返回值。Python表达式语法简单,易于学习和使用。在Python中,可以使用表…

    编程 2024-10-04

发表回复

登录后才能评论