sololearn intermediate Python #1 Collection Types
Fill in the blanks to create a list dictionary and tuple:
# list list = ["one", "two"] # dictionary dict = {1:"one", 2:"two"} # tuple (tp = "one", "two")
What is the result of this code? tuple = (1 (1 2 3)) print(tuple[1])
(1, 2, 3)
What is the value of y after this code runs? x y = [1 2] x y = y x
1
letters = {"a" "b" "c" "d"} if "e" not in letters: print(1) else: print(2)
1
What is the output of this code? a b c d *e f g = range(20) print(len(e))
14
a = {1 2 3} b = {0 3 4 5} print(a & b)
3
What is the result of this code? fib = {1: 1 2: 1 3: 2 4: 3} print(fib.get(4 0) + fib.get(7 5))
8
nums = [i*2 for i in range(10)]
A list of even numbers between 0 and 18
Sets can be combined using mathematical operations.
The union operator | combines two sets to form a new one containing items in either. The intersection operator & gets items only in both. The difference operator - gets items in the first set but not in the second. The symmetric difference operator ^ gets items in either set, but not both. nums.remove (3) 1, 2, 3, 4, 5, 6} second = {4, 5, 6, 7, 8, 9} print(first I second) print(first & second) print(first - second)
tips
When to use a dictionary: - When you need a logical association between a key:value pair. - When you need fast lookup for your data, based on a custom key. - When your data is being constantly modified. Remember, dictionaries are mutable. When to use the other types: - Use lists if you have a collection of data that does not need random access. Try to choose lists when you need a simple, iterable collection that is modified frequently. - Use a set if you need uniqueness for the elements. - Use tuples when your data cannot/should not change. Many times, a tuple is used in combination with a dictionary, for example, a tuple might represent a key, because it's immutable.
A variable that is prefaced with an asterisk (*) takes all values from the collection that are left over from the other variables. example
a b, *C, d = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(a) print(b) print(c) print(d)
Tuple unpacking
allows you to assign each item in a collection to a variable. numbers = (1, 2, 3) a, b, c = numbers print(a) print(b) print(c)
List comprehensions
are a useful way of quickly creating lists whose contents obey a rule. #a list comprehension cubes = [is**3 for i in range(5)] print(cubes)
dictionaries
are another collection type and allow you to map arbitrary keys to values. Dictionaries can be indexed in the same way as lists, using square brackets containing keys. ages = { "Dave": 24, "Mary: 42, "John": 58 }
sets
are similar to lists or dictionaries. They are created using curly braces, and are unordered, which means that they can't be indexed. Due to the way they're stored, it's faster to check whether an item is part of a set using the in operator, rather than part of a list. num_set={1, 2, 3, 4, 5}
Tuples
are very similar to lists, except that they are immutable (they cannot be changed). Also, they are created using parentheses, rather than square brackets. words = ("spam", "eggs", "sausages")
A list comprehension can also contain an if statement to enforce a condition on values in the list.
evens=[i**2 for i in range(10) if i**2 % 2 == e] print(evens)
A useful dictionary function is
get. It does the same thing as indexing, but if the key is not found in the dictionary it returns another specified value instead. print (pairs .get ("orange ") )
Tuples can be created without the parentheses by just separating the values with commas.like
my_tuple = "one", "two", "three"
You can use the add() function to add new items to the set remove() to delete a specific element:
nums 1, 2, 1, 3, 1, 4, 5, 6} print(nums) nums.add(-7)
To determine whether a key is in a dictionary
you can use, in and not in print(1 in nums) print ("three " in nums) print(4 not in nums)
Which of these values can't be used as a dictionary key? {2: 4 3: 9} "one two three" False
{2: 4, 3: 9}