Python入门——Python基础(二)

复合运算符号

+=     y += x    ------>     y = y + x 
-= 
*= 
/=      y /= x     ------->    y = y / x
//=
%=
**= 

换行符

\ 在书写中连接换行位置

内置函数

python中自带的函数,可以直接使用 内置函数列表如下
Python入门——Python基础(二)

abs( )函数
>   Return the absolute value of a number. 返回数字的绝对值。
>   The argument may be an integer or a floating point number. 参数可以是整数或浮点数。
>   If the argument is a complex number, its magnitude is returned. 如果参数是复数,则返回其大小。
>   abs(-3)    ----->  3
round( )语法结构
>   round(number[, ndigits])  默认对number四舍五入到整数
>   ndigits 表示 保留小数点后几位
>  round(3.241541658,5)   -------->   3.24154
>  round(3.241541658)     -------->   3
>  round(3.56)  		  -------->   4
>  

强制类型转换

float()   强制转换为浮点型
int()     强制转换为整型(舍去精度)
bool()    强制转换为布尔类型
x = float(5)   ----->    5.0 
y = int(3.5)   ----->    3
z = bool(0)    ----->    False
m = bool(1)    ----->    True 
n = bool(-5)   ----->    True
print(x,y,z,m,n)
type( ) 查看一个变量/常量的类型
a = 1.2
type(a)
float    浮点型

逻辑运算符

逻辑运算符and、or、not常用来连接条件表达式构成更加复杂的条件表达式, and和or具有惰性求值特点,只计算必须计算的表达式。
另外要注意的是,运算符and和or并不一定会返回True或False,而是得到最后一个被计算的表达式的值,但是运算符not一定会返回True或False。

3>5 and a>3     #注意,此时并没有定义变量a		False
3>5 or a>3      #3>5的值为False,所以需要计算后面表达式
NameError: name 'a' is not defined
3<5 or a>3      #3<5的值为True,不需要计算后面表达式	True
3 and 5         #最后一个计算的表达式的值作为整个表达式的值	  5
3 and 5>2	True
3 is not 5           #not的计算结果只能是True或False之一	True