diff --git a/Codecademy.md b/Codecademy.md index f515096..d2baeac 100644 --- a/Codecademy.md +++ b/Codecademy.md @@ -111,7 +111,7 @@ False = int(0) Force treating as a string: str(7) Force treating as an integer: int("7") -Froce treating as a float: float(7) +Force treating as a float: float(7) ## Check Datatypes @@ -263,6 +263,106 @@ You can also do this with multiple arguments: x_squared, y_squared = square_point(1, 3) ``` +# Lists + +You can put either strings, integers or other lists in a list, probably other things too. + +`heights = [['Jenny', 61], ['Alexus', 70], ['Sam', 67], ['Grace', 64]]` + +Use zip to combine elements from two lists: `zip(names, dogs_names)` would match dogs and their owners. + +But don't forget to turn it into a list before printing `print(list(zipvar))` + +You can append values to lists like this: + +``` +orders = ['daisies', 'periwinkle'] +orders.append('tulips') +``` + +## Ranges + +You can get a range of numbers using `range()` + +It will give you all numbers under the number you input. For example, `range(8)` would give you 0-7, `range(1, 8)` would give you 1-7, and `range(1, 8, 2)` would give you 1,3,5,7 (2 is the interval) Use `print(list(var))` when printing. + +## length + +Grab length with `len(list)` + +# Selecting List elements + + Use: `listname[indexnumber]` + + The index starts at 0. Grab the last in an index using `list[-1]` + +## Giving values to list elements + + `lst[index] = indexlst` + +## Cutting the middle of a list (ex3, functions + lists, ch.4) + + ``` +def remove_middle(lst, start, end): + end = end + 1 + return lst[:start] + lst[end:] + +print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3)) +``` +This prints `[4, 23, 42]` removing the index 1-3. + +# If something occurs more than N times, return True +``` +def more_than_n(lst, item, n): +if lst.count(item) > n: + return True +else: + return False + + print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3)) +``` + +### Sublists + +Grab a subset of a list using `sublist = letters[1:6]` This would give you index **1-5**. + +You can also do `[:5]` for all up to index 4, and `[5:]` for all after index 5. And, you can do `[-3:]` for the last 3 in an index. + +## Counting frequency of elements in a list. + + `var.count('stringtofind')` + +## Sorting strings alphabetically + + ``` + var.sort() + print(var) + ``` + +or use + +``` +sortedvar = sorted(var) +``` + +to produce a new list with sorted contents without changing the original variable. + +## Sorting strings based on an element in a sublist + + Replace 1 with the index you want to sort by. + + `pizzas.sort(key=lambda x: x[1])`` + +# Catch Errors + + ``` + try: + command_may_cause_error + except NameOfError: + print("Nice little message to user.") + ``` + + # Fun Projects Design a shop using Ex7 and Ex9 as a frame: diff --git a/Codecadmey Projects/lens-pizza.py b/Codecadmey Projects/lens-pizza.py new file mode 100644 index 0000000..f3396c6 --- /dev/null +++ b/Codecadmey Projects/lens-pizza.py @@ -0,0 +1,14 @@ +toppings = ['pepperoni', 'pineapple', 'cheese', 'sausage', 'olives', 'anchovies', 'mushrooms'] +prices = [2, 6, 1, 3, 2, 7, 2] +num_pizzas = len(toppings) +print("We sell " + str(num_pizzas) + " kinds of pizza!") +pizzas = list(zip(toppings, prices)) + +pizzas.sort(key=lambda x: x[1]) +cheapest_pizza = pizzas[0] +priciest_pizza = pizzas[-1] +three_cheapest = pizzas[0:3] +print(pizzas) +print(three_cheapest) +num_two_dollar_slices = prices.count(2) +print(num_two_dollar_slices)