python学习笔记03(判断语句,检测列表中的值,多个列表)

Hello,新的一天,你学习了吗?
在这里插入图片描述

1.if语句

前面虽然写了一点if语句,但只是比较初级的,不太完善

1.1一个简单的尝试

nums=['1','2','a','=']
for num in nums:
    if num =='1' or num =='2':
        print(num+" is a number")
    elif num == 'a':
        print(num+" is a word")
    else:
        print("i don't know "+num)
     ---------------------------------------------
1 is a number
2 is a number
a is a word
i don't know =

1.2检测是否相等

love="i love you"
love == "i love you"
------------------------------------------
True
1+1 == 3
----------------------------
False

检测是否相等时,要区分大小写python中区分大小写很重要

word_1='apple'
word_2='Apple'
word_1 == word_2
---------------------------------
False

如果你比较时,需要的工作不介意大小写,那么你可以转化它

word_1='apple'
word_2='Apple'
word_1 == word_2.lower()
-------------------------
True

1.3检测是否不相等

a="i don't love you"
b="i don't talk to you"
a != b
--------------------------
True

哈哈,不和你说话不等于不喜欢你

2.比较数字

age =18
age==18
----------------------
True
age =18
age<=20
-----------------
True

3.检查多个条件

3.1“与”关系

a=125
b=521
if a==125 and b==521:
    print("this is love")
else:
    print("this is not love")
 ------------------------------
 this is love

3.2“或”关系

a='i kiss you'
b='you kiss me'
if a=="i don't kiss you" or b=='you kiss me':
    print("i will kiss you too")

-------------------------
i will kiss you too

4.检测特定值是否在列表中

words=['a','b','c']
find_word='d'
if find_word in words:
    print("yes")
else:
    print("no")
 ------------------
 no

5.检测特定值是否不在列表中

words=['a','b','c']
find_word='d'
if find_word not in words:
    print("yes")
else:
    print("no")
 -----------------
 yes

我在 in 前面加了一个 not ,就是不在的意思

6.布尔表达式

game_began=True
game_over=False

常常用在游戏设计中

7.使用多个列表

you_haves=['apple','rice','milk']
i_needs=['apple','rice','noodles']
for i_need in i_needs:
    if i_need in you_haves:
        print("ok ,wait a little please")
    else:
        print("sorry,we don't have "+i_need)
    ---------------------------
ok ,wait a little please
ok ,wait a little please
sorry,we don't have noodles

这是一个点餐的情景,使用类似上面的代码可以实现。
很实用,哈哈哈
这篇就到这里啦,时间还早,下午再继续学习,总结
随手点个赞吧,自学之路不容易,哈哈
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据攻城小狮子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值