Python学习-if语句

又要到周末,要放假了,上午专业案例课,老师全在讲arcgis的操作,跟遥感有关的,第四节操作课了,我一节课没听,先暂时不理会这个软件,以后方向研究上跟遥感牵扯的点很少。下午的遥感课,老师一个人在上面讲得得劲儿,下面的学生零星几个在听课,实惨!

今天主要学了if语句相关的知识,如何编写简单的if语句、if-else语句、if-elif-else语句、检查元素是否在列表里、使用多个列表等。这几天学的知识没来得及复习,真正开始学习起来才知道,时间是真的很不够用,需要学习的课程太多,很难一并抓起来,之前立了个flag,在12月前要把这本Python入门的书学完并且掌握好它,为之后的学习打下基础。

#10.29练习
cars=["bmw","audi","toyota","subaru"]
for car in cars:
    if car=="bmw":     #此处也需要冒号(:) #检查当前汽车名是否为bmw,如果是,则全以大写方式打印
        print(car.upper())
    else:
        print(car.title())

car="bmw"
print(car=="b")   #一个等号是陈述,两个等号是发问,两个等号可以解读为变量“car”的值是“bmw”吗

words="xiaohao love the girl"
if words!="xiaohao":  #!= 不等于
    print(words)

age=18
print(age==18)

answer=18
if answer!=19:
    print("hahahha")

age_0=18
age_1=20
print(age_0>=17 and age_1<=25)   #用and检查多个文件
print((age_0>=17)and(age_1<=25))   #也可以用and连接分开表达
print(age_0>=17 or age_1<=25)     ##用or检查多个文件

cars=["bmw","audi","toyota","subaru"]
print("bmw"in cars)      #用in来检查特定值是否包含在列表中
print("audi"in cars)
print("xiaoma"in cars)
print(19 in cars)

cars=["bmw","audi","toyota","subaru"]
if "bmw" not in cars:     #此处“bmw”可以用一个变量来定义,如xiaoma="bmw"
    print("xiaoma")
else:
    print(f'this my hometown {cars[3].upper()}!')
#P69小练习
car="toyota"
print("Is car=='toyota'? I predict True.")
print(car=="bmw")
print("\nIs car=='audi'?I predict False.")
print(car=="bmw")
xiaoli="boy"
print(xiaoli=="boy")
print(xiaoli=="girl")
xiaohao="good boy"
if xiaohao!="bad boy":
    print(xiaohao)

age_0=20
age_1=21
age_2=22
print(age_0==18)
print(age_1==21)
print(age_0>=15 and age_1<=30)
print((age_2>=20)and (age_1>=16))
if age_2!=23:
    print(age_0)
cars=["bmw","audi","toyota","subaru"]
if "hah"in cars:
    print(f"My hometown very good!the {cars[3].upper()} is good!")
else:
    print("xiaohao zui niubi")
cars=["bmw","audi","toyota","subaru"]
if "hah" not in cars:
    print(f"My hometown very good!the {cars[3].upper()} is good!")
else:
    print("xiaohao zui niubi")
if conditional_test:
    do something
age=19
if age>=15:
    print("You are welcome!")
age=20
if age>=16:
    print("mymymymymy")   #if语句中的缩进跟for循环的缩进相同
    print("hahahahahhah")
#if-else语句
age=20
if age>=23:
    print("good!")
    print("You are my person!")
else:
    print("worry!")
    print("Go back!")
#if-elif-else语句
age=18
if age<10:
    print("go")
elif age<=18:     #elif代码仅在前面测试未通过的情况下才会运行
    print("come")
else:
    print("go out!")
xiaohao=["xiaojin","xiaobing","xiaopei"]
if "xiaoji"in xiaohao:
    print("go")
elif "xiaobing" in xiaohao:
    print("come")
else:
    print("go out!")
age=25
if age<12:
    price=0
elif age<=18:    #s使用多个elif测试块,跟使用单个elif测试块原理相同
    price=50
elif age <= 30:
    price = 80
else:
    price=100
print(f"you admission cost is ${price}!")   #输入句子向用户指明门票价格

age=25
if age<12:
    price=0
elif age<=18:
    price=50
elif age <= 30:   #Python中可以省略else代码块
    price = 80
print("hahahha")
age=25
if age<12:
    price=0
if 12<=age<=30:  #多个elif代码块可用多个if语句代替
    price=50
