补一篇博,书中前几章的练习题,有一些作业写在一起没加注释,不足之处望多加指正。
第1章 起步
message="Hello Python world!"
print(message)
message="HELLO PYTHON CRASH COURSE WORLD!"
print(message)
第2章 变量和简单数据类型
P23作业
name="Eric"
print("Hello "+name+","+"would you like to learn some Python today?")
name="kitty daddy"
print(name.lower())
print(name.title())
print(name.upper())
name='Albert Einstein'
sentense='"A person who never made a mistake never tried anything new."'
print(name+" once said,"+sentense)
name=" nike "
print(name.rstrip())
print(name.lstrip())
name = "ada lovelace"
print(name.upper())
print(name.lower())
first_name = "ada"
last_name = "lovelace"
full_name = first_name+" "+last_name
print(full_name.title())
message = "Hello,"+full_name.title()+"!"
print(message)
print(5+3)
print(10-2)
print(2*4)
print(8/1)
#homework 1.15
number=32
print("my favorite number is "+str(number))
第3章 列表简介
P33作业
names=['ruhua','jiaozi','nosebro']
print(names[0])
print(names[1])
print(names[-1].title())
message='Nice to meet you '
print(message+names[0])
traffic=['subway','motobike','bus','drive','walk']
message_1 = 'I would like to '
message_2 = ' to work'
print(message_1+traffic[3]+message_2)
P38作业
invite=['jiaozi','ruhua','nosebro']
print(invite[0],invite[1],invite[2])
print(invite[0]+' is not available.')
invite.remove('jiaozi')
invite.insert(0,'erbao')
print(invite[0])
print(invite[1])
print(invite[2])
print('\nI found a bigger table.')
invite.insert(0,'bingxue')
invite.insert(2,'feifei')
invite.append('songsong')
print(invite[0],invite[1],invite[2],invite[3],invite[4],invite[5])
print('\nonly have two seats')
popped_invite=invite.pop()
print(popped_invite+' ,we are sorry to inform you that we can not have a dinner with you.')
popped_invite=invite.pop()
print(popped_invite+' ,we are sorry to inform you that we can not have a dinner with you.')
popped_invite=invite.pop()
print(popped_invite+' ,we are sorry to inform you that we can not have a dinner with you.')
popped_invite=invite.pop()
print(popped_invite+' ,we are sorry to inform you that we can not have a dinner with you.')
print(invite[0]+' ,yeah')
print(invite[1]+' ,yeah')
del invite[0]
del invite[0]
print(invite)
P41作业
sites=['changchun','sichuan','changsha','xiamen','xian']
print(sites)
print(sorted(sites))
print(sites)
print(sorted(sites,reverse=True))
print(sites)
sites.reverse()
print(sites)
sites.reverse()
print(sites)
sites.sort()
print(sites)
sites.sort(reverse=True)
print(sites)
第4章 操作列表
随便写写
magicians=['alice','david','carolina']
for magician in magicians:
print(magician.title()+",that was a great trick!")
print('I can not wait to see your next trick, '+magician.title()+'.\n')
print("Thank you,everyone. That was a great magic show!")
P50
pizzas=['chicken pizza','pepper pizza','fruit pizza']
for pizza in pizzas:
print(pizza)
print('I like '+pizza+'!\n')
print('I really love pizza!\n')
pets=['dog','cat','turtle']
for pet in pets:
print('A '+pet+' would make a great pet.')
print('Any of these animals would make a great pet!')
P54
for value in range(1,21):
print(value)
big=list(range(1,1000001))
print(max(big))
print(min(big))
print(sum(big))
jishus=list(range(1,20,2))
for jishu in jishus:
print(jishu)
print('\n')
hw47=list(range(3,33,3))
print(hw47)
lifang=[]
for value in range(1,11):
lifang.append(value**3)
print(lifang)
print('\n')
lifang=[value**3 for value in range(1,11)]
print(lifang)
P58
thing=['watch','pen','glass','ipad','iphone']
print('The first three items in the list are:')
print(thing[0:3])
print('Three items from the middle of the list are:')
print(thing[1:4])
print('The last three items in the list are:')
print(thing[-3:])
friend_things=thing[:]
thing.append('cup')
friend_things.append('face')
print('\nMy favorite pizzas are:')
for things in thing:
print(things)
print("\nMy friend's favorite pizzas are:")
for friend_thing in friend_things:
print(friend_thing)
print('\n')
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
print("My favorite foods are:")
for food in my_foods:
print(food)
P60
foods=('pizza','chips','water','rice','vegetable')
for food in foods:
print(food)
print('\n')
foods=('tihuan1','tihuan2','water','rice','vegetable')
for food in foods:
print(food)
第5章 if语句
随便写写
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':
print(car.upper())
else:
print(car.title())
P70
car='suba'
print("Is car=='subaru'?I predict True.")
print(car=='subaru')
print('\n')
print("Is car =='audi'?I predict False.")
print(car=='suba')
# okk,i get it!
print('\n')
string_1=['hello','byebye','what','how','why']
string_2='Byebye'
string_3='byebye'
print(string_3==string_2)
print(string_2.lower()=='byebye')
P75
alien_colors=['green','yellow','red']
for alien_color in alien_colors:
if alien_color=='green':
point=5
print('you get '+str(point)+' points!')
P79
#5-8
users=['a','b','c','d','e','admin','f']
for user in users:
if user == 'admin':
print('Hello admin,would you like to see a status report?')
else:
print('Hello '+user+',thank you for logging in again.')
#5-9
print('\n')
users1 = []
if users1:
for userss1 in users1:
popped_userss1 = users1.pop()
print('We need to find some users!')
else:
print('We need to find some users!')
print('\n')
#5-10
current_users=['A','b','c','d','e','admin','f']
new_users=['a','p','e','h','i','j']
m = [string.lower() for string in current_users]
n = [string.lower() for string in new_users]
for new in n:
if new in m:
print('You have to print another name.')
else:
print('This name have not been used.')
#5-11
print('\n')
numbers = ['1','2','3','4','5','6','7','8','9']
for number in numbers:
if number == '1':
print('1st')
elif number == '2':
print('2nd')
elif number == '3':
print('3rd')
else:
print(number+'th')