Python常用函式

2022-08-22 02:45:13 字數 2890 閱讀 9158

name = input('請輸入你的姓名:')
s1 = 'taibai'

print(s1.upper()) >>>>>taibai

a = range(9)

print(a)

>>>>>range(0, 9)

for i in range(5):

print(i)

>>>>0

1 23 4

for i in range(1,7,2):

print(i)

>>>>1 3 5

for i in range(2,-2,-1):

print(i)

>>>>2 1 0 -1

a = range(0,5)   

print(a)

pyrhon2中返回的結果是: [0,1,2,3,4]

python3中返回的結果是: (0,5)

l1 = ("a", "b", "c")

for i in enumerate(l1):

print(i)

>>>>>(0, 'a')

(1, 'b')

(2, 'c')

print ("{} {}".format("hello", "world"))

>>>hello world ###不設定指定位置,預設排序

print (" ".format("hello", "world"))

>>>hello world hello ###按照索引指定位置

s = "alexwusir"

print(s1)

name = "   alex lenb   "

print(name.strip()) >>>>alex lenb

name1 = "00000alex0000"

print(name1.strip('0')) >>>>>alex

a = "alex:alex123"

print(a.split(":")) >>>>['alex', 'alex123']

print(a.split("l",1)) >>>>['a','lexalex123']

print(a.split(":")[0]) >>>>alex

print(a.split(":")[1]) >>>>alex123

b = "dlrblist"

b1 = b.rsplit("l", 1)

print(b1) #['dlrb', 'ist']

b = "dlrblist"

b2 = b.rsplit("l", 2)

print(b2) #['d', 'rb', 'ist']

c = "hello\nworld\ndlrb"

c1 = c.splitlines()

print(c1) #['hello', 'world', 'dlrb']

print(int("0b1111011",2))     #將2進製轉換成十進位制
print(oct(30))  >>>0o36
print(hex(87))   >>>>0x57
li = ["alex", "wusir", "taibai"]

a = "_".join(li)

print(a) >>>>alex_wusir_taibai

lst = ["asd","1231","asdasd"]

print("".join(lst))

>>>asd1231asdasd

a = 10

b = 10

c = 20

print(id(a), id(b), id(c))

>>>>140726332256800 140726332256800 140726332257120

a = 10000

b = 10

print(a.bit_length(),b.bit_length())

>>>>14 4

s = "你好"

s1 = s.encode("utf-8") # 編碼

print(s1)

>>>>b'\xe4\xbd\xa0\xe5\xa5\xbd'

s2 = s1.decode("utf-8") # 解碼

print(s2)

>>>>你好

將乙個可遍歷的資料物件(如列表、元組或字串)組合為乙個索引序列,同時列出資料和資料下標

lst = [11,22,33,44]

for i in enumerate(lst):

print(i)

>>>(0, 11)

(1, 22)

(2, 33)

(3, 44)

print(bin(10))    # 十進位制轉成二進位制

print(oct(10)) # 十進位制轉成八進位制

print(hex(30)) # 十進位制轉成十六進製制

print(int("0x1e",16))

print(int("0o12",8))

print(int("0b1010",2))

python常用函式 python常用函式精講

返回值為bool型別的函式 bool是boolean的縮寫,只有真 true 和假 false 兩種取值 bool函式只有乙個引數,並根據這個引數的值返回真或者假。引數如果預設,則返回false 引數轉換使用標準的邏輯測試表示式 傳入布林型別時,按原值返回 傳入字串時,空字串返回false,否則返回...

python常用函式中文 python常用函式

一 數學相關 1 絕對值 abs 1 2 最大最小值 max 1,2,3 min 1,2,3 3 序列長度 len abc len 1,2,3 len 1,2,3 4 取模 divmod 5,2 2,1 5 乘方 pow 2,3,4 2 3 4 6 浮點數 round 1 二 功能相關 1 函式是否...

Python常用函式

常用函式 abs x abs 返回乙個數字的絕對值。如果給出複數,返回值就是該複數的模。print abs 100 100 print abs 1 2j 2.2360679775 callable object callable 函式用於測試物件是否可呼叫,如果可以則返回1 真 否則返回0 假 可呼...