当前位置:首页>python>《Python:从入门到实践》第一章——第三章学习笔记

《Python:从入门到实践》第一章——第三章学习笔记

  • 2026-08-24 16:01:58
《Python:从入门到实践》第一章——第三章学习笔记

Python:从入门到实践

第一章

没啥好讲的,部署环境而已

第二章

1 变量

eg:

message = "Hello Python world"print(message)

"message"就是变量

注意:

变量名只能包含数字、字母和下划线。且不能以数字打头;

变量名不能包含空格;

不要将Python关键字和函数名用作变量名;

变量名应当既简短又有描述性;

2 字符串

message = "Hello Python world"

"Hello Python world!"就是字符串

用引号引起的都是字符串

3 大小写函数

首字母大写:title( )

全大写:upper( )

全小写:lower( )

4 制表符和换行符

\t:向后移动四个,tab功能

\n:换行

5 strip函数

可以删除字符串中的空白,但只是暂时的,没有改变变量本身

lstrip和rstrip分别删除左侧和右侧空白

6 删除前缀:removeprefix( )

eg:

nostarch_url = 'https://nostarch.com'print(nostarch_url.removeprefix('https://'))

输出结果:nostarch.com

同理,删除后缀则是removesuffix( )

第三章

1 列表

由一系列按特定顺序排列的元素组成,通常包含多个元素

用方括号([ ])表示

如何访问列表元素?

bicycles = ['trek', 'cannondale', 'redline', 'specialized']print(bicycles[0])

输出结果为trek

第一个列表元素的索引为0

最后一个列表元素的索引为 -1/最后一个数字

2 修改列表元素

motorcycles = ['honda', 'yamaha', 'suzuki']motorcycles[0] = 'ducati'print(motorcycles)

输出结果为:['ducati', 'yamaha', 'suzuki']

首先定义了列表motorcycles,然后将列表里第一个值从'honda'修改为'ducati'

3 在列表中添加元素

(1)在列表末尾添加元素

motorcycles = ['honda', 'yamaha', 'suzuki']motorcycles.append('ducati')print(motorcycles)

输出结果为:['honda', 'yamaha', 'suzuki', 'ducati']

甚至可以创造一个空列表,然后调用append( )逐个添加

motorcycles = []motorcycles.append('honda')motorcycles.append('yamaha')motorcycles.append('suzuki')print(motorcycles)

(2)在列表中插入元素

motorcycles = ['honda', 'yamaha', 'suzuki']motorcycles.insert(0, 'ducati')print(motorcycles)

输出结果为:['ducati', 'honda', 'yamaha', 'suzuki']

insert( )方法在0处添加控件,将'ducati'存储于此

其实只要记住insert( )会把括号里的东西放在索原引处的左侧即可

4 在列表中删除元素

(1)del语句

motorcycles = ['honda', 'yamaha', 'suzuki']del motorcycles[0]print(motorcycles)

输出结果为:['yamaha', 'suzuki']

(2)使用pop( )方法

motorcycles = ['honda', 'yamaha', 'suzuki']print(motorcycles)popped_motorcycle = motorcycles.pop()print(motorcycles)print(popped_motorcycle)

输出结果为:

['honda', 'yamaha', 'suzuki']

['honda', 'yamaha']

suzuki

由此可见,使用pop( )方法的好处主要是能继续使用删除的元素。

再举个例子:

motorcycles = ['honda', 'yamaha', 'suzuki']last_owned = motorcycles.pop()print(f"The last motorcycle I owned was a {last_owned.title()}.")

输出结果为:The last motorcycle I owned was a Suzuki.

(3)删除列表中任意位置的元素

motorcycles = ['honda', 'yamaha', 'suzuki']first_owned = motorcycles.pop(0)print(f"The first motorcycle I owned was a {first_owned.title()}.")

输出结果为:The first motorcycle I owned was a Honda.

如何确定使用del语句还是pop( )方法?

主要看后续是否还需要使用删除的元素。不使用用del( ),使用用pop( )

(4)根据值删除元素

有事不知道删除的值在哪里,只知道删除元素的值,可以使用remove( )方法

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']motorcycles.remove('ducati')print(motorcycles)

输出结果为:['honda', 'yamaha', 'suzuki']

使用remove( )方法后,依然可以使用删除元素的值:

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']too_expensive = 'ducati'motorcycles.remove(too_expensive)print(motorcycles)print(f"\nA {too_expensive.title()} is too expensive for me.")

输出结果为:

['honda', 'yamaha', 'suzuki']

A Ducati is too expensive for me.

5 管理列表

(1)使用sort( )方法对列表进行永久排序

sort( )方法可以将列表以字母顺序排列

cars = ['bmw', 'audi', 'toyota', 'subaru']cars.sort()print(cars)

输出结果为:['audi', 'bmw', 'subaru', 'toyota']

也可以按字母相反顺序排列:

cars = ['bmw', 'audi', 'toyota', 'subaru']cars.sort(reverse=True)print(cars)

输出结果为:['toyota', 'subaru', 'bmw', 'audi']、

(2)使用sorted( )函数对列表进行临时排序

cars = ['bmw', 'audi', 'toyota', 'subaru']print("Here is the original list:")print(cars)print("\nHere is the sorted list:")print(sorted(cars))

输出结果为:

Here is the original list:

['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:

['audi', 'bmw', 'subaru', 'toyota']

同时,也可以参考上面运用sort( )进行字母顺序排序相反的方法(reverse=True)进行排序

(3)反向打印

可运用reverse( )方法

cars = ['bmw', 'audi', 'toyota', 'subaru']print(cars)cars.reverse()print(cars)

输出结果为:

['bmw', 'audi', 'toyota', 'subaru']

['subaru', 'toyota', 'audi', 'bmw']

与之前sort(reverse=True)不同,reverse( )是直接对原表格进行相反排序

(4)确定表格长度

运用len( )方法即可

cars = ['bmw', 'audi', 'toyota', 'subaru']print(len(cars))

输出结果:4

最新文章

随机文章