print(f"the ${price}!")
#P75小练习
alien_color="green"
if alien_color=="green":
    print("你获得了5积分!")    #if语句中变量只能用“!=”“==”等连接,不能直接用单等号“=”
else:
    print("我错了")
alien_color="yellow"
if alien_color=="green":
    print("你获得了5积分!")

alien_color="yellow"
if alien_color=="green":
    print("你因射杀该外星人获得5积分")
else:
    print("你获得了10积分")

alien_color="yellow"
if alien_color=="green":
    print("你获得了5分")
elif alien_color=="yellow":
    print("你获得了10积分")
else:
    print("你获得了15积分")

age=1000
if age<2:
    print("我还是个婴儿")
elif age>=2 and age<4:
    print("我是个幼儿")
elif age>=4 and age<13:
    print("我是个儿童")
elif age>=13 and age<20:
    print("我是个青少年")
elif age>=20 and age<65:
    print("我是个成人啦")
else:
    print("我是一个老年人啦,不要欺负我哦!")
favorite_fruits=["apple","orange","banana"]
fruit="hahah"
if fruit in favorite_fruits:   #可以定义一个变量,用变量加到for循环中
    print("You really like apple!")
else:
    print("oh no")
favorite_fruits=["apple","orange","banana"]
if "apple" in favorite_fruits:   #可以直接不定义变量直接加到for循环语句中
    print("You really like apple!")
I_want_eat=["noddle","rice","apple"]
for apples in I_want_eat:
    print(apples)
print("I want to eat!")
I_want_eat=["noddle","rice","apple"]
for apples in I_want_eat:
    if apples=="haha":
        print("hahhahahahahah")
    else:
        print("xiaohaihai")
print("\n我要吃这个")
#确定列表是不是空的
favorite_fruits=[]    #创建一个空列表用于检验
if favorite_fruits:
    for favorite_fruit in favorite_fruits:
        print(f"Adding {favorite_fruit}.")
    print("\nFinished making your pizza!")
else:
    print("Are you sure you want a plain pizza?")
available_topping=["mushrooms","olives","green peppers","pepperoni"]   #披萨店供应的配料
requested_toppings=["mushrooms","french fries","extra cheese"]   #顾客点的配料
for requested_topping in requested_toppings:   #遍历顾客点的配料
    if requested_topping in available_topping:  #检查顾客点的配料是否在店铺供应的配料中
        print(f"Adding {requested_topping}.")
    else:
        print(f"Sorry,we don't have {requested_topping}.")
print("\nFinished making your pizza!")
#P78小练习
users=["admin","xiaolei","xiaoyang","xiaoli"]
for user in users:
    if user=="admin":
        print(f"Hello {user},woeld you like to see a status report?")
    else:
        print(f"Hello {user},thank you for logging in again")
users=["admin","xiaolei","xiaoyang","xiaoli"]
del users[0:4]
print(users)
users=[]
if users:
    for user in users:
        print(user)
    else:
        print("We need to find some users!")
names=[]
if names:
	for name in names:
		if name=='admin':
			print('\nHello Admin,would you like to see a status report?')
		else:
			print('\nHello '+name.title()+',thank you for logging in again.')
else:
	print('We need to find some users.')
current_users=["xiaolei","xiaoyang","xiaocao","xiaofang","xiaogan"]
new_users=["xiaoha","xiaowang","xiaolei","xiaoyang","xiaodi"]
for new_user in new_users:
    if new_user in current_users:
        print("我需要别的用户")
    else:
        print(f"{new_user}未被使用")
current_users=["xiaolei","xiaoyang","xiaocao","xiaofang","xiaogan"]
current_users_1=["xiaolei","xiaoyang","xiaocao","xiaofang","xiaogan"]
new_users=["xiaoha","xiaowang","xiaolei","xiaoyang","xiaodi"]
for new_user in new_users:
    if new_user in current_users:
        print("我需要别的用户")
    else:
        print(f"{new_user}未被使用")
numbers=list(range(1,10))
print(numbers)
for number in numbers:
    if number == 1:
        print(str(number)+"st")
    elif number==2:
        print(str(number)+"nd")
    elif number==3:
        print(str(number)+"rd")
    else:
        print(str(number)+"th")

#if语句格式设置,在诸如== >= <=等比较运算符两边各添加一个空格

明天周六,虽然放假,但还是得继续学习,明天继续来教室。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值