Skip to content

Instantly share code, notes, and snippets.

@alwye
Created June 26, 2018 17:48
Show Gist options
  • Save alwye/fe178ef91afb12045666ff9bd448e5ea to your computer and use it in GitHub Desktop.
Save alwye/fe178ef91afb12045666ff9bd448e5ea to your computer and use it in GitHub Desktop.
'Conditions' was the subject of the module 4. Here's different examples of how you use them.
# you can create a list like this
fruits = [
'apple',
'grapes',
'banana'
]
favourite_fruit = "mango"
if favourite_fruit in fruits:
print(f"My favourite fruit {favourite_fruit} is on the list of fruits!")
else:
print(f"My favourite fruit {favourite_fruit} is missing!")
# create a Dictionary
# dictionary may include other dictionaries, lists and be very complex
person = {
'first_name': 'Alex',
'last_name': 'Zverev',
'age': 23,
'male': True # this is a boolean value: it can be either True or False
}
# access specific attributes of the Dictionary
print("Name: " + person['first_name'])
# this is how you inject
if person['male']:
print(f"{person['first_name']} is a man")
else:
print(f"{person['first_name']} is a woman")
# multiple conditions
if person['age'] <= 1:
print(f"{person['first_name']} is a baby")
elif 1 < person['age'] <= 3:
print(f"{person['first_name']} is a toddler")
elif 3 < person['age'] <= 5:
print(f"{person['first_name']} is at preschool stage")
elif 5 < person['age'] <= 12:
print(f"{person['first_name']} is a gradeschooler")
elif 12 < person['age'] <= 18:
print(f"{person['first_name']} is a teenager")
elif 18 < person['age'] <= 25:
print(f"{person['first_name']} is a young adult")
elif 25 < person['age'] <= 65:
print(f"{person['first_name']} is an adult")
elif 65 < person['age']:
print(f"{person['first_name']} is a senior")
else:
print("This will never be printed :p")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment