Day 1 of Learning Python
书本为《Python编程:从入门到实践》
注意:
函数:通过“函数名()”的方式进行调用。
方法:通过“对象.方法名”的方式进行调用
message="Hello Eric, would you like to learn some Python today?"
print(message.upper()) #全部大写
print(message.lower()) #全部小写
print(message.title()) #首字母大写
message2='Albert Einstein once said, "A person who never made a mistake never tried anything new"'
print(message2)
运行结果如下
HELLO ERIC, WOULD YOU LIKE TO LEARN SOME PYTHON TODAY?
hello eric, would you like to learn some python today?
Hello Eric, Would You Like To Learn Some Python Today?
Albert Einstein once said, “A person who never made a mistake never tried anything new”
\n换行 \t添加一个制表位
Python 用两个称号表示乘方运算
>>>3*3
9
>>>10**6
1000000
列表
python中,用方括号 [] 来表示列表,并用逗号来分割其中元素。第一个列表元素的索引为0,而不是1. 索引指定为-1,可以让python返回最后一个列表元素,-2返回倒数第二个元素,以此类推。
方法append()将元素添加到列表末尾,而不影响列表中其它元素。
names.append('laowang')
方法insert()可在列表任何位置添加新元素,需要指定新元素的索引和值
names.insert(0,'laowang')
使用del 可删除任何位置处列表元素,条件是知道其索引
Del names[1]
方法pop()可删除列表末尾的元素,并让你能够接着使用它。术语弹出(pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycle=motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
运行结果为
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’]
suzuki
实际上,你可以使用pop()删除列表中任意一个元素,只需在括号中指定要删除的元素的索引即可。motorcycles.pop(0)
如果你要从列表中删除一个元素,且不再以任何方式使用它,就是用del语句;如果你要在删除元素后还能继续使用它,就是用方法pop()。
根据值删除元素remove(),比如motorcycles.remove(‘suzuki’)
练习题3-4~3-7
mylist=['A','B','C','D','E']
print(mylist)
cannot=mylist.pop(1)
print(cannot+' cannot come')
mylist.insert(1,'F')
print(mylist)
mylist.insert(0,'G')
mylist.insert(3,'H')
mylist.append('I')
print(mylist)
print("I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print(mylist[0]+", you are still invited")
print(mylist[1]+', your are still invited')
del mylist[1]
del mylist[0]
print(mylist)
结果如下
[‘A’, ‘B’, ‘C’, ‘D’, ‘E’]
B cannot come
[‘A’, ‘F’, ‘C’, ‘D’, ‘E’]
[‘G’, ‘A’, ‘F’, ‘H’, ‘C’, ‘D’, ‘E’, ‘I’]
I can only invite two people
sorry, I, I can only invite two people
sorry, E, I can only invite two people
sorry, D, I can only invite two people
sorry, C, I can only invite two people
sorry, H, I can only invite two people
sorry, F, I can only invite two people
G, you are still invited
A, your are still invited
[]
组织列表
方法sort()永久性按字母顺序排列。按字母顺序相反顺序排列只需向sort()传递参数reverse=True。
cars=['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
cars=['bmw','audi','toyota','subaru']
cars.sort(reverse=True)
print(cars)
运行结果
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
[‘toyota’, ‘subaru’, ‘bmw’, ‘audi’]
函数sorted()能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。
cars=['bmw','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("Here is the sorted list:")
print(sorted(cars))
print("Here is the original list again:")
print(cars)
运行结果
Here is the original list:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
Here is the sorted list:
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
Here is the original list again:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
也可向sorted()传递参数reverse=True。
注意:在并非所有值都是小写时,按字母排列列表要复杂些。
要反转列表元素的排列顺序,可使用方法reverse()。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
结果
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
[‘subaru’, ‘toyota’, ‘audi’, ‘bmw’]
方法reverse()不是指按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序。其可永久性修改列表排列顺序,但只需对列表再次调用reverse()即可恢复原排序。
函数len()可获得列表长度
习题3-8~3-10
places=['tokyo','sydney','london','paris','hongkong']
print('original list')
print(places)
print('\nlisted by character')
print(sorted(places))#使用sorted()按字母顺序打印,但不修改
print('\noriginal list')
print(places) #核实原来顺序
places.reverse()#修改列表排序
print('\nreverse the original list')
print(places)
places.reverse()#恢复原来顺序
print('\noriginal list')
print(places)
places.sort()#按字母顺序排列
print('\nlisted by character')
print(places)
places.sort(reverse=True)#按字母顺序反向排列
print('\nreverse the list')
print(places)
print('\nThere are '+str(len(places)) +' places in total')
结果
original list
[‘tokyo’, ‘sydney’, ‘london’, ‘paris’, ‘hongkong’]
listed by character
[‘hongkong’, ‘london’, ‘paris’, ‘sydney’, ‘tokyo’]
original list
[‘tokyo’, ‘sydney’, ‘london’, ‘paris’, ‘hongkong’]
reverse the original list
[‘hongkong’, ‘paris’, ‘london’, ‘sydney’, ‘tokyo’]
original list
[‘tokyo’, ‘sydney’, ‘london’, ‘paris’, ‘hongkong’]
listed by character
[‘hongkong’, ‘london’, ‘paris’, ‘sydney’, ‘tokyo’]
reverse the list
[‘tokyo’, ‘sydney’, ‘paris’, ‘london’, ‘hongkong’]
There are 5 places in total