世界的开始
Hello world!
1 print ("Hello Python world!" )
Hello Python world!
添加了一个名为message
的变量
1 2 message = "Hello Python world!" print (message)
Hello Python world!
变量
在程序中可随时修改变量的值,而Python将始终记录变量的最新值
变量的命名
变量名只能包含字母、数字和下划线
变量名不能包含空格,但可使用下划线来分隔其中的单词
不要将Python关键字和函数名用作变量名
慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0
字符串 以下都是方法 .xx()
title()
首字母大写upper()
全部大写lower()
全部小写 使用加号+
来合并字符串 使用字符组合\t
添加制表符 使用字符组合\n
添加换行符rstrip
删除字符串末尾的空白(暂时)lstrip
删除字符串开头的空白(暂时)strip
删除字符串两端的空白(暂时)
1 2 3 4 name = "ada lovelace" print (name.title())print (name.upper())print (name.lower())
Ada Lovelace
ADA LOVELACE
ada lovelace
1 2 3 4 first_name = "ada" last_name = "lovelace" full_name = first_name + " " + last_name print (full_name)
ada lovelace
1 2 3 print ("Python" )print ("\tPython" )
Python
Python
1 2 print ("Languages:\nPython\nC\nJavaScript" )
Languages:
Python
C
JavaScript
1 print ("Languages:\n\tPython\n\tC\n\tJavaScript" )
Languages:
Python
C
JavaScript
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
列表
索引从 0 而不是 1 开始
修改列表元素
添加列表元素
删除列表元素
组织列表元素(排序)
1 bicycles = ['Trek' , 'cannondale' , 'redline' , 'specialized' ]
['Trek', 'cannondale', 'redline', 'specialized']
Trek
1 print (bicycles[0 ].upper())
TREK
1 2 3 bicycles[0 ]=bicycles[0 ].title() print (bicycles[0 ])
Trek
添加列表元素(append
insert
)
1 2 3 motorcycles = ['honda' , 'yamaha' , 'suzuki' ] motorcycles.append('ducati' ) print (motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
1 2 3 motorcycles = ['honda' , 'yamaha' , 'suzuki' ] motorcycles.insert(2 , 'ducati' ) print (motorcycles)
['honda', 'yamaha', 'ducati', 'suzuki']
删除列表元素(del
pop
remove
)
1 2 3 motorcycles = ['honda' , 'yamaha' , 'suzuki' ] del motorcycles[0 ] print (motorcycles)
['yamaha', 'suzuki']
1 2 3 4 5 motorcycles = ['honda' , 'yamaha' , 'suzuki' ] print (motorcycles) popped_motorcycle = motorcycles.pop() print (popped_motorcycle) print (motorcycles)
['honda', 'yamaha', 'suzuki']
suzuki
['honda', 'yamaha']
1 2 3 4 5 motorcycles = ['honda' , 'yamaha' , 'suzuki' ] print (motorcycles) popped_motorcycle = motorcycles.pop(0 ) print (popped_motorcycle) print (motorcycles)
['honda', 'yamaha', 'suzuki']
honda
['yamaha', 'suzuki']
1 2 3 4 motorcycles = ['honda' , 'yamaha' , 'suzuki' , 'ducati' ] print (motorcycles) motorcycles.remove('suzuki' ) print (motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'ducati']
方法remove()
只删除第一个指定的值
排序
sort
永久性排序sorted
临时排序reverse
倒序len
确定列表的长度
1 2 3 4 5 6 7 cars = ['bmw' , 'aito' , 'toyota' , 'maybach' ] cars.sort() print (cars) cars.sort(reverse=True ) print (cars)cars.sort(reverse=False ) print (cars)
['aito', 'bmw', 'maybach', 'toyota']
['toyota', 'maybach', 'bmw', 'aito']
['aito', 'bmw', 'maybach', 'toyota']
1 2 3 4 5 6 7 8 9 cars = ['bmw' , 'aito' , 'toyota' , 'maybach' ] print ("Here is the original list:" ) print (cars) print ("\nHere is the sorted list:" ) print (sorted (cars,reverse=True )) print ("\nHere is the sorted list:" ) print (sorted (cars)) print ("\nHere is the original list again:" ) print (cars)
Here is the original list:
['bmw', 'aito', 'toyota', 'maybach']
Here is the sorted list:
['toyota', 'maybach', 'bmw', 'aito']
Here is the sorted list:
['aito', 'bmw', 'maybach', 'toyota']
Here is the original list again:
['bmw', 'aito', 'toyota', 'maybach']
1 2 3 4 cars = ['bmw' , 'aito' , 'toyota' , 'maybach' ] print (cars) cars.reverse() print (cars)
['bmw', 'aito', 'toyota', 'maybach']
['maybach', 'toyota', 'aito', 'bmw']
1 2 3 cars = ['bmw' , 'aito' , 'toyota' , 'maybach' ] len (cars)
4
1 2 cars = ['bmw' , 'aito' , 'toyota' , 'maybach' ] print (cars[4 ])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[10], line 2
1 cars = ['bmw', 'aito', 'toyota', 'maybach']
----> 2 print(cars[4])
IndexError: list index out of range
list index out of range 列表索引超出范围(或者是列表为空)
操作列表
range()
生成数字list()
转换数字成列表min()
最小值max()
最大值sum()
求和
遍历
对于用于存储列表中每个值的临时变量,可指定任何名称
避免缩进错误
不要遗漏了冒号
1 2 3 magicians = ['Black Swan' , 'Huohuo' , 'Silver Wolf' ] for magicain in magicians: print (magicain)
Black Swan
Huohuo
Silver Wolf
创建数值列表 range
函数 左开右闭
1 2 for value in range (1 ,5 ): print (value)
1
2
3
4
使用 range
函数 创建数字列表 可使用函数list()
将range()
的结果直接转换为列表
1 2 numbers = list (range (1 ,6 )) print (numbers)
[1, 2, 3, 4, 5]
range
可指定步长
1 2 even_numbers = list (range (2 ,11 ,2 )) print (even_numbers)
[2, 4, 6, 8, 10]
在上面这个例子里面,函数range()从2开始数,然后不断地加2,直到达到或超过终值(11)
1 2 3 4 5 6 7 squares = [] for value in range (1 ,11 ): square = value ** 2 squares.append(square) print (squares)
[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
列表简单统计 1 2 digits = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] min (digits)
0
1 2 digits = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] max (digits)
9
1 2 digits = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] sum (digits)
45
列表解析 1 2 squares = [value**2 for value in range (1 ,11 )] print (squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
要使用这种语法,首先指定一个描述性的列表名,如squares;然后,指定一个左方括号, 并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value**2,它计 算平方值。接下来,编写一个for循环,用于给表达式提供值,再加上右方括号
1 2 3 4 numbers = [number for number in range (1 ,1000001 )] min (numbers)max (numbers)sum (numbers)
500000500000
切片
左开右闭
要创建切片,可指定要使用的第一个元素和最后一个元素的索引
1 2 3 players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli' ] print (players[0 :3 ])
['charles', 'martina', 'michael']
1 2 3 players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli' ] print (players[1 :4 ])
['martina', 'michael', 'florence']
1 2 3 players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli' ] print (players[:4 ])
['charles', 'martina', 'michael', 'florence']
:
不可以缺失
1 2 3 players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli' ] print (players[1 :])
['martina', 'michael', 'florence', 'eli']
1 2 3 players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli' ] print (players[-3 :])
['michael', 'florence', 'eli']
遍历切片 1 2 3 4 players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli' ] print ("这是前三名队员" ) for player in players[:3 ]: print (player.title())
这是前三名队员
Charles
Martina
Michael
复制列表 复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引[:]
1 2 3 4 5 6 7 8 my_foods = ['pizza' , 'falafel' , 'carrot cake' ] friend_foods = my_foods[:] my_foods.append('ice cream' ) friend_foods.pop(2 ) print ("我最爱的食物是:" ) print (my_foods) print ("\n我朋友最爱的食物是:" ) print (friend_foods)
我最爱的食物是:
['pizza', 'falafel', 'carrot cake', 'ice cream']
我朋友最爱的食物是:
['pizza', 'falafel']
元组
Python将不能修改的值称为不可变的,而不可变的列表被称为元组 元组看起来犹如列表,但使用圆括号而不是方括号来标识
1 2 3 4 dimensions = (200 , 50 ) print (dimensions[0 ]) print (dimensions[1 ])
200
50
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 1
----> 1 dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment
试图修改元组的操作是被禁止的
1 2 3 4 dimensions = (200 , 50 ) for dimension in dimensions: print (dimension)
200
50
修改元组变量 虽然不能修改元组的元素,但可以给存储元组的变量赋值
1 2 3 4 5 6 7 8 9 10 dimensions = (200 , 50 ) print ("Original dimensions:" ) for dimension in dimensions: print (dimension) dimensions = (400 , 100 ) print ("\nModified dimensions:" ) for dimension in dimensions: print (dimension)
Original dimensions:
200
50
Modified dimensions:
400
100
设置代码格式