Python Chapter 2 homework, python Chapter 2 homework
2-1 simple information:
message="hello world"print(message)
Running result:
2-2 multiple simple messages:
message="hello world"print(message)message="no thanks"print(message)
Running result:
2-3 personalized information:
name="Ro bo"print("hello, "+name+", Good morning!")
Running result:
2-4 adjust the name case:
name="Ro bo"print("hello, "+name.lower()+", Good morning!")print("hello, "+name.upper()+", Good morning!")print("hello, "+name.title()+", Good morning!")
Running result:
2-5 famous saying:
print('Dresser said:"The ideal is the sun of life."')
Running result:
2-6 famous saying 2:
famous_person="Dresser"message='"The ideal is the sun of life."'print(famous_person+" said: "+message)
Running result:
2-7 remove white spaces in person names:
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())
Running result:
2-8 digits 8:
print(4+4)print(9-1)print(2*4)print(int(16/2))
Running result:
2-9 favorite numbers:
number=8print("My favourite number is "+str(number)+"!")
Running result:
2-10 Add comments:
# Each element in the list is the 3rd power of value. arr = [value ** 3 for value in range ()] print (arr)
# Some mathematical functions arr = [] for item in range (): arr. append (item) print (arr) print (min (arr) # minimum value print (max (arr) # maximum value print (sum (arr) # sum
2-11 zen about Python:
Running result: