1. 程式人生 > >day5-Python學習筆記(八)內置函數

day5-Python學習筆記(八)內置函數

lte lis map 真的 數據類型 小白 十進制 list all

#函數即變量
# len type print input str

# print(all([1, 2, 3, 4])) # 判斷可叠代的對象裏面的值是否都為真
# print(any([0, 1, 2, 3, 4])) # 判斷可叠代的對象裏面的值是否有一個為真
# print(bin(100)) # 十進制轉二進制
# ejz = bin(100)
# print(ejz.replace(‘0b‘,‘‘))
# print(chr(65)) # 打印數字對應的ascii
# print(ord(‘A‘)) # 打印字符串對應的ascii碼
# print(dir(1)) # 打印傳入對象的可調用方法
# print(eval(‘[]‘)) # 執行python代碼,只能執行簡單的,定義數據類型和運算
# code = ‘def func(a):pass‘
# print(eval(code))
# code = ‘‘‘def a():print(‘aa‘)‘‘‘
# print(exec(code)) # 執行python代碼
#zip
# print(filter(lambda x: x > 5, [12, 3, 12, 2, 1, 2, 35])) # 把後面的叠代對象根據前面的方法篩選
# print(map(lambda x: x > 5, [1, 2, 3, 4, 5, 6]))

ids= [1,2,3,4,7,8,0,-1]
names=[‘小黑‘,‘小白‘,‘小黃‘,‘小綠‘]
names1=[‘小黑‘,‘小白‘,‘小黃‘,‘小綠‘]

# for id,name,s in zip(ids,names,names1):
# print(id,name,s)
print(sorted(‘0123450‘))#升序
print(sorted(ids,reverse=True))#降序
round(1.987123,5)# 保留幾位小數

# def func(a):
# if a%2==0:
# return a
# else:
# return ‘奇數‘
# nums = [x for x in range(11)]
# print(nums)
# res = map(func,nums)
# print(list(res))
# #循環調用函數,然後把每次函數處理的結果,放到一個list裏面返回


def func(a):
if a%2==0:
return True
else:
return False
nums = [x for x in range(11)]
# print(nums)
res = filter(func,nums)
print(list(res))
#filter 也是幫你循環調用函數的,filter只保存結果返回為真的


函數即變量
# def say(name):
# print(name)
#
# ybq = say
# ybq(‘原寶青‘)

def add():
print(‘添加商品‘)
def view():
print(‘查看商品‘)
def delete():
print(‘刪除商品‘)

choice = input(‘請輸入選擇 1、2、3、‘).strip()

menu = {
‘1‘:add,
‘2‘:view,
‘3‘:delete
}
if choice in menu:
menu[choice]() #適合用於函數沒有參數,或者參數是一樣的情況下。
else:
print(‘輸入錯誤‘)







day5-Python學習筆記(八)內置函數