python运算符

1.算术运算符

+ :加算运算符

int类型相加

>>> 12+13
25

str类型相加

>>> 'zhang'+'san'
'zhangsan'

float类型相加

>>> 12.1+12.3
24.4

float类型相加有精度误差

可使用round()内置函数精确小数位数   并且数据类型没变

>>> 0.2+0.1
0.30000000000000004
>>> a=0.1+0.2
>>> print(round(a,2))
0.3
>>> print(a)
0.30000000000000004

>>> print(type(round(a,2)))

  <class 'float'>

 float与int类型相加

>>> 1+3.0
4.0

int类型与str类型相加

int类型与float类型不能与str类型 做运算

>>> 'zhang'+1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>> 'zhang'+1.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not float

正数与负数相加

>>> -1+3
2
>>> -1+-2
-3
>>> 3+-1
2

- 减算运算

int类型相减

>>> 12-18
-6

str类型相减

str类型不能做减法运算

>>> 'zhang'-'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

float类型相减

注意精度误差,float类型的精度误差是每一种语言都有的

当值有精度误差时可使用round()内置函数精确小数位数   并且数据类型没变

>>> 25.3-1.3
24.0
>>> 2.0-1.8
0.19999999999999996
>>> a=2.0-1.8
>>> print(round(a,2))
0.2
>>> print(type(round(a,2)))
<class 'float'>

int类型与float类型相减

>>> 25-1.3
23.7

正数与负数相减

>>> 25--1.3
26.3
>>> -25--1.3
-23.7

*乘法运算

int类型相乘

>>> 3*3
9

str类型相乘

>>> 'zhang'*'san'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

float类型相乘

注意float精度误差

>>> 2.0*3.5
7.0

int类型与float类型相乘

>>> 2*3.5
7.0

int类型与str类型相乘

注意:int类型与str类型相乘结果是str

>>> 2*'3.5'
'3.53.5'
>>> 2*'zhang'
'zhangzhang'

正数与负数相乘

>>> 2*-2
-4
>>> -2*-2
4

/ 除法运算

int类型相除

python3x int类型相除结果为float类型

>>> 6/3
2.0

python2x int类型相除结果为int类型 只取整数

>>> 6 / 3
2
>>> 7 / 3
2

float类型相除

>>> 6.5/0.5
13.0

str类型相除

注意:str类型不能与str类型相除

>>> 'zhang'/'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'str'

int类型与float类型相除

>>> 6/1.5
4.0

int类型与str类型相除

注意:int类型不能与str类型相除

>>> 6/'zhang'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'str'

正数与负数相除

注意:python3x中结果为float类型 python2x中结果为int类型 只取整数

>>> 6/-3
-2.0
>>> -6/-3
2.0

** 乘方运算(幂运算)  a**b  结果为a的b次方

int类型乘方运算

>>> 2**3   #2的3次方
8

float类型乘方运算

>>> 1.5**1.5   #1.5的1.5次方
1.8371173070873836

str类型乘方运算

注意 :str不能与str做乘方运算

>>> 'zhang'**'san'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'

int类型与float类型乘方运算

>>> 2**1.5
2.8284271247461903

int类型str类型乘方运算

注意:int类型不能与str类型做乘方运算 但是可以做乘法运算 得出的结果是str

>>> 'zhang'**2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

正数与负数的乘方运算

>>> 2**-1.5
0.3535533905932738
>>> 2**-1.5
0.3535533905932738
>>> -2**-1.5
-0.3535533905932738
>>> -2**1.5
-2.8284271247461903
>>> -2**-2
-0.25

% 取模运算 a%b =a-(b*c)  c是商值

int类型中 a%b=a-(b*c) 

c=a/b c为正数时,商值c的取值规则向0取值

int类型取模运算

>>> 5%3    5/3 商值1或2   1<2  商值取1
2
>>> 6%3    商值只能取2     
0

float类型取模运算

>>> 5.0%1.5  5.0/1.5商值为3或4   3<4 商值取3
0.5

str类型取模运算

注意:str类型不能进行取模运算

>>> 'zhang'%'z'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

int类型与float类型取模运算

>>> 5%1.5
0.5
>>> 5%3.0
2.0

