写的太粗糙了,记得后期改
2-1 简单信息x=5
x
52-2 多条简单信息
y=4 y
4
y=3 y
3
user_name = "Eric" user_name
'Eric'2-3 个性化信息
print("Hello "+user_name+", would you like to learn some Python today?")
Hello Eric, would you like to learn some Python today?2-4 调整名字大小写
print(user_name.lower())
eric
print(user_name.upper())
ERIC
print(user_name.title())
Eric2-5 名言
famous_person = "Albert Einstein" message = "A person who never made a mistake never tried anything new." print(famous_person+' once said, “'+message+'”')
Albert Einstein once said, “A person who never made a mistake never tried anything new.”2-6 名言2
2-7 剔除人名中的空白略
先用’t’,再用’n’, 视觉上更明显
name = "tLi Mingn" print(name)
Li Ming
print(name.lstrip())
Li Ming
print(name.rstrip())
Li Ming
print(name.strip())
Li Ming2-8 数字8
print(5+3)
8
print(16/2) print(4*2)
8.0 8
print(9-1)
82-9 最喜欢的数字
number = 8
print("This is my favorite number : "+str(number))
This is my favorite number : 83-1 姓名
names = ['zhang san','li si','wang wei']
for name in names:
print(name.title())
Zhang San Li Si Wang Wei3-2 问候语
for name in names:
print(name.title()+", have a good day!")
Zhang San, have a good day! Li Si, have a good day! Wang Wei, have a good day!3-3 自己的列表
ways = ['taking bus','riding a bicycle']
for way in ways:
print(way+" is one of my favorite ways to go to work!")
taking bus is one of my favorite ways to go to work! riding a bicycle is one of my favorite ways to go to work!3-4 嘉宾名单
guests = ['zhang san','li si','wang wei']
for guest in guests:
print(guest.title()+ ", would you like to have dinner with me?")
Zhang San, would you like to have dinner with me? Li Si, would you like to have dinner with me? Wang Wei, would you like to have dinner with me?3-5 修改嘉宾名单
print("Sorry, LiSi doesn't .")
Sorry, LiSi doesn't .
guests[1] = 'li ming'
for guest in guests:
print(guest.title()+ ", would you like to have dinner with me?")
Zhang San, would you like to have dinner with me? Li Ming, would you like to have dinner with me? Wang Wei, would you like to have dinner with me?3-6添加嘉宾
print("A bigger table was found.")
A bigger table was found.
guests.insert(0,'zhao yi')
guests.insert(2,'dong er')
guests.append('tian wu')
for guest in guests:
print(guest.title()+ ", would you like to have dinner with me?")
Zhao Yi, would you like to have dinner with me? Zhang San, would you like to have dinner with me? Dong Er, would you like to have dinner with me? Li Ming, would you like to have dinner with me? Wang Wei, would you like to have dinner with me? Tian Wu, would you like to have dinner with me?3-7 缩减名单
print("Sorry, I can only invite two people.")
Sorry, I can only invite two people.
对列表求长度是用函数len(),这里确定范围用的是range()函数,指定个数和列表
for index in range(2,len(guests)):
guest = guests.pop()
print(guest.title()+ ", sorry,I can't have dinner with you.")
for guest in guests:
print(guest.title()+", I feel so glad to have dinner with you!")
del guests[-1]
del guests[0]
print(guests)
Tian Wu, sorry,I can't have dinner with you. Wang Wei, sorry,I can't have dinner with you. Li Ming, sorry,I can't have dinner with you. Dong Er, sorry,I can't have dinner with you. Zhao Yi, I feel so glad to have dinner with you! Zhang San, I feel so glad to have dinner with you! []3-8 放眼世界
places = ["xizang","chengdu","dali","qingdao","mohe"] print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
sorted()函数用于临时排序,所以只能在print时调用,但是sort()方法用于永久排序 反序时传参数reverse = True
print(sorted(places))
['chengdu', 'dali', 'mohe', 'qingdao', 'xizang']
print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
print(sorted(places,reverse = True))
['xizang', 'qingdao', 'mohe', 'dali', 'chengdu']
print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
places.reverse() print(places)
['mohe', 'qingdao', 'dali', 'chengdu', 'xizang']
places.reverse() print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
places.sort() print(places)
['chengdu', 'dali', 'mohe', 'qingdao', 'xizang']
places.sort(reverse = True) print(places)
['xizang', 'qingdao', 'mohe', 'dali', 'chengdu']3-9 晚餐嘉宾
names = ['zhang san','li si','wang wei'] len(names)
33-10 尝试各种函数
cities = ['qingdao','beijing','xiamen','chengdu','taian','dali'] print(cities[0])
qingdao
# print(cities[8]) 主动引发索引错误 print(cities[-1]) #通过索引来访问元素,注意负数的使用,有时会带来方便
dali
寻求更加干净整洁的输出
print(cities[0].title()) #首字母大写
Qingdao
注意列表元素的索引是从0开始的
message = cities[3].title() + "'foods are famous in the world!" print(message)
Chengdu'foods are famous in the world!
元素的增删改
cities[2] = 'tianjin' print(cities)
['qingdao', 'beijing', 'tianjin', 'chengdu', 'taian', 'dali']
cities.append('mohe')
print(cities)
['qingdao', 'beijing', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
cities.insert(2,'jinan') print(cities)
['qingdao', 'beijing', 'jinan', 'jinan', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
del cities[2] #永久删除,且删除的值不可再用 print(cities)
['qingdao', 'beijing', 'jinan', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
capital = cities.pop(1) #永久删除,但被删除的元素可以使用 print(capital + " is the capital of China")
beijing is the capital of China
cities.remove('jinan') # 按值移除,只删第一个出现的,多个重复值时要考虑用循环来删
print(cities)
['qingdao', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
排序
print(sorted(cities))
['chengdu', 'dali', 'mohe', 'qingdao', 'taian', 'tianjin']
cities.reverse() print(cities)
['mohe', 'dali', 'taian', 'chengdu', 'tianjin', 'qingdao']
cities.sort() print(cities)
['chengdu', 'dali', 'mohe', 'qingdao', 'taian', 'tianjin']
cities.sort(reverse = True) print(cities)
['tianjin', 'taian', 'qingdao', 'mohe', 'dali', 'chengdu']
len(cities)
6



