Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Video lecture, Collections
- print("Welcome!")
- # collection of products
- products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
- # you can just get a single variable by accessing a certain index
- test = products[2]
- print(test)
- # ask user for a integer
- choice = input("Which product would you like to see?\n")
- choice = int(choice)
- # check that the user gave a proper index (less than the amount of products)
- if choice < len(products) and choice >= 0:
- # or we can also ask this from the user and place it as index
- first = products[choice]
- print(first)
- else:
- print("Bad index! Product doesn't exist!")
- # NEW FILE
- products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
- # looping through all products and printing one name at a time
- for p in products:
- print(p)
- # NEW FILE
- products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
- # get the number of products
- amount = len(products)
- # process each product one at a time
- for index in range(amount):
- # index only contains the index number, not the value of the product
- # print(index)
- p = products[index]
- # print(p)
- # we can now combine both the index and the product!
- # here a numbered list from 1 to 4
- print(f"{index + 1}. {p}")
- # NEW FILE
- # a tuple, just like list, but can't be changed afterwards
- weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
- # tuples can't be changed afterwards (TypeError)
- # weekdays[2] = "Someday"
- # ask from user and reduce 1 so numbers and days match
- # for example 3 = Wednesday
- choice = input("Which day would you like to see?\n")
- choice = int(choice) - 1
- print(weekdays[choice])
- # NEW FILE
- # a dictionary is a "variable" containing
- # other variables related to each other
- # for example, one person
- person = {
- "name": "Test Person",
- "age": 31,
- "location": "Rovaniemi"
- }
- # getting data from dictionary
- print("Your name:")
- print(person["name"])
- print()
- # ... or the age, another way.
- person_age = person['age']
- print("Your age:")
- print(f"{person_age} years old.")
- # NEW FILE
- products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
- # changing a certain product with index
- products[1] = "CHANGED PRODUCT"
- print(products)
- # ask user for the index instead
- choice = input("Which product to change?\n")
- choice = int(choice)
- # ask user for the new product for the previous index
- new_product = input("What is the new product?\n")
- products[choice] = new_product
- print(products)
- # NEW FILE
- # three day's temperature data
- day_1 = [13.5, 12.8, 11.9, 6.8]
- day_2 = [11.6, 11.7, 8.4, 7.5]
- day_3 = [12.5, 9.5, 10.3, 8.1]
- # add all days into one list
- all_days = [day_1, day_2, day_3]
- # because we have a list of lists
- # we need a for-loop inside a for-oop
- # go through each day
- for day in all_days:
- print("NEW DAY!")
- # go through each temperature of one day at a time
- for temperature in day:
- print(temperature)
- # NEW FILE
- # dictionary in dictionary
- book = {
- "name": "My Lady Jane",
- "year": 2016,
- "publisher": {
- "name": "HarperTeen",
- "organization": "HarperCollins Publishers",
- "location": "New York"
- }
- }
- # getting the book name
- print(book['name'])
- # getting the publisher, then gettin the location of publisher
- publisher = book['publisher']
- print(publisher['location'])
- # we can also get everything with one code line
- print(book['publisher']['location'])
- # NEW FILE
- # string variable containing three pieces of data
- # separated by underscode
- # in one of the exercises, you can just put this
- # in a for-loop!
- text = "ORDER2327_C5162_2021"
- # split into a list by underscore
- parts = text.split("_")
- print(parts)
- # get the data into separate variables
- order = parts[0]
- client = parts[1]
- year = parts[2]
- print(order)
- print(client)
- print(year)
- # NEW FILE
- # append is useful, here we filter
- # short and long city names in two
- # seperate lists!
- # cities
- cities = ["rovaniemi", "oulu", "helsinki", "stockholm", "madrid", "oslo"]
- # intiialize "empty buckets" or lists for short and long city names
- short_city_names = []
- long_city_names = []
- # go through cities and filter them into short and long city names
- for city in cities:
- # less or equal to 6 characters long
- if len(city) <= 6:
- short_city_names.append(city)
- else:
- long_city_names.append(city)
- print(short_city_names)
- print(long_city_names)
- # NEW FILE
- # cities
- cities = ["rovaniemi", "helsinki", "stockholm", "oulu", "madrid", "oslo"]
- # break can be used to end the loop earlier
- # if some condition is met
- for city in cities:
- # less or equal to 6 characters long
- if len(city) <= 6:
- # short city name, quit the loop
- break
- else:
- # otherwise print city name
- print(city)
- # continue can be used to skip a turn if needed
- for city in cities:
- # less or equal to 6 characters long
- if len(city) <= 6:
- # short city name, skip this cycle
- continue
- else:
- # otherwise print city name
- print(city)
- # NEW FILE
- # sometimes using collections allow you to
- # use powerful collection functions
- # for example, average of numbers
- # notice, we don't need a loop this way!
- grades = [5, 7, 5, 6, 9, 8, 6, 10]
- # get sum + amount
- total = sum(grades)
- amount = len(grades)
- # average = total sum / number of elements
- average = total / amount
- print("The average is:")
- print(average)
- # NEW FILE
- cities = ["Rovaniemi", "helsinki", "London", "Madrid", "Rome"]
- # default way of sorting
- # doesn't work correctly if some words
- # start with capital and some with lowercase letters
- # sorted_cities = sorted(cities)
- # print(sorted_cities)
- # a trick by using a lambda function
- # before comparing cities to each other
- # change the city names CAPITAL
- cities.sort(key=lambda v: v.upper())
- print(cities)
Add Comment
Please, Sign In to add comment