Lists

Contain data which can store multiple values

types_of_food = ["fruits", "dairy", "protein", "vegetables"]
print(types_of_food)
['fruits', 'dairy', 'protein', 'vegetables']

Dictionaries

Making a dictionary with the five most popular fruit and vegetables with their price in 2021

fruit = {"bananas": 0.62, "apples": 1.23, "grapes": 2.24, "strawberries": 1.37, "oranges": 1.45 }
vegs = {"potatoes": 0.78, "tomatoes": 1.91, "onions": 0.97, "carrots": 0.49, "bell peppers": 1.46}

food = dict()
food["fruit"] = fruit
food["vegs"] = vegs

for key, value in food.items():
    print(key)
    blank = ""
    for i in key:
        blank += ""
    for keys, values in value.items():
        print(blank, keys, values)
fruit
 bananas 0.62
 apples 1.23
 grapes 2.24
 strawberries 1.37
 oranges 1.45
vegs
 potatoes 0.78
 tomatoes 1.91
 onions 0.97
 carrots 0.49
 bell peppers 1.46

Loops

While Loop

Using a while loop to give a list of fruits and vegetables which have a price of less than $1.00

fruit = {"bananas": 0.62, "apples": 1.23, "grapes": 2.24, "strawberries": 1.37, "oranges": 1.45 }
vegs = {"potatoes": 0.78, "tomatoes": 1.91, "onions": 0.97, "carrots": 0.49, "bell peppers": 1.46}

food = dict()
food["main_fruit"] = fruit
food["main_vegs"] = vegs

for key, value in food.items():
    key=list(value.keys())
    i=0
    while(i<len(key)):
        price=value[key[i]]
        if(price<1.0):
            print(key[i], " ",price)
        i+=1
bananas   0.62
potatoes   0.78
onions   0.97
carrots   0.49

Recusion

Using recursion to calculate factorials

def fact(n):
    if n==1:
        return n
    else:
        return n*fact(n-1)
fact(7)
5040