list1 = ["A",'B',"C"]
list2 = ['X','Y',"Z"]
list3 = [x+y for x in list1 for y in list2]
print(list3)
# 方法一
f = lambda n: f(n-1) * n if n >= 2 else 1
print(f(5))
# 方法二
from functools import reduce
res = reduce(lambda x, y: x * y, range(1, 6))
print(res)
例1:輸入:[1, 3, 5, 6], 5 輸出:2 例2:輸入:[1, 3, 5, 6], 2 輸出:1
def searchInsert(nums, target):
for i in nums:
if i>=target:
return nums.index(i)
if nums[0]>=target:
return 0
else:
return len(nums)
list1 = [1, 3, 5, 6]
res = searchInsert(list1, 7)
print(res)
import calendar
year = input("輸入年份:")
month = input("輸入月份:")
year = int(year)
month = int(month)
cal = calendar.month(year, month)
print("當前輸出月份:" + cal)
# 辛光華
import socket
socket.socket()
# 袁野
import socket
server = socket.socket() # 建立一個socket對象
ip_port = ('0.0.0.0', 8001) # 給出IP地址和端口號
server.bind(ip_port) # 綁定IP和端口
server.listen(5) # 監聽
conn, address = server.accept() # 被動接收請求
while 1:
content = input('服務端:')
conn.send(content.encode('utf-8')) # 發送信息
from_client_msg = conn.recv(1024) # 接收來自客戶端的信息
from_client_msg = from_client_msg.decode('utf-8') # 解碼來自客戶端的信息
print('來自客戶端的信息:', from_client_msg)
if from_client_msg == 'bye': # 當客戶端的信息爲bye時結束聊天
break
conn.close() # 關閉通道
server.close() # 關閉服務端
from multiprocessing import Process
def foo(i):
print ('say hi', i)
if __name__ == '__main__':
for i in range(10):
p = Process(target=foo, args=(i,))
p.start()
class Context: TODO pass with Context() as ctx: ctx.do_something()
請在 Context 類下添加代碼完成該類的實現。class Sample:
def __enter__(self):
return self
def __exit__(self, type, value, trace):
print("type:", type)
print("value:", value)
print("trace:", trace)
print(sample)
def do_something(self):
bar = 1
return bar + 10
with Sample() as sample:
sample.do_something()