Python 编程
变量的命名和使用
在Python中使用变量时,需要遵守一些规则和指南。违反这些规则将引发错误,而指南旨在让你编写的代码更容易阅读和理解。请务必牢记下述有关变量的规则。
变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头,例如,可将变量命名为message_1,但不能将其命名为1_message。
变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greeting message会引发错误。
不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print (请参见附录A.4)。
变量名应既简短又具有描述性。例如,name比n好,student_name比s_n好,name_length比length_of_persons_name好。
慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。
要创建良好的变量名,需要经过一定的实践,在程序复杂而有趣时尤其如此。随着你编写的程序越来越多,并开始阅读别人编写的代码,将越来越善于创建有意义的变量名。
注意 注意 就目前而言,应使用小写的Python变量名。在变量名中使用大写字母虽然不会导致错误,但避免使用大写字母是个不错的主意。
字符串
1.修改字符串的大小写
举例:name = "ada lovelace"
print(name.title()) 首字母改成大写
name = "Ada Lovelace"
print(name.upper()) 全部改成大写
print(name.lower()) 全部改成小写
2.使用制表符或换行符来添加空白
举例:print("\tPython") 制表符
print("Languages:\nPython\nC\nJavaScript") 换行符
print("Languages:\n\tPython\n\tC\n\tJavaScript") 混合使用
3.删除空白
favorite_language = 'python '
favorite_language.rstrip() 删除字符串末尾没有空白
favorite_language = favorite_language.rstrip() 重新保存在变量中
favorite_language.lstrip() 'python ' 删除字符串开头的空白
favorite_language.strip() 'python' 删除字符串两端的空白
4.函数str()
举例:age = 23
message= "Happy"+str(age) +"re Birthday!"
print(message)
注释
# 注释
列表
在Python中,用方括号([] )来表示列表,并用逗号来分隔其中的元素
1.访问列表元素
举例: bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0] 访问第一个元素
2.索引从0开始
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1]) 访问第二个元素
print(bicycles[3]) 访问第四个元素
3.使用列表中的各个值
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)
修改 添加 删除元素
1.修改元素列表
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
2.在列表末尾添加元素
方法append() 将元素'ducati' 添加到了列表末尾,而不影响列表中的其他所有元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
2.在列表中插入元素
方法insert() 可在列表的任何位置添加新元素。为此,你需要指定新元素的索引和值
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
3.从列表中删除元素
del 可删除任何位置处的列表元素,条件是知道其索引
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
使用方法pop()删除元素
❶ motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles)
❷ popped_motorcycle = motorcycles.pop()
❸ print(motorcycles)
❹ print(popped_motorcycle)
据值删除元素 根据值删除元素
你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值,可使用方法remove()
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
举例:
motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("\nA "+ too_expensive.title()+"is too expensive for me.")
组织列表
1. sort() 永久性地修改了列表元素的排列顺序
你还可以按与字母顺序相反的顺序排列列表元素,为此,只需向sort() 方法传递参数reverse=True
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
2.函数sorted() 让你能够按特定顺序显示列表元素
cars = ['bmw', 'audi', 'toyota', 'subaru']
❶ print("Here is the original list:")
print(cars)
❷ print("\nHere is the sorted list:")
print(sorted(cars))
❸ print("\nHere is the original list again:")
print(cars)
3.,reverse() 不是指按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序
方法reverse() 永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此只需对列表再次调用reverse() 即可
cars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars)
cars.reverse()
print(cars)
4.函数len() 可快速获悉列表的长度
浙公网安备 33010602011771号