click below
click below
Normal Size Small Size show me how
Programming #4
From all lessons in "Organizing Data"
| Question | Answer |
|---|---|
| List | Ordered, Resizable, Mutable, Allows duplicates. myList = [1, 2] |
| Setting list elements | If the element exists in the list, it can be set using the assignment operator. Using an element in a list is just like using a variable |
| in / not in operator | Takes an element and a list and returns True if the element can be found in the list and False otherwise. Not in performs the opposite. 30 in numList |
| Iterating through a list using a for loop | for selectedFruit in fruit: print(selectedFruit) |
| Iterating through a list using a for loop and length | for i in range(len(fruit)): selectedFruit = fruit[i] | print(selectedFruit) |
| + | Concatenate two lists. [1, 2] + [3, 4] gives you [1, 2, 3, 4] |
| * | Repeats the elements of a list for a given number of times. [1, 2, 3]*3 gives you [1, 2, 3, 1, 2, 3, 1, 2, 3] |
| [] | Used to extract an element that is positioned at a given index in the given list (ex. list[index]). Positive indexes start from the front with 0 being the first index. Negative indexes start from the back with -1 being the last index |
| [:] | Allows the extraction of entire section of elements. [lower-bound: upper-bound]. Lower is inclusive, Upper is exclusive. Both bounds are optional. list[1:3] returns [b, c]. list[:2] returns [a, b] |
| [::] | Slicing with a specified step (skip value). [lower-bound : upper-bound : step]. Step is the number of elements to be skipped plus 1. Can be negative, meaning case elements are extracted in reverse order. [1: 9: 2] gives you [20, 40, 60, 80] |
| append() | Adds to the end of a list. name.append("Fred") |
| index() | Returns the first index where that value is. name.index("Jill") |
| extend() | Appends a list. name.extend(["Bob, "Jill"]) |
| insert() | Inserts at specified index. name.insert(0, "Mike") |
| pop() | Remove element at index. name.pop(1) |
| remove() | Removes first occurrence. name.remove("Bob") |
| sort() | Sorts list alphanumerically. name.sort() |
| Set | Unordered, No index, Fixed, no Duplicates, Resizable. fruit = {"apple", "banana"} |
| update(<collection>) | Adds a collection to the set |
| Joining sets using union | purchased = fruit1.union(fruit2) |
| Joining sets using update | fruit1.update(fruit2) |
| Intersecting sets | Get set of elements in first set that are also in second set. inBoth = fruit1.intersection(fruit2) |
| Updating a set with its intersection | fruit1.intersection_update(fruit2) |
| Difference in sets | Get set of elements from first that that is not in second set. unique = fruit1.difference(fruit2) |
| Updating a set with its differences | fruit1.difference_update(fruit2) |
| Tuple | Ordered, Fixed, allows Duplicates, not resizable. fruit = ("apple", "banana") |
| Updating a tuple | Convert it to a list, modify it, then convert it back to a tuple |
| Unpacking a tuple | Assigns each element to a corresponding variable. For example, a, b, c = ("apple", "banana", "cherry") assigns "apple" to a, "banana" to b, and "cherry" to c |
| index() | Returns the first index where that value is. name.index("Jill") |
| count() | Counts number of occurrences in the list. name.count("Mike") |
| Dictionaries | Ordered, Resizable. Elements(called items) are made of a key and a value. Fixed key and mutable values. Keys are unique, values can be duplicates. person = { "person1" : "Bob", "person2" : "Jill"} |
| Accessing dictionary items | Use key to access item's value. person[<key>] or person.get(<key>) |
| Get list of keys | person.keys() |
| Get list of values | person.values() |
| Change or add dictionary items | person[<key>] = <value> OR person.update({<key> : <value>}) |
| Dictionary for loops | for value in person: print(value) |
| update({<key>: <value>}) | Add or update an item |
| pop(<key>) | Remove element at index. name.pop(1) |
| items() | Returns a list of key values as tuples |
| Strings | Ordered, Indexed, Fixed, Not resizable |
| split([separator]) | Split string into list with separator |
| Characters | Strings with one element (no dedicated type). Encoded using ASCII or Unicode |
| strip() | Removes space from start and end |
| split(<separator>) | Split string into tuple on first occurrence of separator |
| splitlines() | Split string into list with new lines |
| replace(<old>, <new>) | Replaces all occurrences of the substring <old> with <new> in a string |
| partition(<separator>) | Split string into tuple on first occurrence of separator |