CodeSec QQȺ
| ¼
δDZ

Pythonȫջ֮·ϵ֮

С | | С
[python python | С05 | ʱ 2017 | ] 0ղղ

ƪǼ򵥵pythonԻ﷨ôڼȥĹУѧϰPythonͣҲǴ󲿷Python̿ѧϰĿ¼dz롣

Pythonõļ;¼֣ʵPythonڲͻǺܶģ൫Ǻõˣ̫ҲDzˣѳõļ˾OKˡ

ôǻѧЩõأ


Pythonȫջ֮·ϵ֮

Ȼ˵ѧϰͣʵֻѧϰÿһṩAPIѣҪĴ󲿷ֹܣPythonѾǷװˣҪκЧʵ⣬ϤЩAPI֮ӦãΪڿĹDZزٵģôǿʼAPIѧϰ֮ðɡ

е߱ķӦ棬һ͵Ķʱö߱ĹܶӦС

Python3Уֵ͡͡ȶԳ֮Ϊ͡

͵Ķ

int ֣ͨ͵ķʽ֣ڴʱֵ߲Ҫ˫Żš

һִ͵ķʽ

>>> number = 9 >>> type(number) <class 'int'>

ڶִ͵ķʽ

>>> number = int(9) >>> type(number) <class 'int'>

ִͶķʽԴģҲбϵ𣬵һַʽʵϻתɵڶַʽȻڶַʽڵݽ __init__ 췽췽 int ģȻ췽ڴпһռݣʵʱûκġ

췽ÿжУPythonڲģͼʾ


Pythonȫջ֮·ϵ֮

__init__

def __init__(self, x, base=10): # known special case of int.__init__

ԴԴп __init__ ķ base=10 ǿѡIJ x Ƕֵ base=10 ʵ˵ǵֵ(Ĭ϶)ʮƵķʽͨʵԿ

>>> var=int('0b100',base=2) >>> var 4

ͨint()Խһֵַһָ˵ڶԽֵתΪ

# ַתΪַͨתΪ >>> int('99'),int('100',8),int('40',16),int('10000000',2) (99, 64, 64, 128) # תΪ >>> int('0x40',16),int('0b1000000',2) (64, 64)

ѶƵ4ͨʮ4Ķƾ 0b100 һ֪ʶķУ __ ͷ __ βķPythonڲԼȥõģдĹDzҪȥõģ򵥵Ӿ __init__ ͨͼǾͿԺĿ

intڲŻ

ͼǿԺĿintڴʱڴռ


Pythonȫջ֮·ϵ֮

֪Ǵһvar1ʱڴпһռΪvar1ֵõģǴڶvar2ʱҲڴпһռΪvar2ֵ˵Dz˵var1var2ֵڴǷͬʱأͨʵԵõ𰸣

C:\Users\anshe>c:\Python35\python.exe # עõPython3.5.1 Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2016, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. # ֱ𴴽var1var2 >>> var1=123 >>> var2=123 # ǿԿǵڴַָ`1502084720` >>> id(var1) 1502084720 >>> id(var2) 1502084720

ͨĽǿԿvar1var2ڴַͬģʹǵֵʹõͬһռ䣬ôҰvar2ֵΪ456أ

>>> var2=456 >>> id(var1) 1502084720 >>> id(var2) 2452305956816

Կvar2ڴַѾıˣϻΪֵһˣŲı䣬OKǿԵõһ۾ǣ߶ֵͬһʱôЩ󶼻ʹͬһڴֵַзΧģĬϷΧ -5~257 õ֮Ǽ¿


Pythonȫջ֮·ϵ֮

ͼͬ󣬵ΨһͬҰѵһĶֵΪڶֵǿ϶ʹõͬһڴַҰѵһֵĶأ

>>> var1=123 >>> var2=var1 >>> id(var1) 1502084720 >>> id(var2) 1502084720 >>> var1=456 >>> id(var1) 2452305956816 >>> id(var2) 1502084720

˼ﲻͣȻ˵˵ղŵĻ⣬˵ -5~257 Χڶֵͬһڴַǿͨʵԣ

