网站首页 > 精选文章 / 正文
Learn Python Operators Easily: A Beginner's Guide for Students 运算符
2025-05-23 17:36 huorong 精选文章 2 ℃ 0 评论Introduction
Hello, students! Today we will learn about operators in Python. An operator is a symbol that tells the computer to perform specific operations (操作). Understanding operators is essential (必要的) for writing Python programs. Let’s start with the most common ones!
1. Arithmetic Operators (算术运算符)
These operators are used for math calculations.
Operator | Name | Example | Result |
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 3 | 1.666... |
% | Modulus (取余) | 5 % 3 | 2 |
// | Floor Division (整除) | 5 // 3 | 1 |
** | Exponent (指数) | 5 ** 3 | 125 |
Code Example:
a = 10
b = 3
print(a + b) # Output: 13
print(a * b) # Output: 30
print(a ** b) # Output: 1000 (10×10×10)
2. Assignment Operators (赋值运算符)
These operators are used to assign values to variables.
Operator | Example | Same As |
= | x = 5 | Set x to 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
//= | x //= 3 | x = x // 3 |
**= | x **= 3 | x = x ** 3 |
Code Example:
x = 5
x += 2 # Now x is 7
x *= 3 # Now x is 21
print(x) # Output: 21
3. Comparison Operators (比较运算符)
These operators compare values and return True or False.
Operator | Name | Example | Result |
== | Equal to | 5 == 3 | False |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater or equal | 5 >= 5 | True |
<= | Less or equal | 5 <= 3 | False |
Code Example:
a = 8
b = 8
print(a == b) # Output: True
print(a != b) # Output: False
print(a >= 10) # Output: False
4. Logical Operators (逻辑运算符)
These operators combine conditional (条件的) statements.
Operator | Description | Example |
and | True if both conditions are true | (5 > 3) and (8 < 10) |
or | True if at least one condition is true | (5 > 3) or (8 > 10) |
not | Reverse the result (取反) | not (5 == 3) |
Code Example:
age = 18
has_ticket = True
if age >= 18 and has_ticket:
print("Can enter.") # Output: Can enter.
else:
print("Cannot enter.")
5. Membership Operators (成员运算符)
These operators check if a value exists in a sequence (序列, like a list or string).
Operator | Description | Example |
in | True if value is present | 'a' in 'apple' |
not in | True if value is NOT present | 'b' not in 'apple' |
Code Example:
fruits = ["apple", "banana"]
print("apple" in fruits) # Output: True
print("orange" not in fruits) # Output: True
6. Identity Operators (身份运算符)
These operators check if two variables refer to the same object.
Operator | Description | Example |
is | True if same object | a is b |
is not | True if not the same object | a is not b |
Code Example:
x = [1, 2, 3]
y = x
print(x is y) # Output: True (same object)
z = [1, 2, 3]
print(x is z) # Output: False (different objects)
Conclusion
Operators are the building blocks (基石) of Python programming! Practice using them in simple programs to get familiar.
轻松学 Python 运算符:学生初学者指南
引言
同学们好!今天我们将学习Python中的运算符(operator)。运算符是告诉计算机执行特定操作的符号。理解运算符是编写Python程序的必要技能。让我们从最常见的开始吧!
1. 算术运算符
这些运算符用于数学计算。
运算符 | 名称 | 示例 | 结果 |
+ | 加法 | 5 + 3 | 8 |
- | 减法 | 5 - 3 | 2 |
* | 乘法 | 5 * 3 | 15 |
/ | 除法 | 5 / 3 | 1.666... |
% | 取余 | 5 % 3 | 2 |
// | 整除 | 5 // 3 | 1 |
** | 指数 | 5 ** 3 | 125 |
代码示例:
a = 10
b = 3
print(a + b) # 输出:13
print(a * b) # 输出:30
print(a ** b) # 输出:1000(10×10×10)
2. 赋值运算符
这些运算符用于为变量赋值。
运算符 | 示例 | 等同于 |
= | x = 5 | 设置x为5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
//= | x //= 3 | x = x // 3 |
**= | x **= 3 | x = x ** 3 |
代码示例:
x = 5
x += 2 # 现在x是7
x *= 3 # 现在x是21
print(x) # 输出:21
3. 比较运算符
这些运算符用于比较值,返回True或False。
运算符 | 名称 | 示例 | 结果 |
== | 等于 | 5 == 3 | False |
!= | 不等于 | 5 != 3 | True |
> | 大于 | 5 > 3 | True |
< | 小于 | 5 < 3 | False |
>= | 大于等于 | 5 >= 5 | True |
<= | 小于等于 | 5 <= 3 | False |
代码示例:
a = 8
b = 8
print(a == b) # 输出:True
print(a != b) # 输出:False
print(a >= 10) # 输出:False
4. 逻辑运算符
这些运算符用于组合条件语句。
运算符 | 描述 | 示例 |
and | 两个条件都为真时返回True | (5 > 3) and (8 < 10) |
or | 至少一个条件为真时返回True | (5 > 3) or (8 > 10) |
not | 反转结果(取反) | not (5 == 3) |
代码示例:
年龄 = 18
有票 = True
if 年龄 >= 18 and 有票:
print("可以进入。") # 输出:可以进入。
else:
print("不能进入。")
5. 成员运算符
这些运算符用于检查值是否存在于序列中(如列表或字符串)。
运算符 | 描述 | 示例 |
in | 值存在时返回True | 'a' in 'apple' |
not in | 值不存在时返回True | 'b' not in 'apple' |
代码示例:
水果 = ["苹果", "香蕉"]
print("苹果" in 水果) # 输出:True
print("橙子" not in 水果) # 输出:True
6. 身份运算符
这些运算符用于检查两个变量是否引用同一个对象。
运算符 | 描述 | 示例 |
is | 是同一个对象时返回True | a is b |
is not | 不是同一个对象时返回True | a is not b |
代码示例:
x = [1, 2, 3]
y = x
print(x is y) # 输出:True(同一个对象)
z = [1, 2, 3]
print(x is z) # 输出:False(不同对象)
结论
运算符是Python编程的基石!通过在简单程序中练习使用它们来熟悉。
专业词汇及不常用词汇表
- operator, /'pret(r)/, n,运算符
- essential, /'senl/, adj,必要的
- arithmetic, /'rθmtk/, adj,算术的
- modulus, /'mdjls/, n,取余
- floor division, /fl(r)/ /d'vn/, n,整除
- exponent, /k'spnnt/, n,指数
- assignment, /'sanmnt/, n,赋值
- comparison, /km'paersn/, n,比较
- logical, /'ldkl/, adj,逻辑的
- conditional, /kn'dnl/, adj,条件的
- membership, /'membp/, n,成员关系
- sequence, /'sikwns/, n,序列
- identity, /a'dentti/, n,身份
- object, /'bdkt/, n,对象
- control flow, /kn'trl/ /fl/, n,控制流
Tags:less全局变量
猜你喜欢
- 2025-05-23 TED演讲:少抱怨外部因素、多思考如何解决问题(中英文)
- 2025-05-23 Linux环境变量配置全攻略
- 2025-05-23 全网惊了!陶哲轩带AI下场,33分钟「盲证」数学
- 2025-05-23 大前端,这可能是最走心的Vue3组件库——Naive UI
- 2025-05-23 正确复制、重写别人的代码,不算抄袭
- 2025-05-23 「技术分享」postman完整的接口测试
- 2025-05-23 说下你的vue项目的目录结构,该怎么划分?
- 2025-05-23 Bash技巧:使用 shift 内置命令左移前几个命令参数
- 2025-05-23 基于ES6,使用React、Webpack、Babel构建模块化JavaScript应用
- 2025-05-23 Linux 常用命令集合