《Python编程从入门到实践》——学习python的第六天

Python学习的第六天

前言

今天是学习python的第六天,太忙了,所以更新有点推迟了,话不多说咱们直接上干货吧。

if语句的补充

省略else代码块

为什么要省略else语句?

因为else是一条包罗万象的语句,只要不满足任何if或者elif中的条件测试,其代码就会被执行,这可能会引入无效或者恶意的数据。

测试多个条件(仅使用多个if语句)
目的:检查我所测试的条件是否出现错误,并且对多个同时进行提高检测效率

request_toppings = ['mushroom','extra cheese']#对多个条件进行检测时,我们使用的是单个if语句而不是if_elif_else结构来进行检测的
if 'mushroom' in request_toppings:
    print("Adding mushrooms")
if 'pepperoni' in request_toppings:
    print("Adding pepperoni")
if 'extra cheese' in request_toppings:
    print("Adding extra cheese")

print("\n Finished making your pizza")

结果:
Adding mushrooms
Adding extra cheese

Finished making your pizza

使用if语句来处理列表

主要作用是:对特定的元素进行特定的操作

检查特殊元素

request_toppings = ['mushroom','green peppers','extra cheese']
for request_topping in request_toppings:#for的用法:for + 变量 in 列表:就欧克了  其实就相当于遍历
    if request_topping == 'green peppers':#这里的特殊元素是“green peppers”
        print("Sorry ,we are out of green peppers right right now")
    else:
        print(f"Adding {request_topping}.")
print("\nFinished making your pizza")

结果:
Adding mushroom.
Sorry ,we are out of green peppers right right now
Adding extra cheese.

Finished making your pizza

确定列表不是空的

确定列表是不是空的我们所使用的语句是 if与for镶嵌 将列表中的元素作为if的判断条件

request_toppings = []
if request_toppings:#如果使用一个列表用作条件的话,Python将在列表内容为空的情况下 返回False,当至少存在一个元素时都会返回True
    for request_topping in request_toppings:
        print(f"Adding {request_topping}.")
        print("\nFinished making your pizza!")#如果有元素则会打印出该语句
else:
    print("Are you sure you want a plain pizza")

结果:
Are you sure you want a plain pizza

使用多个列表
使用的场景。假如在一家披萨店,有一个官方的配料表格,而一些顾客缺喜欢往披萨里面加入一些非常奇怪的配料,但是我们也得满足顾客的需求

available_toppings = ['mushroom','olives','green peppers','pepperoni','pineapple','extra cheese']#定义官方的菜单
requested_toppings = ['mushroom','french fries','extra cheese']#这里是客户所需要的菜单
for requested_topping in requested_toppings:#循环客户中的菜单
    if requested_topping in available_toppings:#将顾客的表单中的元素在官方的菜单中进行对应
        print(f"Adding {requested_topping}.")
    else:
        print(f"Sorry,we don't have {requested_topping}.")#而“french fries”是官方菜单中没有的所以我们将其打印出来,并进行解释
        print("\nFinished making your pizza")     

结果:
Adding mushroom.
Sorry,we don’t have french fries.
Adding extra cheese.

Finished making your pizza

设置if语句
在变量的前后加上空格和符号前后,使得编码更加的美观

字典

字典相当于,一个线索对应一个值吧,可以这样理解。

一个简单的字典

yinyang = {'age':'18','tall':'180'}


print(yinyang['age'])
print(yinyang['tall'])

结果:
18
180

总结:字典的名称是yinyang,它储存了年龄:18,身高:180。而且这里字典的定义是使用的“{}”,并且每类信息对应的具体信息是使用冒号来的

使用字典

alien_0 = {'color':'green','points':'5'}


new_points = alien_0['points']
print(f"You just earned {new_points} points!")

结果:
You just earned 5 points!

总结:当想要使用一个字典时,首先得进行定义,其次需要定义一个新变量将两者相关联,再使用于相关的语句。

注意:color:green 是一对键值对 'color’是键,'green’是值,两个一块称为键值对。

添加键值对
字典往往需要进行更新,所以得添加键值对

alien_0 = {'color':'green','points':'5'}
print(alien_0)


alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)

结果:
{‘color’: ‘green’, ‘points’: ‘5’}
{‘color’: ‘green’, ‘points’: ‘5’, ‘x_position’: 0, ‘y_position’: 25}

总结:对于键值对的添加,首先要明确的是你定义了一个字典,并且进行添加键值对的格式是:字典名[‘类别名’] = 具体信息。并且加入的数值也是按顺序来的

今天就到这里了,其实整篇看下来应该也不到20来分钟吧,应该会更短的。
加油哦!!!

  • 42
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 24
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值