亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2873 | 回復: 0
打印 上一主題 下一主題

Python 語法之操作符和表達式 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-10-23 18:44 |只看該作者 |倒序瀏覽


  Normal
  0
  
  
  
  7.8 磅
  0
  2
  
  false
  false
  false
  
  EN-US
  ZH-CN
  X-NONE
  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
  
  
   
   
   
   
   
   
   
   
   
   
   
  

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

/* Style Definitions */
table.MsoNormalTable
        {mso-style-name:普通表格;
        mso-tstyle-rowband-size:0;
        mso-tstyle-colband-size:0;
        mso-style-noshow:yes;
        mso-style-priority:99;
        mso-style-qformat:yes;
        mso-style-parent:"";
        mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
        mso-para-margin:0cm;
        mso-para-margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        font-size:10.5pt;
        mso-bidi-font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-ascii-font-family:Calibri;
        mso-ascii-theme-font:minor-latin;
        mso-hansi-font-family:Calibri;
        mso-hansi-theme-font:minor-latin;
        mso-font-kerning:1.0pt;}
Python 語法之操作符和表達式
File information
2009-10-23
磁針石:xurongzhong#gmail.com
博客:
oychw.cublog.cn
參考資料:
《Python Essential Reference 4th
Edition 2009》
《beginning python from novice to
professional second edition 2008》


*數(shù)值運算符
      
      
x + y 加法
x - y 減法
x * y 乘法
x / y 除法
x // y 整除
x ** y 乘方
x % y 取模
–x Unary minus
+x Unary plus

       Python 2,兩個整數(shù)相除結(jié)果為取模,Python 3,7/4得1,Python 3中改為用浮點運算,得1.75。Python 2導入:from __future__ import division,強制轉(zhuǎn)換。Future一般用于調(diào)用以后版本會實現(xiàn)的功能。

>>>
2.75 % 0.5
0.25

>>>
-3 ** 2
-9
>>>
(-3) ** 2
9

長整數(shù)的處理:
>>>
1000000000000000000
1000000000000000000L
       2.2及以前版本處理的范圍:2147483647 (or smaller than –2147483648)

      
       16進制:
       >>> 0xAF
175
八進制:
>>>
010
8
變量必須賦值才能使用。


位操作符:
              x
              x >> y Right shift
              x & y Bitwise and
              x | y Bitwise or
              x ^ y Bitwise xor (exclusive or)
              ~x Bitwise negation
       python在位運算不會自動截斷,要注意到是否會產(chǎn)生巨長的結(jié)果。

       其他運算符:
              abs(x) Absolute value
              divmod(x,y) Returns (x // y, x %
y)
              pow(x,y [,modulo]) Returns (x **
y) % modulo
              round(x,[n]) Rounds to the nearest
multiple of 10-n (floating-point numbers
              only)
       注意pow可以作為三元運算符,一般用于加密算法。
       round以遠離0為目標:round(0.5)得1,round(-0.5)得-1。Python 3中有點怪異:round(0.5)和round(0.5)得0,round(1.5)得2, round(-1.5)得-2。


       算數(shù)比較符:
              x
              x > y Greater than
              x == y Equal to
              x != y Not equal to
              x >= y Greater than or equal to
              x

       它們的連接,比如w ,理解為w
       python中的隱式轉(zhuǎn)換并不多。

*復合運算符
x +=y
x -=y
x *=y
x /=y
x //=y
x **=y
x %=y
x
&=y
x |=y
x ^=y
x
>>=y
x

              a = 3
              b = [1,2]
              c = "Hello %s %s"
              a += 1 # a = 4
              b[1] += 10 # b = [1, 12]
              c %= ("Monty",
"Python") # c = "Hello Monty Python"

*點號

*函數(shù)調(diào)用()
def
foo(x,y,z):
    return x+y+z
from
functools import partial
f =
partial(foo,1,2)
f(3)

       這個東東和currying進程很類似。

*函數(shù)調(diào)用()
def
foo(x,y,z):
    return x+y+z
from
functools import partial
f =
partial(foo,1,2)
f(3)

       這個東東和currying進程很類似。

*類型轉(zhuǎn)換
       int(x [,base]) Converts x to an integer.
base specifies the base if x
is a
string.
float(x)
Converts x to a floating-point number.
complex(real
[,imag]) Creates a complex number.
str(x)
Converts object x to a string representation.
repr(x)
Converts object x to an expression string.
format(x
[,format_spec]) Converts object x to a formatted string.
eval(str)
Evaluates a string and returns an object.
tuple(s)
Converts s to a tuple.
list(s)
Converts s to a list.
set(s)
Converts s to a set.
dict(d)
Creates a dictionary. d must be a sequence of
(key,value)
tuples.
frozenset(s)
Converts s to a frozen set.
chr(x)
Converts an integer to a character.
unichr(x)
Converts an integer to a Unicode character (Python 2
only).
ord(x)
Converts a single character to its integer value.
hex(x)
Converts an integer to a hexadecimal string.
bin(x)
Converts an integer to a binary string.
oct(x)
Converts an integer to an octal string.

a =
int("34") # a = 34
b =
long("0xfe76214", 16) # b = 266822164L (0xfe76214L)
b =
float("3.1415926") # b = 3.1415926
c =
eval("3, 5, 6") # c = (3,5,6)
      
*布爾類型操作符

x or y
If x is false, return y; otherwise, return x.
x and y
If x is false, return x; otherwise, return y.
not x If
x is false, return 1; otherwise, return 0.

*對象相等
相等:(x == y)
是同一對象:is

*運算順序
       除了乘方以外,都是由左至右的。
優(yōu)先級由高到低:
(...),
[...], {...} Tuple, list, and dictionary creation
s,
s[i:j] Indexing and slicing
s.attr
Attributes
f(...)
Function calls
+x, -x,
~x Unary operators
x ** y
Power (right associative)
x * y, x
/ y, x // y, x % y Multiplication, division, floor division, modulo
x + y, x
- y Addition, subtraction
x
> y Bit-shifting
x &
y Bitwise and
x ^ y
Bitwise exclusive or
x | y
Bitwise or
x
tests
x >
y, x >= y,
x == y,
x != y
x is y,
x is not y
x in s,
x not in s
not x
Logical negation
x and y
Logical and
x or y
Logical or
lambda
args: expr Anonymous function

*條件表達式
if a
minvalue
= a
else:
minvalue
= b

等同于:
minvalue
= a if a

values =
[1, 100, 45, 23, 73, 37, 69 ]
clamped
= [x if x
print(clamped)
# [1, 50, 45, 23, 50, 37, 50]

               
               
               

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/21908/showart_2076986.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP