Python Base——Part 4

Python Base——Part 4

好久不见,今天继续和大家分享关于Python的一些基础知识

本次分享的主要内容包括两个部分:

第一部分:遍历切片;复制列表;元组

#遍历切片
for player in players[:3]: #对数值列表遍历是range(),对字符列表遍历是用索引
    print(player.title())
#复制列表
players=["a","b","c","d","e","f","g","h","i","j"]
best_players=players[:3] #复制包含players切片的列表
print(best_players)
all_players=players[:]
print(all_players) #复制players整个列表
#检查一下是不是真的存在以上这些不同的经过复制后的列表
print(players)
best_players.append("lisa")
print(best_players)
all_players.append("bill")
print(all_players)
#接下来的这种“复制”方法为错误示例
whole=players #这里并非是复制,而是直接将两者变成了同一个列表
print(whole)
whole.append("jack") #会发现,如果在whole列表添加一个元素,原来的players列表也被增加了这个值
print(whole)
print(players) #输出结果表明,两个列表是同一个,它们变成了同一个列表
#练习4.10
friends=["lisa","bill","jack","maria","hellen"]
print("The first three items in the list are:")
for friend in friends[0:3]:
    print(friend.title())
print("Three items from the middle of the list are:")
for friend in friends[1:-1]:
    print(friend.title())
print("The last three items in the list are:")
for friend in friends[-3:]:
    print(friend.title())
#练习4.2
my_friends=friends[:]
friends.append("cindy")
my_friends.append("troye")
print("His friends are:")
for friend in friends[:]:
    print(friend.title())
print("My friends are:")
for my_friend in my_friends[:]:
    print(my_friend.title())
#元组:Python将不能修改的值称为不可变的,而不可变的列表称为元组
#元组使用圆括号而非中括号来标识
dimensions=(200,50)
for dimension in dimensions:
    print(dimension)
#元组中的元素是不可以被修改的,如下所示:
#将dimensions中的第一个值修改为300
#dimensions[0]=300 #程序会报错“TypeError: 'tuple' object does not support item assignment”
rect=130,30
for rec in rect:
    print(rec)
print(rect)
#注意:严格意义上来说,元组是由逗号来定义的,圆括号只是为了让元组看起来更整洁,更清晰
#如果要创建一个只包含一个元素的元组,那么必须在这个元素后面加上逗号
what=3,
print(what)
#创建一个只包含一个元素的元组通常没有意义,但是自动生成的元组有可能只有一个元素
#虽然不能修改元组的元素,但是可以给元组赋值
dimensions=(200,50)
print("Original dimensions are:")
for dimension in dimensions:
    print(dimension)
dimensions=(100,20)
print("Modified dimensions are:")
for dimension in dimensions:
    print(dimension)
#练习4.13
foods="watermelon","cucumben","peach","icecream","cola"
for food in foods:
    print(food.title())
foods="water","cucumber","peach"
for food in foods:
    print(food.title())

第二部分:if语句(以及if语句叠加for循环);如何编写结果要么为 True 要么为 False 的简单条件测试;如何编写简单 的 if 语句、if-else 语句和 if-elif-else 结构,并且在程序中使用这些结构来测试特定的条件, 以确定这些条件是否满足;如何在利用高效的 for 循环的同时,以不同于其他元素的方式对特定 的列表元素进行处理等

#简单示例
cars=['audi','messides','bmw','porsche']
for car in cars:
    if car=='bmw':
        print(car.upper())
    else:
        print(car.title())
#条件测试:每条if语句的核心都是一个值为True或False的表达式,这种表达式称为条件测试
car="bmw"
car=="toyota" #那么测试将会返回“False”
car=="bmw" #那么测试将会返回“True”
#如果条件测试的值为True,那么Python就会执行紧跟在if后面的代码;如果是False,那么Python将会忽略if后面的代码
car=="BMW"
#此时,测试同样将会返回False,因为Python在检查是否相等时区分大小写
#两个大小写不同的值将被视为不相等的值
car.lower()=="bmw"
#如果大小写无关紧要,只想检查变量的值,那么就可以和上面一样,将变量的值转换为小写,再进行比较
#这种情况下,lower()并不会改变原始列表car的大小写
cat="brandy"
if cat != "hello kitty":
    print("What I want is Hello Kitty!")
#检查两个值是否不相等,结合使用惊叹号和等号(!=)

#检查数值是否相等
age = 18
age == 18
answer = 42
if answer != 18:
    print("It's not correct. Please try again!")

#大小写比较
length = 18
length < 28 #测试将会返回True
length > 15 #测试将会返回True
length <= 18 #测试将会返回True
length >= 22 #测试将会返回False

#检查lisa和bill两个人的年龄是否都不小于21岁
lisa = 19
bill = 23
lisa >= 21 and bill >= 21 #测试将会返回False

#检查lisa和bill至少有一个人的年龄不小于21岁
lisa >=21 or bill >= 21 #测试将会返回True

#检查特定的值是否包含在列表中
cars=['audi','messides','bmw','porsche']
"toyato" in cars
#测试将会返回False
"audi" in cars
#测试将会返回True

#检查特定值是否不在列表中
"toyota" not in cars #测试将会返回False
like="toyato"
if like not in cars:
    print(f"What I want to buy is {like.title()}!")

#布尔表达式:条件测试的别名
#与条件表达式一样,布尔表达式的结果要么为 True,要么为 False
#布尔值通常用于记录条件,如游戏是否正在运行,或者用户是否可以编辑网站的特定内容
game_active = True 
can_edit = False 

#练习5.1
medi = "car"
print("medi == 'hhh'. I think is not.")
print(medi == 'what')
print('medi == "car", I think is so.')
print(medi == "car")
a = "lisa"
b = "Lisa"
print(a==b)
print(a==b.lower())
x = 8
y = 4
print(x >= y)
print(x <= y)
print(x == y)
print(x != y)
print(x < y)
print(x<y and y<2)
print(x == 2**3 or y>6)
bingo=["yes","not"]
bingoo=["don't"]
if bingo == bingoo:
    print("really")
else:
    print("yet")
print("but" in bingo)
print("yes" in bingo)
x = 6
y = 4
if x == y*2:
    z=x*y
else:
    z=x**y
print(z)

#简单的if语句
age = 17
if age >= 15:
    print("Congratulations! you can participate this vote.")
#if-else语句
if age >= 18:
    print("Congratulations! you can participate this vote.")
else:
    print("Sorry, you do not specify our criteria.")
#if-elif-else语句
if age <= 15:
    print("Sorry, you do not specify our criteria.")
elif age <= 18:
    print("We will invite you when you are 18 years old.")
else:
    print("You are too old to participate this party.")

今天的分享就这么多了。欢迎大家莅临我的另一个草稿箱(公众号:统计小菜椒)

(ps:不开心的话就学Python)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值