1. 程式人生 > 實用技巧 >python3之變數和資料型別

python3之變數和資料型別

1 變數

a 命名格式

var=value
var1,var2,var3=value1,value2,value3

變數可以是任意資料型別,value是字串時必須要用單引號('')或者雙引號("")括起來

b變數名(var)規則

1 變數名只能包含字母、數字和下劃線,但不能有和數字開頭
2 變數名不能包含空格,但可使用下劃線來分隔其中的單詞
3 不要將Python關鍵字和函式名用作變數名

2 資料型別

3 python3關鍵字

False

None

True

and

as

assert

break

class

continue

def

del

elif

else

except

finally

for

from

global

if

import

in

is

lambda

nonlocal

not

or

pass

raise

return

try

while

with

yield

4 python3內建函式

abs()

dict()

help()

min()

setattr()

all()

dir()

hex()

next()

slice()

any()

divmod()

id()

object()

sorted()

ascii()

enumerate()

input()

oct()

staticmethod()

bin()

eval()

int()

open()

str()

bool()

exec()

isinstance()

ord()

sum()

bytearray()

filter()

issubclass()

pow()

super()

bytes()

float()

iter()

print()

tuple()

callable()

format()

len()

property()

type()

chr()

frozenset()

list()

range()

vars()

classmethod()

getattr()

locals()

repr()

zip()

compile()

globals()

map()

reversed()

__import__()

complex()

hasattr()

max()

round()

delattr()

hash()

memoryview()

set()

常用內建函式描述
abs(num) 返回num的絕對值

all(iterable) 如果iterable的所有元素都為真或者 iterable為空,則返回Ture,否則返回False

any(iterable) 如果iterable的任一元素都為真,則返回Ture,如果 iterable為空,則返回False

divmod(num1,num2)  返回num1除以num2的商和餘數

eval('num1+num2')   'num1+num2'字串當做表示式計算,並返回計算的結果

float(num1)         將字串或數值轉化為浮點數
int(num1)           將字串或數值轉換為int

isinstance(obj, cls)  檢查obj是否是cls類的物件,是則返回Ture,否則返回False

len()                 返回長度
max(num1,num2)          返回最大值
min(num1,num2)          返回最小值
pow(x,y)                返回x的y次方結果

dict(key1=value1,key2=value2)   返回字典
list(iterable)                  返回列表
tuple(iterable)                 返回元組

map(function,iterable)   對iterable中的每個元素使用fuction函式,並將結果作為新的iterable返回
sum(iterable,num)        返回iterable所有元素加上num的和,num預設0