Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Classroom lecture, 4.11.2021, Collections!
- print("Welcome!")
- # NEW FILE
- products = ['Bosch washing machine',
- 'Philips coffee maker',
- 'Siemens freezer',
- 'Samsung smartphone']
- # only use this if just testing what's inside a collection
- # print(products)
- # ask user for index
- choice = input("Which product you want to see?\n")
- choice = int(choice)
- # only print the product if index is within acceptable range, in this case, 0-3
- if choice < len(products) and choice >= 0:
- p = products[choice]
- print(p)
- else:
- print("No product with given index.")
- # NEW FILE
- products = ['Bosch washing machine',
- 'Philips coffee maker',
- 'Siemens freezer',
- 'Samsung smartphone',
- 'LG 4K television']
- # looping through products
- for p in products:
- print(p)
- # NEW FILE
- products = ['Bosch washing machine',
- 'Philips coffee maker',
- 'Siemens freezer',
- 'Samsung smartphone',
- 'LG 4K television']
- amount = len(products)
- # numbered list with traditional loop
- for index in range(amount):
- p = products[index]
- print(f"{index + 1}. {p}")
- # NEW FILE
- # tuple is a list that can't be changed afterwards
- weekdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
- 'Friday', 'Saturday', 'Sunday')
- # ask the weekday from user
- # -1 fixes the problem with collections starting from 0!
- choice = input("Which weekday number?\n")
- choice = int(choice) - 1
- day = weekdays[choice]
- print(day)
- # NEW FILE
- # a person dictionary, key-value pairs
- # basically a variable containing multiple variables
- # that are related to each other
- person = {
- "name": "Dave Developer",
- "age": 32,
- "location": "Rovaniemi"
- }
- # printing snippets of info from the dictionary
- print(person['name'])
- print(f"Person's age: {person['age']} years old.")
- print()
- # if you need to loop through dictionary, it can be done like this
- for key in person:
- value = person[key]
- print(value)
- # NEW FILE
- products = ['Bosch washing machine',
- 'Philips coffee maker',
- 'Siemens freezer',
- 'Samsung smartphone',
- 'LG 4K television']
- # which index
- choice = input("Which product would you like to change?\n")
- choice = int(choice)
- # which new value
- new_product = input("What is the new product?\n")
- # replace the value in the collection
- products[choice] = new_product
- for p in products:
- print(p)
- print("done")
- # NEW FILE
- # list of lists
- day_1 = [20.0, 15.6, 17.8, 12.6, 11.4]
- day_2 = [21.0, 14.3, 17.5, 13.3, 15.3]
- day_3 = [19.0, 11.6, 14.9, 15.6, 14.9]
- temperatures = [day_1, day_2, day_3]
- #print(temperatures)
- counter = 1
- # because we have a list of lists
- # we need a for loop inside a for loop
- for day in temperatures:
- print(f"Day number {counter}!")
- counter += 1
- for temp in day:
- print(temp)
- print("done")
- # NEW FILE
- # the original code
- text = "ORDER142_A1567_2021"
- # split into a list
- parts = text.split('_')
- # save list parts into variables
- code = parts[0]
- client = parts[1]
- year = parts[2]
- # let's test
- print(code)
- print(client)
- print(year)
- # NEW FILE
- numbers = [5, 6, 7, 7, 8, 9, 3, 4, 5, 6, 7, 5]
- # average = total sum of values / amount of values
- amount = len(numbers)
- total = sum(numbers)
- # collections can be helpful like this
- average = total / amount
- print(average)
Add Comment
Please, Sign In to add comment