目錄
# 輸入
name = input("請輸入您的姓名:") # 返回字串
# 輸出
print("hello: ", name)
# 格式化輸出
print('%2d-%02d' % (1, 1)) # ' 1-01'
print('a', 1, sep=',', end='!') # a,1!
# format格式化輸出
print(",".format('a','b')) # a,b
print(",".format(a='a',b='b')) # a,b
print(",".format(['a','b'])) # a,b
print("".format(3.1415926)) # 3.14
# 物件的format格式化輸出
class a(object):
def __init__(self, value):
self.value = value
a = a(6)
print(''.format(a)) # 6
for i in range(5):
print(i) # 0 1 2 3 4
for i in range(1,5):
print(i) # 1 2 3 4
for i in range(1,5,2):
print(i) # 1 3
l = [-2, -1, 0, 1, 2]
print(max(l)) # 2
print(min(l)) # -2
print(len(l)) # 5
print(sum(l)) # 0
print(abs(-1)) # 1
print(pow(2, 5)) # 32
print(divmod(5, 2)) # (2, 1) (商,餘數)
print(round(1.25361, 3)) # 1.254
print(round(1627731, -1)) # 1627730
# 返回物件的雜湊值
print(hash('b')) # 8720545829485517778
a = complex(1, 2) # 或 a = 1 + 2j
print(a.real) # 1.0
print(a.imag) # 2.0
print(a.conjugate()) # (1-2j)
print(int('123')) # 123
print(float('123.4')) # 123.4
print(str(123)) # '123'
print(bin(2)) # 0b10 二進位制
print(oct(8)) # 0o10 八進位制
print(hex(16)) # 0x10 十六進製制
print(bytes(1)) # b'\\\\x00'
print(ord('a')) # 97
print(chr(97)) # a
print(list((1,2,3))) # [1, 2, 3]
print(set([1,2,3,3])) #
print(frozenset([1,2,3,3]))
# frozenset()
print(dict([('a', 1), ('b', 2)]))
# print(dict(zip(['a', 'b'], [1, 2])))
# # zip打包
l1 = ['a','b','c']
l2 = [1,2,3]
for i in zip(l1,l2):
print(i)
# ('a', 1) ('b', 2) ('c', 3)
# zip解壓
for i in zip(*zip(l1,l2)):
print(i)
# ('a', 'b', 'c') (1, 2, 3)
print(all([0,1,2])) # 所有元素為真返回true
print(any([0,1,2])) # 任一元素為真返回true
print(bool(2)) # true
print(type(2)) # print(id(2)) # 返回記憶體位址
print(callable(2)) # 能否呼叫
print(isinstance(2,int)) # true
print(isinstance(2,(str,int))) # true
print(issubclass(str, int)) # false
print(dir([str])) # 返回物件的屬性方法列表
print(help('sys')) # 返回物件的幫助文件
print(locals()) # 返回當前區域性變數的字典
print(globals()) # 返回當前全域性變數的字典
# 物件的屬性相關方法
class student(object):
def __init__(self, name):
self.name = name
s = student('tom')
print(getattr(s, 'name')) # tom
print(hasattr(s, 'age')) # false
print(getattr(s, 'age', 5)) # 屬性age不存在,但會返回預設值5
setattr(s, 'age', 5) # 設定age屬性
print(hasattr(s, 'age')) # true
delattr(s, 'age') # 刪除屬性
print(hasattr(s, 'age')) # false
r = compile("print('hello,world')", "", "exec")
exec(r)
# hello,world
print(eval("1+2*3"))
# 7
r = compile("3*4+5",'','eval')
print(eval(r))
# 17
# map對映
def func(x):
return x * x
for i in map(func, [1, 2, 3]):
print(i) # 1 4 9
for i in map(lambda x: x*x, [1, 2, 3]):
print(i) # 1 4 9
for i in map(lambda x,y: x+y, [1,3,5], [2,4,6]):
print(i) # 3 7 11
# reduce累積
from functools import reduce
def add(x, y):
return x + y
print(reduce(add, [1, 3, 5])) # 9
# filter過濾
for i in filter(lambda e: e%2, [1, 2, 3]):
print(i) # 1 3
# sorted排序
print(sorted([1, 5, 2], reverse=true))
# [5, 2, 1]
print(sorted([('b',2), ('a',1)], key=lambda x:x[0]))
# [('a', 1), ('b', 2)]
python學習之內置函式(二)
4.7.3 內建函式 2 int str bool set list 將乙個可迭代物件轉化為列表 tuple 將乙個可迭代物件轉換成元組 dic 通過相應的方式建立字典 print 輸出到螢幕原始碼 print value,sep end n file sys.stdout,flush false ...
python之內置函式
非空即真,非0即真 記住這句話可以讓你少寫好多 l asdfgwert3r 1 sorted l 排序 字串可以直接使用sorted排序 2 all 如果list裡面都為真的情況返回為 true all 1 2,3 4 print true all 1 2,3 0 print false all 1...
python之內置函式
它將兩個 非複數 數字作為實參,並在執行整數除法時返回一對商和餘數。對於混合運算元型別,適用雙目算術運算子的規則。對於整數,結果和 a b,a b 一致,分別對應取整數和取餘數 對於浮點數,結果是 q,a b q 通常是 math.floor a b 但可能會比 1 小。如 將153拆解 a,b d...