>>> var1=12345 >>> var2=12345 >>> id(var1) 2452305956816 >>> id(var2) 2452308384720

ʵ֤ǵĽȫûģעʵ Python3.5 ִеŶ var1 var2 ֵͬ12345ǵڴַDzһPythonڲŻ -5~257 ΧdzõֶõģOK˽⡣

͵ij

python2.7 ǷͺͳģҲ˵ִһķΧôpythonתΪΣһͰ32λԴ洢 -2147483648 214483647

һ (long) ͻռøĿռ䣬64λĿԴ洢 -922372036854775808 922372036854775808

python3longѾˣintͿԴ洢Сͣ64Ϊ

PythonڲĴΪͨͳͨΪλͨ32λΧԶķΧȫûƣ£

Python2.7.x

>>> var=123456 >>> var 123456 >>> var=10**20 >>> var 100000000000000000000L >>> type(var) # longdz <type 'long'>

Python3.5.x

>>> var=123456789 >>> var 123456789 >>> var=10**20 >>> var 100000000000000000000 >>> type(var) <class 'int'>

в - -

߱ķ bit_length

رʾʱռõλ

>>> num=20 >>> num.bit_length() 5 conjugate

ظøĹ0+2j,num.real,num.imagֱ𷵻ʵ鲿num.conjugate()乲

>>> num =-20 >>> num.conjugate() -20 >>> num=0+2j >>> num.real 0.0 >>> num.imag 2.0 >>> num.conjugate() -2j imag

ظ

>>> number = 10 >>> number.imag 0 >>> number = 3.1415926 >>> number.imag 0.0

õķ denominator from_bytes numerator real to_bytes ʵڸ㲻ʲôãҲ̫⣬Ͳˣͨ help(int.numerator) 鿴÷İϢȡ

ν;Ǹ㣬ʾ

>>> 3.14159 + 10 13.14159

еһһһôӵأʵܼ򵥣PythonֵתΪӵǸͣȻٶͬ㡣

УȰ 10 תΪ 10.0 Ȼ 3.14159 ӡ

͵ĸӶ

ȸ򵥡ȸ򵥡

(bool)

ʵ01ıֶ 棨True/0 ٣False/1 ʵϾõ͵ѡ

# 0棬ô'0 is False.' >>> if not 0: print('0 is False.') ... 0 is False. # 1棬ô'1 is True.' >>> if 1: print('1 is True.') ... 1 is True.

㻹ʹòֵмӼȻûκδмʽ

>>> True + 1 # 1 + 1 = 2 2 >>> False + 1 # 0 + 1 = 1 1 (set)

ϵԪDzظɱļϣϾֵֵһеԪֻܹһҲظ

set

>>> s = set([11,22,33]) >>> s {33, 11, 22} >>> type(s) <class 'set'>

ڶֲôsetϵķʽ

# ֵĴʽеԪ൱ֵеkey >>> s = {11,22,33} >>> type(s) <class 'set'> >>> s {33, 11, 22}

ɵתΪset

>>> li = ["a","b","c"] >>> seting = set(li) >>> seting {'b', 'a', 'c'} >>> type(seting) <class 'set'>

ֱ֧ͬʽ

# ȴ >>> x = set('abcde') >>> y = set('bdxyz') >>> x {'a', 'd', 'b', 'c', 'e'} >>> y {'y', 'd', 'b', 'x', 'z'} # ʹinгԱ >>> 'a' in x True #  >>> x - y {'a', 'e', 'c'} # >>> x | y {'b', 'y', 'z', 'a', 'd', 'e', 'c', 'x'} # >>> x & y {'d', 'b'} # ԳƲ >>> x ^ y {'y', 'z', 'a', 'e', 'c', 'x'} # Ƚ >>> x > y, x < y (False, False)

Ͻ

>>> {x for x in 'abc'} {'a', 'b', 'c'} >>> {x+'b' for x in 'abc'} {'bb', 'cb', 'ab'} ṩķ add

Ԫ

