Python第二章課後作業,python第二章課後
2-1簡單資訊:
message="hello world"print(message)
運行結果:
2-2多條簡單資訊:
message="hello world"print(message)message="no thanks"print(message)
運行結果:
2-3個人化資訊:
name="Ro bo"print("hello, "+name+", Good morning!")
運行結果:
2-4調整名字大小寫:
name="Ro bo"print("hello, "+name.lower()+", Good morning!")print("hello, "+name.upper()+", Good morning!")print("hello, "+name.title()+", Good morning!")
運行結果:
2-5名言:
print('Dresser said:"The ideal is the sun of life."')
運行結果:
2-6名言2:
famous_person="Dresser"message='"The ideal is the sun of life."'print(famous_person+" said: "+message)
運行結果:
2-7剔除人名中的空白:
famous_person="\nDresser\t"print("origin:")print(famous_person)print("lstrip")print(famous_person.lstrip())print("rstrip")print(famous_person.rstrip())print("strip")print(famous_person.strip())
運行結果:
2-8數字8:
print(4+4)print(9-1)print(2*4)print(int(16/2))
運行結果:
2-9最喜歡的數字:
number=8print("My favourite number is "+str(number)+"!")
運行結果:
2-10添加註釋:
#列表中的每個元素都是value的三次方arr=[value**3 for value in range(1,5)]print(arr)
#某些數學函數arr=[]for item in range(1,11):arr.append(item)print(arr)print(min(arr))#最小值print(max(arr))#最大值print(sum(arr))#求和
2-11Python之禪:
運行結果: