Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

6. Python 运算符

Python操作符允许我们对变量进行公共处理。我们将通过示例和操作符优先级来研究不同类型的python操作符。

Python运算符

Python运算符是可以操作一个或多个操作数的值的特殊符号。

Python运算符类型

Python操作符可以分为几个类别。

  • 算术运算符
  • 逻辑运算符
  • 比较运算符
  • 位运算符
  • 赋值运算符

Python算术运算符

操作员说明例子
+用来加两个数总和=a+b
–个;用于减法差异=a和#8211;b
*用来乘以两个数。如果字符串与int相乘,则字符串将重复int次。mul=a*b

>;>;“;嗨”;*5‘;嗨嗨嗨’;

/用来除两个数的div=b/a公司
%模运算符,返回除法的余数模式=a%b
**指数运算符

#create two variables
a=100
b=200

# addition (+) operator
print(a+b) 

# subtraction (-) operator
print(a-b) 

# multiplication (*) operator
print(a*b)

# division (/) operator
print(b/a)

# modulus (%) operator
print(a%b) # prints the remainder of a/b

# exponent (**) operator
print(a**b) #prints a^b

输出:

Python算术运算符

Python比较运算符

操作员说明例子
==如果两个操作数相等,则返回True,否则返回False。标志=a==b
!=如果两个操作数不相等,则返回True,否则返回False。旗子=a!=b
>;如果左操作数大于右操作数,则返回True,否则返回False。标志=a>;b
<;如果左操作数小于右操作数,则返回True,否则返回False。标志=a<;b
>=如果左操作数大于或等于右操作数,则返回True,否则返回False。标志=a>;b
<=如果左操作数小于或等于右操作数,则返回True,否则返回False。标志=a<;b

# create two variables
a=100
b=200

# (==) operator, checks if two operands are equal or not
print(a==b)

# (!=) operator, checks if two operands are not equal
print(a!=b)

# (>) operator, checks left operand is greater than right operand or not
print(a>b)

# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)

# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)

Python比较运算符

Python位运算符

操作员说明例子
&;二进制与运算符x=10和#038;7=2
|二进制或运算符x=10 | 7=15
^二进制异或运算符x=10^7=13
~二元一补运算符x=~10=-11
<;<;二进制左移位算子x=10<;<;1=20
>;>;二元右移算子x=10>1=5

#create two variables
a=10 # binary 1010
b=7  # binary 0111

# Binary AND (&) operator, done binary AND operation
print(a&b)

# Binary OR (|) operator, done binary OR operation
print(a|b)

# Binary XOR (^) operator, done binary XOR operation
print(a^b)

# Binary ONEs Compliment (~) operator, done binary One"s Compliment operation
print(~a)

# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1) 
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)

Python位运算符

Python逻辑运算符

操作员说明例子
逻辑与运算符标志=exp1和exp2
逻辑或运算符标志=exp1或exp2
逻辑非运算符flag = not(True) = False

#take user input as int
a=int(input())

# logical AND operation

if a%4==0 and a%3==0:
    print("divided by both 4 and 3")

# logical OR operation
if a%4==0 or a%3==0:
    print("either divided by 4 or 3")

# logical NOT operation
if not(a%4==0 or a%3==0):
    print("neither divided by 4 nor 3")

Python逻辑运算符

Python赋值运算符

操作员说明
+=a+=b相当于a=a+b
*=a*=b相当于a=a*b
/=a/=b相当于a=a/b
%=a%=b相当于a=a%b
**=a**=b相当于a=a**b(指数运算符)
//=a//=b相当于a=a//b(楼层划分)


# take two variable, assign values with assignment operators
a=3
b=4

print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a+b
a+=b

print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a**b ( exponent operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a//b ( floor division)
a//=b
print("a: "+str(a))
print("b: "+str(b))

Python赋值运算符

Python运算符优先级

python运算符的优先级表示运算符的优先级。当一个表达式中有多个运算符时,这一点就变得至关重要了。例如,考虑以下表达式:


>>> 2+3*4

现在,你认为这一系列的手术会是什么?我们可以加上2和3,然后把结果乘以4。另外,我们可以先把3和4相乘,然后再加上2。在这里我们可以看到运算符的优先级很重要。

下面是表示优先级的运算符列表。它是按降序排列的。这意味着上一组的优先级高于上一组。

  1. 括号–;()
  2. 指数化;**
  3. 恭维,一元加减–;~, +, -
  4. 乘、除、模和#8211;*, /, %
  5. 加减法–;+, -
  6. 左右移动–;>>, <<
  7. 按位与–;&
  8. 按位“或”与“异或”–;|, ^
  9. 比较运算符–;==, !=, >, <, >=, <=
  10. 赋值运算符-=
您可以从我们的GitHub存储库.

参考文献:Python官方文档