Skip to content
python
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())
Audi
BMW
Subaru
Toyota
python
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
True
False
python
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
    print(f"{user.title()}, you can post a response if you wish.")
Marie, you can post a response if you wish.
python
# 5.1
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')

print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
Is car == 'subaru'? I predict True.
True

Is car == 'audi'? I predict False.
False
python
# 5.2
print('aaa' == 'aaa')
print('aa' != 'ab')
print('AAB'.lower() == 'aab')
print(1==1 and 2!=3 and 3>2 and 5<6 and 3>=3 and 7<=9)
print((1==3 and 1==2) or 1<2)
print('ab' in ['a', 'c', 'ab'])
print('c' not in ['a', 'b'])
True
True
True
True
True
True
True
python
age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 25
else:
    price = 40
print(f"Your admission cost is ${price}.")
Your admission cost is $25.
python
# 5.3
alien_color = 'green'
if alien_color == 'green':
    print("You get five score!")
alien_color = 'yellow'
if alien_color == 'green':
    print("You get five score!")
You get five!
python
# 5.4
alien_color = 'green'
if alien_color == 'green':
    print("You get five score!")
else:
    print("You get ten score!")
alien_color = 'yellow'
if alien_color == 'green':
    print("You get five score!")
else:
    print("You get ten score!")
You get five!
You get ten!
python
# 5.5
alien_color = 'green'
if alien_color == 'green':
    print("You get five score!")
elif alien_color == 'yellow':
    print("You get ten score!")
else:
    print("You get fifteen score.")

alien_color = 'yellow'
if alien_color == 'green':
    print("You get five score!")
elif alien_color == 'yellow':
    print("You get ten score!")
else:
    print("You get fifteen score.")

alien_color = 'red'
if alien_color == 'green':
    print("You get five score!")
elif alien_color == 'yellow':
    print("You get ten score!")
else:
    print("You get fifteen score.")
You get five score!
You get ten score!
You get fifteen score.
python
# 5.6
age = 26
if age < 2:
    print("age <2")
elif age < 4:
    print("2<= age <4")
elif age < 13:
    print("4<= age <13")
elif age < 18:
    print("13<= age <18")
elif age < 65:
    print("18<= age <65")
else:
    print("65<= age")
18<= age <65
python
favorite_fruits = ['apple', 'orange','banana']
if "apple" in favorite_fruits:
    print("You really like apple!")
if 'banana' in favorite_fruits:
    print("You really like banana!")
if 'coco' in favorite_fruits:
    print("You really like coco!")
You really like apple!
You really like banana!
python
requested_toppings = []
if requested_toppings:
    for requested_topping in requested_toppings:
        print(f"Adding {requested_topping}.")
    print("\nFinished making your pizza!")
else:
    print("Are you sure you want a plain pizza?")
Are you sure you want a plain pizza?
python
# 5.8
names = ['lalala', 'xixixi', 'admin']
for name in names:
    if name == 'admin':
        print(f"Hello {name.title()}, would you like to see a status report?")
    else:
        print(f"Hello {name.title()}, thank you for logging in again.")
Hello Lalala, thank you for logging in again.
Hello Xixixi, thank you for logging in again.
Hello Admin, would you like to see a status report?
python
# 5.9
names = names = ['lalala', 'xixixi', 'admin']
if not names:
    print("1-We need to find some users!")
names.clear()
if not names:
    print("2-We need to find some users!")
2-We need to find some users!
python
# 5.10
current_users = ['lalala', 'xixixi', 'admin', 'lueluelue', 'heiheihei']
new_users = ['a', 'b', 'lalala', 'LueLueLue', 'admin']
for new_user in new_users:
    if new_user in current_users:
        print("Name already use!")
    else:
        print("No use!")
print('-'*100)
current_users2 = [name.lower() if isinstance(name, str) else name for name in current_users]
for new_user in new_users:
    if new_user.lower() in current_users2:
        print("Name already use!")
    else:
        print("No use!")
No use!
No use!
Name already use!
No use!
Name already use!
----------------------------------------------------------------------------------------------------
No use!
No use!
Name already use!
Name already use!
Name already use!
python
# 5.11
for i in range(1, 10):
    if i == 1:
        print(f"{i}st", end=' ')
    elif i == 2:
        print(f"{i}nd", end=' ')
    elif i == 3:
        print(f"{i}rd", end=' ')
    else:
        print(f"{i}th", end=' ')
1st 2nd 3rd 4th 5th 6th 7th 8th 9th