import random listC = ['加多寶', '雪碧', '可樂', '勇闖天涯', '椰子汁'] print(random.choices(listC), type(random.choices(listC))) # choices函數返回列表類型數據 print(random.choice(listC), type(random.choice(listC))) # choice函數返回字符串類
listA = ['水煮乾絲', '豆腐', '基圍蝦', '青菜', '西紅柿炒雞蛋'] listA.append('紅燒肉') print(listA) listA.remove('水煮乾絲') print(listA)
dictMenu = {'卡布奇諾': 32, '摩卡': 30, '抹茶蛋糕': 28, '布朗尼': 26} Sum = 0 for i in dictMenu.values(): Sum += i print(Sum)
s = input() print(eval(s[::-1])) # eval函數會根據輸入的內容字符串s中內容轉換爲相應的類型
print('{:+>25}'.format(123456))
print('{:>30,}'.format(12345678.9))
print('0x{0:x},0o{0:o},{0},0b{0:b}'.format(0x1010))
s = input() print(s.lower())
s = input() print(s.count('a'))
s = input() print(s.replace('py', 'Python'))
data = input() a = data.split(',') # a是列表類型 b = [] for i in a: b.append(i) print(max(b))
s = '9e10' if type(eval(s) == type(0.0)): print('True') else: print('False')
s = '9e10' print('True' if type(eval(s)) == type(0.0) else 'False')
s = '123' print('True' if type(eval(s)) == type(1) else 'False')
ls = [123, '456', 789, '123', 456, '798'] Sum = 0 for item in ls: if type(item) == type(123): Sum += item print(Sum)
while True: s = input() if s in ['y', 'Y']: break
while True: s = input() if s== 'y' or s== 'Y': exit()
try: a = eval(input()) print(100 / a, type(100 / a)) # float except: pass
def psum(a, b): return a ** 2 + b ** 2 if __name__ == '__main__': t1 = psum(2, 2) print(t1)
def psum(a, b=10): return (a ** 2 + b ** 2), a + b if __name__ == '__main__': t1, t2 = psum(2) print(t1, t2)
def psum(a, b): return (a ** 2 + b ** 2), a + b if __name__ == '__main__': t1, t2 = psum(2, 2) print(t1, t2)
n = 2 def psum(a, b): global n return (a ** 2 + b ** 2) * n if __name__ == '__main__': print(psum(2, 3))
pyinstaller -F py.pypython
pyinstaller -I py.ico -F py.pyapp
import jieba txt = '中華人民共和國教育部考試中心' ls = jieba.lcut(txt, cut_all=True) print(ls)
['中華', '中華人民', '中華人民共和國', '中華人民共和國教育部', '華人', '人民', '人民共和國', '共和', '共和國', '國教', '教育', '教育部', '教育部考試中心', '考試', '中心']dom
try: f = open('a.txt', 'x') except: print('文件存在,請當心讀取!')
ls = [123, '456', 789, '123', 456, '789'] ls.insert(3, '012') print(ls)
[123, '456', 789, '012', '123', 456, '789']函數
ls = [123, '456', 789, '123', 456, '789'] ls.remove(789) print(ls)
ls = [123, '456', 789, '123', 456, '789'] print(ls[::-1])
['789', 456, '123', 789, '456', 123]code
ls = [123, '456', 789, '123', 456, '789'] print(ls.index(789))
d = {123: '123', 456: '456', 789: '789'} print(list(d.values()))
d = {123: '123', 456: '456', 789: '789'} print(list(d.keys()))