python学习——python编程从入门到实践(5)

本文通过一系列实例,深入探讨了Python中的if语句用法,包括条件判断、字符串比较、数值运算、逻辑运算符的应用,以及如何使用if语句进行列表元素的检查,适合初学者和进阶学习者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第5章 if 语句

课后题

#5-1
car='subaru'
print("Is car=='subaru'?I predict TRUE")
print (car == 'subaru')
print("\nIs car == 'audi'?I predict False")
print(car=='audi')

#5-2
str1='Sunday'
str2='sunday'
print(str1 == str2)
print(str1.lower()==str2)
num_1 = 2019
num_2 = 2020
print(num_1==num_2)
print(num_1 != num_2)
print(num_1>num_2)
print(num_1<num_2)
if num_1>2018 and num_2<2021:
    print("This is True.")
else:
    print("This is False.")

if num_1<2018 or num_2<2021:
    print("This is True.")
else:
    print("This is False.")

numbers = [ 1 , 3 , 6 ,7 ,9]
number = 8
if number in numbers:
    print(str(number) + " is in the numbers.")
else:
    print(str(number) + " is not in the numbers.")

# 5-3
alien_color='green'
if alien_color=='green':
    print("You have gotta 5 points.")
# 5-4
alien_color='red'
if alien_color=='green':
    print("You have gotta 5 points.")
else:
    print("You have gotta 10 points.")
# 5-5
alien_color='red'
if alien_color=='green':
    print("You have gotta 5 points.")
elif alien_color=='yellow':
    print("You have gotta 10 points.")
elif alien_color=='red':
    print("You have gotta 15 points.")
# 5-6
age = 26
if age<2:
    print("baby")
elif age>=2 and age<30:
    print("teenager")
elif age>30:
    print("adult")
# 5-7
favorite_fruits=['apple','banana','orange']
if "apple" in favorite_fruits:
    print("You really like apple!")
# 5-8
user_names=["A","B","C","D","admin"]
for user_name in user_names:
     if user_name =='admin':
         print("Hello admin,would you like to see a status report?")
     else:
         print("Hello Eric,thank you for logging in again")

# 5-9
user_names=["A","B","C","D","admin"]
n = 5
del user_names[-n:]
if user_names:
  for user_name in user_names:
     if user_name =='admin':
         print("Hello admin,would you like to see a status report?")
     else:
         print("Hello Eric,thank you for logging in again")
else:
    print("We need to find some users.")

 # 5-10
current_users=["A","B","C","D","E"]
new_users=["f","m","A","B"]
new_current_users=[]

for current_user in current_users:
    new_current_users.append(current_user)
    for new_user in current_users:
       if new_user in new_current_users:
           print(new_user+'1')
       else:
           print('2')
        
# 5-11
current_users = ['ADMIN', 'link', 'administrator', 'lisa', 'guest']

new_users = ['admin', 'GUEST', 'Park', 'tom', 'jams']

new_current_users = []

for current_user in current_users:
    new_current_users.append(current_user.lower())

for new_user in new_users:

    if new_user.lower() in new_current_users:

        print("User name has been occupied" )

    else:

        print("Welcome to back!!\n" + new_user)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值