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
这是一个点餐的情景,使用类似上面的代码可以实现。
很实用,哈哈哈
这篇就到这里啦,时间还早,下午再继续学习,总结
随手点个赞吧,自学之路不容易,哈哈