Python运算符

 

Python运算符

 

  • 算术运算符

  • 比较(关系)运算符

  • 赋值运算符

  • 逻辑运算符

  • 成员运算符

  • 位运算符

  • 身份运算符

  • 运算符优先级

 

1.算术运算符

 

 

实例:

 

复制代码
----------------加法-----------------
a = 10
b = 3
print(a + b)
--------------运算结果--------------
13


-----------------减法-----------------
a = 10
b = 3
print(a - b)
--------------运算结果---------------
7


-----------------乘法----------------
a = 10
b = 3
print(a * b)
--------------运算结果---------------
30


---------------除法-------------------
a = 10
b = 3
print(a / b)
--------------运算结果---------------
3.3333333333333335


---------------取模------------------
a = 10
b = 3
print(a % b)
---------------运算结果--------------
1


--------------幂----------------------
a = 10
b = 3
print(a ** b)
---------------运算结果--------------
1000


-----------------取整除--------------
a = 10
b = 3
print(a // b)
---------------运算结果----------------
3
复制代码

 

2.比较运算符

 

 

例子:

 

复制代码
a = 10
b = 3

if a == b :
    print("a等于b")
else :
    print("a不等于b")

if a < b :
    print("a小于b")
else :
    print("a大于b")
-------------输出结果------------------
a不等于b
a大于b
复制代码

 

3.赋值运算符

 

 

例子:

 

复制代码


a = 10 b = 5 c = a + b print(c) a = 20 c = 30 c+=a print(c) -----------------输出结果------------ 15 50
复制代码

 

4.逻辑运算符

 

 

运算逻辑符判断的顺序:括号()-----> not -----> and ----->or

 

x or y:

 

若 x 是零,输出y;若x是非零,输出x

 

 

例子:

 

1.判断1 == 3 or 4 == 4 and 2 == 3 or 3 == 3的结果

 

复制代码
print(1 == 3 or 4 == 4 and 2 == 3 or 3 == 3)
-------------输出结果--------------------
True

-----------------------------------------
快速判断小秘诀
真 or = 真 真 and = 继续 假 or = 继续 假 and = 假
复制代码

 

2.求出下列逻辑语句的值

 

  1)8 or 3 and 4 or 2 and 0 or 9 and 7

 

  2)  0 or 2 and 3 and 4 or 6 and 0 or 3

 

复制代码
print(8 or 3 and 4 or 2 and 0 or 9 and 7)

print(0 or 2 and 3 and 4 or 6 and 0 or 3)

---------------------------输出结果------------------------------

8

4
复制代码

 

3.下列结果是什么?

 

  1) 6 or 2>1        2) 3 or 2>1        3) 0 or 5<4        4) 5<4 or 3       5) 2>1 or 6

 

  6) 3 and 2>1          7) 0 and 3>1           8) 2>1 and 3          9) 3>1 and 0

 

    10) 3>1 and 2 or 2<3 and 3 and 4 or 3>2

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@wolaixunshan ~]# cat py.py
#!/usr/bin/python3
#-*- coding:UTF-8 -*-
#Author:zjk
#mail:zjkmmd@163.com
#Time:2018-07-28 09:44:28
#Name:py.py
#Version:V1.0
#Description:This is a script.
print(6 or 2>1,3 or 2>1,0 or 5<4,5<4 or 3,2>1 or 6)
print(3 and 2>1,0 and 3>1,2>1 and 3,3>1 and 0,3>1 and 2 or 2<3 and 3 and 4 or 3>2)
 
---------------------------------------输出结果--------------------------------------
 
[root@wolaixunshan ~]# python3 py.py
6 3 False 3 True
True 0 3 0 2

 

5.成员运算符

 

 

例子:

 

复制代码
a = int(input(">>>"))
b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
if a in b :
    print("a在b中")
else :
    print("a不在b中")
-----------输出结果------------------
>>>4
a在b中

>>>101
a不在b中
复制代码

 

复制代码
a = int(input(">>>"))
b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
if a not in b :
    print("a不在b中")
else :
    print("a在b中")
-----------------输出结果------------------
>>>10
a不在b中

>>>3
a在b中
复制代码

 

6.位运算符

 

按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:

 

下表中变量 a 为 60,b 为 13,二进制格式如下:

 

复制代码
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011
复制代码

 

 

例子:

 

复制代码
a = 60           # 60 = 0011 1100
b = 13           # 13 = 0000 1101
                 # --------------
print(a & b)     # 12 = 0000 1100
---------------输出结果----------------
12


--------------------------------------
a = 60          # 60 = 0011 1100
b = 13          # 13 = 0000 1101
                # --------------
print(a | b)    # 61 = 0011 1101
---------------输出结果----------------
61


--------------------------------------
a = 60           # 60 = 0011 1100
b = 13           # 13 = 0000 1101
                 # --------------
print(a ^ b)     # 49 = 0011 0001
----------------输出结果----------------
49


---------------------------------------
a = 60          # 60 = 0011 1100
b = 13          # 13 = 0000 1101
                # --------------
print(~a)       # -61 = 1100 0011, ~a相当于-a-1 = -60-1 = -61
----------------输出结果----------------
-61


--------------------------------------
a = 60          # 60 = 0011 1100
b = 13          # 13 = 0000 1101
                # --------------
print(a << 2)   #240 = 1111 0000
-----------------输出结果--------------
240


--------------------------------------
a = 60          # 60 = 0011 1100
b = 13          # 13 = 0000 1101
                # --------------
print(b >> 2)   #  3 = 0000 0011
------------------输出结果-------------
3
复制代码

 

7.身份运算符

 

身份运算符用于比较两个对象的存储单元(内存地址)是否一样;

 

 

注: id() 函数用于获取对象内存地址。

 

说明:

 

只要是列表、字典、元组、set集合,这些的数据类型的相同值的内存地址一定不一样;

 

小数据池:

 

str:长度超过20位的、有特殊字符的比如空格,-+*等等;内存地址都不一样;

 

int:范围-5~256,在此范围内创建的相同数字都指向同一个内存地址,包括-5和256;

 

 

例子:

 

复制代码
------------------------------------------------
a = 10
b = 10
print(id(a))        #获取变量a的内存地址
print(id(b))        #获取变量b的内存地址
if a is b :
    print("True")
else :
    print("False")
----------------------输出结果------------------
1613092032      #可以看出a的内存地址和b的内存地址一样,说明a和b引
1613092032      #用的是同一个对象              
True
复制代码

 

注意:

 

is 与 == 区别;

 

is用于判断两个变量引用对象是否为同一个;==用于判断引用变量的值是否相等。

 

例如:

 

复制代码
----------  is  --------------
a = str(10)
b = "10"
print(id(a))
print(id(b))
if a is b :
    print("True")
else :
    print("False")
------------输出结果------------
35284608
30655712
False


----------  ==  ---------------
a = str(10)
b = "10"
print(id(a))
print(id(b))
if a == b :
    print("True")
else :
    print("False")
-----------输出结果---------------
8676992
2147552
True
复制代码

 

8.运算符优先级

 

一下表格列出了从最高到最低优先级的所有运算符:

 

 

例子:

 

复制代码
a = (1 + 3) * 4 / 2
b = ((2 + 7) / 3 + 10 * 2)
c = (3 + 9) * (2 ** 3)
print(a)
print(b)
print(c)
----------------输出结果--------------------
8.0
23.0
96

 

原文地址:https://www.cnblogs.com/xiangweilai/p/9438285.html