int类型与str类型取模运算

注意:int类型不能与str类型做取模运算

>>> 5%'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'int' and 'str'
>>> 5%'1'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'int' and 'str'

正数与负数取模运算

c=a/b c为负数时, 商值c的取值规则向负无穷取值

c=a/b c为正数时,商值c的取值规则向0取值

>>> 5%-3    5/-3商值为-1或-2  -2<-1 商值取-2    a%b=a-(b*c)=5-(-3*-2)=5-6=-1 
-1
>>> -5%-3  -5/-3商值为1或2  1<2 商职取1    a%b=a-(b*c)=-5-(-3*1)=-5-(-3)=-2
-2
>>> -5%3   -5/3商值为-1或-2  -2<-1 商职取-2   -5-(3*-2)=-5-(-6)=1
1

 //整除法 a//b=c c是商值 c=a/b

int类型整除

c=a/b c为正数时 商值c的取值规则向0取值

c=a/b c为负数时 商值c的取值规则向负无穷取值

>>> 5//3  5/3商值为1或2   1<2 商值取1   #向0取值
1
>>> 5//-3   5/-3商值为-1或-2   -2<-1  商值取-2 #向负无穷取值
-2
>>> -5//3   -5/3商值为-1或-2   -2<-1 商值取-2 #向负无穷取值
-2
>>> -5//-3  -5/-3商值为1或2  1<2  商值取1    #向0取值
1

float类型整除

>>> 5.5//1.5
3.0

str类型整除

注意:str类型不能做整除运算

>>> 'zhang'//'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for //: 'str' and 'str'
>>> '5'//'3'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for //: 'str' and 'str'

int类型与float类型整除

>>> 5//1.6
3.0

>>> 5.5//2
2.0

2.赋值运算符

=:简单的赋值运算符

>>> a=5    #一个等号为赋值  
>>> b=4
>>> c=a+b
>>> print(c)
9

+=:加法赋值运算符

>>> a=5
>>> b=3
>>> a+=b   #a+=b 等于 a=a+b   把a+b的值重新赋值给a   之所以a被称为变量  是因为值是不断变化的
>>> print(a)
8

-=:减法赋值运算符

>>> a=5
>>> b=3
>>> a-=b   #等于a=a-b
>>> print(a)
2

*=:乘法赋值运算符

>>> a=5
>>> b=3
>>> a*=b  #等于a=a*b
>>> print(a)
15

/=:除法赋值运算符

>>> a=5
>>> b=3
>>> a/=b   #等于a=a/b
>>> print(a)
1.6666666666666667

%=:取模赋值运算符

>>> a=5
>>> b=3
>>> a%=b   #等于a=a%b
>>> print(a)
2

**=:幂赋值运算符

>>> a=5
>>> b=3
>>> a**=b  #等于a=a**b
>>> print(a)
125

//=:取整赋值运算符

>>> a=5
>>> b=3
>>> a//=b  #等于a=a//b
>>> print(a)
1

3.python变量命名规则

  变量名由字母,数字,下划线组成 。

  但必须是字母或下划线开头。区分大小写,不能由数字开头

  变量名不能包含空格

  不能用python的保留关键字作为变量名

  _xx:前置单下划线变量名:私有属性或方法 但是并不能做到真正的 '私有' 只是约定俗称而已 这样写表示这个变量不希望在外部直接被调用,但是类对象和子类可以访问

  __xx:前置双下划线变量名:私有属性或方法 无法在外部直接访问。真正的私有 只允许类本身进行访问  连子类都不能访问

  __xx__:前后双下划线变量名:系统定义名字 对于python来说有特殊意义 一般用于类对象中的构造方法 普通变量应该避免这种命名风格

  xx_:后置单下划线变量名:用于避免python关键字冲突

 

>>> 5a=2   #数字开头的变量报错 
  File "<stdin>", line 1
    5a=2
     ^
SyntaxError: invalid syntax
>>> a2=2
>>> print(a2)
2
>>> a 2=2  #空格的变量名报错
  File "<stdin>", line 1
    a 2=2
      ^
SyntaxError: invalid syntax

