《Python编程 从入门到实践》day5

本文介绍了Python中列表的切片、复制,元组的定义与遍历,以及if语句的基本用法,包括条件测试和布尔表达式的应用。同时强调了代码格式设置的重要性,如缩进、行宽和空行规则。
摘要由CSDN通过智能技术生成

昨日知识点回顾:

遍历列表

min()、max()、range() 函数运用

今日知识点学习:

4.4 使用列表的一部分

        4.4.1 切片 列表名[索引1,索引2]        

squares = [value**2 for value in range(1,11)]
print(squares)
print(squares[2:5])

#运行结果:[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#[9, 16, 25]

        4.4.2 遍历切片

                for 列表名单数 in 列表名复数[:元素个数]:

squares = [value**2 for value in range(1,11)]
print(squares)
print(squares[2:5])
for square in squares[:5]:
    print(square)

#运行结果:[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#[9, 16, 25]
# 1
# 4
# 9
# 16
# 25

        4.4.3 复制列表 [:]        

list1 = list(range(0,7))
list2 = list1[:] #复制列表
list3 = list1 #复制列表地址
list1.append(8) #list1最后添加元素8
list2.append(9) #list2最后添加元素9
list3.append(10) #list3最后添加元素10
print(list1)
print(list2)
print(list3)

#运行结果:
# [0, 1, 2, 3, 4, 5, 6, 8, 10]
# [0, 1, 2, 3, 4, 5, 6, 9]
# [0, 1, 2, 3, 4, 5, 6, 8, 10]

test 4-10 切片

list = list(range(0,16))
print(list)
print('The first three items in the list are:')
print(list[0:3])
print('Three items from the middle of in the list are:')
print(list[6:9])
print('The last three items in the list are:')
print(list[-3:])

#运行结果:
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# The first three items in the list are:
# [0, 1, 2]
# Three items from the middle of in the list are:
# [6, 7, 8]
# The last three items in the list are:
# [13, 14, 15]

4.5 元组

        列表中的元素是可修改的,元组中的元素是不可修改的

        4.5.1 定义元组   

dimensions =(100,50 )
print(dimensions[0])
print(dimensions[1])
#运行结果:
#100
#50

# dimensions[0] = 150
#运行结果:TypeError: 'tuple' object does not support item assignment

        4.5.2 遍历元组中的值        

dimensions = (100,50 )
for dimension in dimensions:
    print(dimension)

#运行结果:
#100
#50

        4.5.3 修改元组变量

dimensions = (100,50 )
for dimension in dimensions:
    print(dimension)
dimensions = (150,50)
print('修改后的尺寸:')
for dimension in dimensions:
    print(dimension)
#运行结果:
# 100
# 50
# 修改后的尺寸:
# 150
# 50

4.6 设置代码格式

        4.6.1 格式设置指南

                代码阅读的次数比编写的次数多,让代码变得易于阅读

        4.6.2 缩进

                每级缩进使用4个空格

        4.6.3 行长

                代码每行不超过80字符、注释每行不超过72字符

        4.6.4 空行

                一个空行隔离不同功能的代码

        4.6.5 其他格式设置指南

                PEP 8指南

第五章 if 语句

5.1 一个简单示例

cars = ['audi','bmw','benz','toyota','honda']
for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

#运行结果:
# Audi
# BMW
# Benz
# Toyota
# Honda

5.2 条件测试

        5.2.1 检查是否相等                

car = 'bmw'
print(car == 'bmw')
print(car == 'audi')

# 运行结果:
# True
# False

        5.2.2 检查是否相等时区分大小写

car = 'BMW'
print(car == 'bmw')
print(car.lower() == 'bmw')

# 运行结果:
# False
# True

        5.2.3 检查是否不相等

car = 'audi'
if car.title() != 'bmw':
    print('The car is not bmw!')
    
# 运行结果:
# The car is not bmw!

        5.2.4 比较数字

print(18 > 19)
print(18 != 19)

# 运行结果:
# False
# True

        5.2.5 检查多个条件

                条件1 and 条件2:任一条件为假,结果为False

                条件1 or 条件2:任一条件为真,结果为Ture

print(18 > 19 and 20 > 18)
print(18 > 19 or 20 > 18)

# 运行结果:
# False
# True

        5.2.6、5.2.7 检查特定值是否(不)包含在列表内

number = list(range(1,12))
print(number)
print(2 in number)
print(18 in number)
print(18 not in number)

# 运行结果:
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
# True
# False
# True

        5.2.8 布尔表达式

                布尔值用于记录条件,当值为True时,代码继续运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值