首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

《Python编程:从入门到实践》 第四章 笔记

在大多数计算机中,终端窗口每行只能容纳 79 字符4.1  遍历整个列表

names = ['irving','smart','tatum','brown','horford']

fornameinnames:

print(name)

irving

smart

tatum

brown

horford

4.1.2  在 for 循环中执行更多的操作

names = ['irving','smart','tatum','brown','horford']

fornameinnames:

print(name.title() +",I wanne see you win the champion!"+"\n")

Irving,I wanne see you win the champion!

Smart,I wanne see you win the champion!

Tatum,I wanne see you win the champion!

Brown,I wanne see you win the champion!

Horford,I wanne see you win the champion!

4.1.3  在 for 循环结束后执行一些操作

names = ['irving','smart','tatum','brown','horford']

fornameinnames:

print(name.title() +",I wanne see you win the champion!"+"\n")

print("And Smart win the FMVP!!!")

Irving,I wanne see you win the champion!

Smart,I wanne see you win the champion!

Tatum,I wanne see you win the champion!

Brown,I wanne see you win the champion!

Horford,I wanne see you win the champion!

And Smart win the FMVP!!!

4.3.1  使用函数 range()

foriiiinrange(1,6):

print(iii)

4.3.2  使用 range() 创建数字列表

number =list(range(1,6))

print(number)

[1, 2, 3, 4, 5]

使用函数 range() 时,还可指定步长。例如,下面的代码打印 1~10 内的偶数:

number =list(range(2,11,2))

print(number)

[2, 4, 6, 8, 10]

使用函数 range() 几乎能够创建任何需要的数字集,例如,如何创建一个列表,其中包含前 10 个整数(即 1~10 )的平方呢?在 Python 中,两个星号( ** )表示乘方运算。下面的代码演示了如何将前 10 个整数的平方加入到一个列表中:

squares = []

forvalueinrange(1,11):

squares.append(value**2)

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.3.3  对数字列表执行简单的统计计算

digits = [,4,5,9,11,20,34,42]

print(min(digits))

print(max(digits))

print(sum(digits))

42

125

4.4  使用列表的一部分

4.4.1  切片

cs_player = ['Irving','Horford','Tatum','Browm','Harward','Smart']

print(cs_player[:3])

['Irving', 'Horford', 'Tatum']

cs_player = ['Irving','Horford','Tatum','Browm','Harward','Smart']

print(cs_player[1:3])

['Horford', 'Tatum']

cs_player = ['Irving','Horford','Tatum','Browm','Harward','Smart']

print(cs_player[:4])

['Irving', 'Horford', 'Tatum', 'Browm']

cs_player = ['Irving','Horford','Tatum','Browm','Harward','Smart']

print(cs_player[2:])

['Tatum', 'Browm', 'Harward', 'Smart']

cs_player = ['Irving','Horford','Tatum','Browm','Harward','Smart']

print(cs_player[-2:])

['Harward', 'Smart']

4.4.2  遍历切片

cs_players = ['Irving','Horford','Tatum','Browm','Harward','Smart']

print("Welcome Celtics's players!")

forcs_playerincs_players:

print(cs_player)

Welcome Celtics's players!

Irving

Horford

Tatum

Browm

Harward

Smart

4.4.3  复制列表

my_fav_team =['c','e','l','t','i','c','s']

champion =my_fav_team[:]

print("My favourite team is:")

print(my_fav_team)

print("And the champion is:")

print(champion)

My favourite team is:

['c', 'e', 'l', 't', 'i', 'c', 's']

And the champion is:

['c', 'e', 'l', 't', 'i', 'c', 's']

exercises = ['squat','benching','dead lift']

fitness = exercises[:]

exercises.append('pull down')

fitness.append('chinning bar')

print(exercises)

print(fitness)

['squat', 'benching', 'dead lift', 'pull down']

['squat', 'benching', 'dead lift', 'chinning bar']

对比(区别在第二行)

exercises = ['squat','benching','dead lift']

fitness = exercises

#这样不对

exercises.append('pull down')

fitness.append('chinning bar')

print(exercises)

print(fitness)

['squat', 'benching', 'dead lift', 'pull down', 'chinning bar']

['squat', 'benching', 'dead lift', 'pull down', 'chinning bar']

这里将 exercises 赋给 fitness,而不是将 exercises的副本存储到fitness。这种语法实际上是让 Python 将新变量 fitness关联到包含在 exercises 中的列表,因此这两个变量都指向同一个列表。鉴于此,当我们将 'pull down' 添加到 exercises 中时,它也将出现在 fitness中;同样,虽然 'chinning bar' 好像只被加入到了fitness 中,但它也将出现在这两个列表中。

4.5  元组

4.5.1  定义元组

不可修改的的列表。

eg:('c', 'e', 'l', 't', 'i', 'c', 's')

4.5.2  遍历元组中的所有值

元祖无法修改元素但是可以修改变量赋值

exercises = ('squat','benching','dead lift')

forexerciseinexercises:

print(exercise)

print(" ")

exercises = ('pull down','chinning bar')

forexerciseinexercises:

print(exercise)

squat

benching

dead lift

pull down

chinning bar

4.6  设置代码格式

如果一定要在让代码易于编写和易于阅读之间做出选择, Python 程序员几乎总是会选择后者。

4.6.2  缩进

PEP 8 建议每级缩进都使用四个空格,这既可提高可读性,又留下了足够的多级缩进空间。

4.6.3  行长

在大多数计算机中,终端窗口每行只能容纳 79 字符。

在大多数编辑器中,都可设置一个视觉标志 —— 通常是一条竖线,让你知道不能越过的界线在什么地方。

要将程序的不同部分分开,可使用空行。你应该使用空行来组织程序文件,但也不能滥用;只要按本书的示例展示的那样做,就能掌握其中的平衡。例如,如果你有 5 行创建列表

的代码,还有 3 行处理该列表的代码,那么用一个空行将这两部分隔开是合适的。然而,你不应使用三四个空行将它们隔开。

空行不会影响代码的运行,但会影响代码的可读性。 Python 解释器根据水平缩进情况来解读代码,但不关心垂直间距。

4-15 代码审核 :从本章编写的程序中选择三个,根据 PEP 8 指南对它们进行修改。

1、每级缩进都使用四个空格。

2、每行都不要超过 80 字符。

3、不要在程序文件中过多地使用空行。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20171213G0CR2F00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券