4.python内存管理机制

  • 变量与对象 关系图如下

 

 

  • 变量:通过指针引用对象 指针指向具体对象的内存空间 取对象的值
  • 对象:类型已知 每个对象都包含一个头部信息(类型标识符和引用计数器)
  • 注意:变量没有类型,类型属于对象 变量引用的对象是什么类型 变量就是什么类型
  • id()是python的内置函数,用于返回对象的身份,即对象的内存地址
>>> a=5
>>> b='zhang'
>>> print(id(a))
1947509984
>>> print(id(b))
2910255636128
>>>
>>>
>>> c=b
>>> print(id(c))    #c与b引用的是同一个对象 同一个内存地址
2910255636128
>>> a=6   
>>> print(id(6))    #重新定义a时  内存地址会变
1947510016
  • 引用所指判断

 

 

  • python通过is进行引用判断 is用来判断所引用的对象类型是否相同
  • Python缓存了整数和短字符串,因此每个对象在内存中只存有一份,引用所指对象就是相同的,即使使用赋值语句,也只是创造新的引用,而不是对象本身
  • Python没有缓存长字符串、列表及其他对象,可以由多个相同的对象,可以使用赋值语句创建出新的对象

整数

>>> a=1
>>> b=1
>>> print(a is b)
True

短字符串

>>> a='hello'
>>> b='hello'
>>> print(a is b)
True

长字符串

>>> a='hello duoceshi'
>>> b='hello duoceshi'
>>> print(a is b)
False

列表

>>> a=[]
>>> b=[]
>>> print(a is b)
False

字典

>>> a={}
>>> b={}
>>> print(a is b)
False

 

  • python垃圾回收
  • 当python的某个对象的引用计数器为0时,说明没有任何变量引用指向该对象,该对象就成为要被回收的垃圾
  • 比如某个新建对象,被分配给某个引用,对象的引用计数变为1。如果引用被删除,对象的引用计数为0,那么该对象就可以被垃圾回收
  • del a后,已经没有任何引用指向之前建立的5,该表引用计数变为0,用户不可能通过任何方式接触或者动用这个对象,当垃圾回收启动时,Python扫描到这个引用计数为0的对象,就将它所占据的内存清空
>>> a=5
>>> del a

 5.比较运算符

==:比较两个对象是否相等  返回bool值  True或False

>>> a=5
>>> b=5
>>> print(a==b)
True
>>> a=5
>>> b=4
>>> print(a==b)
False

!=:比较两个对象是否不相等

>>> a=5
>>> b=4
>>> print(a!=b)
True
>>> a=5
>>> b=5
>>> print(a!=b)
False

>:比较一个对象是否大于另一个对象

>>> a=5
>>> b=4
>>> print(a>b)
True
>>> a=5
>>> b=5
>>> print(a>b)
False

<:比较一个对象是否小于另一个对象

>>> a=5
>>> b=6
>>> print(a<b)
True
>>> a=5
>>> b=4
>>> print(a<b)
False

>=:比较一个对象是否大于等于另一个对象

注意:>=中的符号是或的关系 满足其一即可

>>> a=5
>>> b=5
>>> print(a>=b)
True
>>> a=5
>>> b=6
>>> print(a>=b)
False

<=:比较一个对象是否小于等于另一个对象

>>> a=5
>>> b=6
>>> print(a<=b)
True
>>> a=5
>>> b=4
>>> print(a<=b)
False

6.逻辑运算符

and :and连接的条件必须同时满足

>>> a=5
>>> b=4
>>> c=5
>>> print(a>=c and a>b)
True
>>> a=5
>>> b=4
>>> c=5
>>> print(a>=c and a<b)
False

or:or连接的条件满足其一即可

>>> a=5
>>> b=4
>>> c=5
>>> print(a>=c or a<b)
True
>>> a=5
>>> b=4
>>> c=5
>>> print(a<=c or a==b)
True

not:not非 否定

>>> a=5
>>> b=4
>>> c=5
>>> print(not a>c)
True
>>> a=5
>>> b=4
>>> c=5
>>> print(not a<b)
True
>>> print(not a>b)
False

 

posted @ 2020-12-12 18:02  it-youzi  阅读(277)  评论(0)    收藏  举报
GenerateContentList();