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 23:28
下一篇 2024-10-03 23:28

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29

发表回复

登录后才能评论