>>> se = { 11, 22, 33 } >>> se {33, 11, 22} # ԪдС >>> se.add(44) >>> se {33, 11, 44, 22}

clear

>>> se = { 11, 22, 33 } >>> se {33, 11, 22} >>> se.clear() >>> se set()

copydz

Ľ

difference

ѰҼϵԪvar1дڣvar2вڵ

>>> var1 = { 11, 22, 33 } >>> var2 = { 22 ,55 } >>> var1.difference(var2) {33, 11} >>> var2.difference(var1) {55}

difference_update

ѰҼϵԪvar1дڣvar2вڵԪأѲҳԪ¸Ƹvar1

>>> var1 = { 11, 22, 33 } >>> var2 = { 22 ,55 } >>> var1.difference_update(var2) >>> var1 {33, 11}

discard

ƳָԪأڲ

>>> var1 = { 11, 22, 33 } >>> var1.discard(11) >>> var1 {33, 22} >>> var1.discard(1123123) >>> var1 {33, 22} remove

ƳָԪأڱ

>>> var1 = { 11, 22, 33 } >>> var1 {33, 11, 22} >>> var1.remove(11) >>> var1 {33, 22} >>> var1.remove(asda) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'asda' is not defined intersection

Ԫжڵֵ

>>> var1 = { 11, 22, 33 } >>> var2 = { 22, 55, "һ" } >>> var1.intersection(var2) {22}

intersection_update

ȡµA

>>> var1 = { 11, 22, 33 } >>> var2 = { 22, 55, "һ" } >>> var1.intersection_update(var2) >>> var1 {22}

isdisjoint

жûнзFalse򷵻True

>>> var1 = { 11, 22, 33 } >>> var2 = { 22, 44, 55 } >>> var1.isdisjoint(var2) False >>> var2 = { 66, 44, 55 } >>> var1.isdisjoint(var2) True

issubset

Ƿ,Ҳ˵var2Ԫضvar1ˣôvar2var1

>>> var1 = {11,22,33,44} >>> var2 = {11,22} >>> var2.issubset(var1) True issuperset

ǷǸ

>>> var1 = { 11, 22, 33 } >>> var2 = { 22, 44, 55 } >>> var1.issuperset(var2) True pop

ƳһԪأʾƳԪأƳʱ

>>> var1 = {11,22,33,44} >>> var1.pop() 33 >>> var1 {11, 44, 22} symmetric_difference

Գƽvar1bںvar2var1ڵԪغһ

>>> var1 = { 11, 22, 33, 44 } >>> var2 = { 11, 22, 77, 55 } >>> var1.symmetric_difference(var2) {33, 44, 77, 55} symmetric_difference_update

Գƽµvar1

>>> var1 = { 11, 22, 33, 44 } >>> var2 = { 11, 22, 77, 55 } >>> var1 {33, 11, 44, 22} >>> var1.symmetric_difference_update(var2) >>> var1 {33, 44, 77, 55} union

еԪطһظֻһ

>>> var1 = { 11, 22, 33, 44 } >>> var2 = { 11, 22, 77, 55 } >>> var1.union(var2) {33, 11, 44, 77, 22, 55} update

£һеԪظµһ

>>> var1 = { 11, 22, 33, 44 } >>> var2 = { 11, 22, 77, 55 } >>> var1.update(var2) >>> var1 {33, 11, 44, 77, 22, 55} ԭ Pythonȫջ֮·ϵ

Ŀpython:python̳ python߳ webʦ ʦ

ҳ12
תע
ı⣺Pythonȫջ֮·ϵ֮
վӣhttp://www.codesec.net/view/522153.html


1.CodeSecTeamתص,ý,ĿڴݸϢ,վͬ۵ʵԸ
2.תص½ԭ߹۵,뱾վ޹ءԭԼгֺδվ֤ʵ,վԸԼȫ߲ݡֵʵԡԡʱԣκα֤
3.籾վתظ漰Ȩ,߼ʱϵվ,ǻἰʱ
¼ӵղ¡עߵȨ...
| python | (0) | Ķ(14)