Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 6, 12.10.2023, collections more loops today!
- print("Welcome!")
- # NEW FILE
- # a list of products, 4 in total
- products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
- # this is more or less only for testing/debug purposes
- # only to be used by the coder
- # don't show this to actual user
- print(products)
- # NEW FILE
- # a list of products, 4 in total
- products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
- # this is more or less only for testing/debug purposes
- # only to be used by the coder
- # don't show this to actual user
- # print(products)
- # let's print the product in index 2 (which is the 3rd product)
- text = products[2]
- print(text)
- # NEW FILE
- # a list of products, 4 in total
- products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
- # ask user which product they want to see (index)
- choice = input("Which product do you wish to see?\n")
- choice = int(choice)
- # based on the choice-variable, show a product
- # so if user gives 2, this is same as products[2]
- text = products[choice]
- print(text)
- # NEW FILE
- # a list of products, 4 in total
- products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Toothbrush", "Microwave"]
- # ask user which product they want to see (index)
- choice = input("Which product do you wish to see?\n")
- choice = int(choice)
- # let's save the information of how many products
- # we have into a variable
- amount = len(products)
- # if user gave an index, that is less than the max amount of products
- if choice < amount and choice >= 0:
- # based on the choice-variable, show a product
- # so if user gives 2, this is same as products[2]
- text = products[choice]
- print(text)
- else:
- print("No product with given index.")
- # NEW FILE
- # a list of products, you can also put this in multiple lines
- products = ["Washing machine", "Coffee maker",
- "Freezer", "Fridge", "Toothbrush",
- "Microwave", "Bookshelf"]
- # use a for loop to go through all the data in the products list
- # starts with 1st element, ends with last element
- # this always works, regardless of number of products
- for p in products:
- print(p)
- # VERSION 2, on same line on one text variable
- # a list of products, you can also put this in multiple lines
- products = ["Washing machine", "Coffee maker",
- "Freezer", "Fridge", "Toothbrush",
- "Microwave", "Bookshelf"]
- text = ""
- # use a for loop to go through all the data in the products list
- # starts with 1st element, ends with last element
- # this always works, regardless of number of products
- for p in products:
- text = text + p + ", "
- print(text)
- # NEW FILE
- # a list of products, you can also put this in multiple lines
- products = ["Washing machine", "Coffee maker",
- "Freezer", "Fridge", "Toothbrush",
- "Microwave", "Bookshelf"]
- # how many products we have
- amount = len(products)
- # traditional for-loop with range
- # use the amount of products in the range
- # index contains the active index
- # p-variable: get a certain product with active index
- for index in range(amount):
- p = products[index]
- print(f"{index + 1}. {p}")
- # NEW FILE
- # let's create a tuple, notice, normal parentheses
- weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday",
- "Friday", "Saturday", "Sunday")
- # ask user for the index of the day to show
- choice = input("Which days would you like to see?\n")
- # this way we can easily match the user's number
- # to the actual index (lists start at 0, not 1)
- choice = int(choice) - 1
- # get the corresponding day and print it
- text = weekdays[choice]
- print(text)
- # NEW FILE
- # a list of products, you can also put this in multiple lines
- products = ["Washing machine", "Coffee maker",
- "Freezer", "Fridge", "Toothbrush",
- "Microwave", "Bookshelf"]
- # change the value of product in index 3 to Smartphone
- products[3] = "Smartphone"
- # we can now see that 4th product changed (index 3)
- print(products)
- # NEW FILE
- # a list of products, you can also put this in multiple lines
- products = ["Washing machine", "Coffee maker",
- "Freezer", "Fridge", "Toothbrush",
- "Microwave", "Bookshelf"]
- # ask the user for which product to change
- choice = input("Which product to change?\n")
- choice = int(choice)
- # ask the user what is the replacing product
- replacement = input("What is the replacement product?\n")
- # replace this product in this index
- # with the user's new value
- products[choice] = replacement
- # test if everything worked
- print(products)
- # NEW FILE
- # a single dictionary, containing
- # all information regarding this one person
- # in one "variable"
- person = {
- "name": "Test Person",
- "age": 33,
- "city": "Rovaniemi"
- }
- # use this only for testing purposes
- # this prints everything, even secrets or sensitive information
- # print(person)
- # printing from dictionary is done via the key
- # so to get the person's name, use the key "name"
- print("Person's name:")
- print(person["name"])
- print()
- print("Person's age:")
- print(person["age"])
- print()
- # NEW FILE
- # a single dictionary, containing
- # all information regarding this one person
- # in one "variable"
- person = {
- "name": "Test Person",
- "age": 33,
- "city": "Rovaniemi"
- }
- # use this only for testing purposes
- # this prints everything, even secrets or sensitive information
- # print(person)
- # printing from dictionary is done via the key
- # so to get the person's name, use the key "name"
- print("Person's name:")
- print(person["name"])
- print()
- print("Person's age:")
- print(person["age"])
- print()
- # NEW FILE
- # some product identifier
- codes = ["ORDER142_A1567_2023", "ORDER5678142_A15674242_2023"]
- # go through all codes
- # and split them into parts
- for code in codes:
- # split by underscore to a list
- parts = code.split("_")
- first = parts[0]
- second = parts[1]
- year = parts[2]
- print(first)
- print(second)
- print(year)
- print()
- print("ALL DONE")
- # NEW FILE
- # list of cities
- cities = ["rovaniemi", "oulu", "helsinki", "stockholm", "madrid", "oslo"]
- # make two empty lists, one for short city names, and another for long ones
- long_cities = []
- short_cities = []
- # loop through the cities, and place them
- # in either of the lists or "buckets" based on the
- # length of the city name
- for city in cities:
- if len(city) < 6:
- short_cities.append(city)
- else:
- long_cities.append(city)
- # let's test what is inside the lists now
- print(long_cities)
- print(short_cities)
- # NEW FILE
- # combining lists is very easy in Python
- foods = ["banana", "apple", "cherry"]
- drinks = ["water", "tea", "coffee"]
- # combine the two lists
- everything = foods + drinks
- # test out the values
- print(everything)
- # NEW FILE
- # list of grades
- # you can also create this list based on user inputs (for-loop)
- grades = [7, 8, 9, 9, 10, 7, 6, 8, 8, 10]
- # average = sum / count
- total = sum(grades)
- amount = len(grades)
- # calculate the average
- average = total / amount
- average = round(average, 1)
- print(average)
- # NEW FILE
- cities = ["Rovaniemi", "Helsinki", "Athens", "mikkeli", "Stockholm", "Madrid"]
- print("Original order:")
- print(cities)
- #cities.sort()
- cities = sorted(cities)
- print("Sorted order, cities.sort() / sorted(cities):")
- print(cities)
- # sort with lambda so, that all letters are converted to
- # uppercase when comparing them to each other
- cities.sort(key=lambda v:v.upper())
- print("Sorted with lambda:")
- print(cities)
Advertisement
Advertisement