python3常见操作

系统 1404 0
阅读更多

 

 布尔操作,是或者否:

Operation

Result

Notes

x   or   y

if  x  is false, then  y , else  x

(1)

x   and   y

if  x  is false, then  x , else  y

(2)

not   x

if  x  is false, then  True , else  False

(3)

 

 

比较操作,大还是小,是否相同对象,是否相同值:

Operation

Meaning

<

strictly less than

<=

less than or equal

>

strictly greater than

>=

greater than or equal

==

equal

!=

not equal

is

object identity

is   not

negated object identity

 

 

常用数学计算:

Operation

Result

Notes

Full documentation

x   +   y

sum of  x  and  y

   

x   -   y

difference of  x  and  y

   

x   *   y

product of  x  and  y

   

x   /   y

quotient of  x  and  y

   

x   //   y

floored quotient of  x  and  y

(1)

 

x   %   y

remainder of  x   /   y

(2)

 

-x

x  negated

   

+x

x  unchanged

   

abs(x)

absolute value or magnitude of  x

 

abs()

int(x)

x  converted to integer

(3)(6)

int()

float(x)

x  converted to floating point

(4)(6)

float()

complex(re,   im)

a complex number with real part  re , imaginary part  im im  defaults to zero.

(6)

complex()

c.conjugate()

conjugate of the complex number  c

   

divmod(x,   y)

the pair  (x   //   y,   x   %   y)

(2)

divmod()

pow(x,   y)

x  to the power  y

(5)

pow()

x   **   y

x  to the power  y

(5)

 

 

字符串常见操作:

Operation

Result

Notes

x   in   s

True  if an item of  s  is equal to  x , else  False

(1)

x   not   in   s

False  if an item of  s  is equal to  x , else  True

(1)

s   +   t

the concatenation of  s  and  t

(6)(7)

s   *   n  or  n   *   s

equivalent to adding  s  to itself  n  times

(2)(7)

s[i]

i th item of  s , origin 0

(3)

s[i:j]

slice of  s  from  i  to  j

(3)(4)

s[i:j:k]

slice of  s  from  i  to  j  with step  k

(3)(5)

len(s)

length of  s

 

min(s)

smallest item of  s

 

max(s)

largest item of  s

 

s.index(x[,   i[,   j]])

index of the first occurrence of  x  in  s  (at or after index  i  and before index  j )

(8)

s.count(x)

total number of occurrences of  x  in  s

 

 

 

数组常见操作:

Operation

Result

Notes

s[i]   =   x

item  i  of  s  is replaced by  x

 

s[i:j]   =   t

slice of  s  from  i  to  j  is replaced by the contents of the iterable  t

 

del   s[i:j]

same as  s[i:j]   =   []

 

s[i:j:k]   =   t

the elements of  s[i:j:k]  are replaced by those of  t

(1)

del   s[i:j:k]

removes the elements of  s[i:j:k] from the list

 

s.append(x)

appends  x  to the end of the sequence (same as  s[len(s):len(s)]   =   [x] )

 

s.clear()

removes all items from  s  (same as  del   s[:] )

(5)

s.copy()

creates a shallow copy of  s  (same as  s[:] )

(5)

s.extend(t)  or  s   +=   t

extends  s  with the contents of  t  (for the most part the same as s[len(s):len(s)]   =   t )

 

s   *=   n

updates  s  with its contents repeated  n  times

(6)

s.insert(i,   x)

inserts  x  into  s  at the index given by  i (same as  s[i:i]   =   [x] )

 

s.pop([i])

retrieves the item at  i  and also removes it from  s

(2)

s.remove(x)

remove the first item from  s  where  s[i]  is equal to  x

(3)

s.reverse()

reverses the items of  s  in place

(4)

 

 

常见格式化占位符:

Conversion

Meaning

Notes

'd'

Signed integer decimal.

 

'i'

Signed integer decimal.

 

'o'

Signed octal value.

(1)

'u'

Obsolete type – it is identical to  'd' .

(6)

'x'

Signed hexadecimal (lowercase).

(2)

'X'

Signed hexadecimal (uppercase).

(2)

'e'

Floating point exponential format (lowercase).

(3)

'E'

Floating point exponential format (uppercase).

(3)

'f'

Floating point decimal format.

(3)

'F'

Floating point decimal format.

(3)

'g'

Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.

(4)

'G'

Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.

(4)

'c'

Single character (accepts integer or single character string).

 

'r'

String (converts any Python object using  repr() ).

(5)

's'

String (converts any Python object using  str() ).

(5)

'a'

String (converts any Python object using  ascii() ).

(5)

'%'

No argument is converted, results in a  '%'  character in the result.

 

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论