Tuples and more Flashcards
What makes a tuple different from a list?
A tuple is immutable
Tuples are unchangeable and always in the same order
What do you need to add if you only have one item in a tuple?
A coma
tuple = (‘one’,)
What constructor do you use to make something into a tuple?
tuple()
What is an array?
linear data structure
There are 4 array like structures in python
Must be same data type to be an array technically.
Arrays are indexed
Show a way to change a tuple item for:
x = (‘apple’, ‘banana’, ‘cherry’)
y = list(x)
y[1] = ‘kiwi’
x = tuple(y)
Show a way to add orange to the below
x = (‘apple’, ‘banana’, ‘cherry’)
y = list(x)
y.append(‘orange’)
x = tuple(y)
Add two tuples together
thistuple = (“apple”, “banana”, “cherry”)
y = (“orange”,)
thistuple += y
What does packing mean
What does unpacking mean
ie: packing a tuple
assigning values to it
extracting values back into variables.
So if you have the list fruits (like below) and you put it’s contents into separate variables (like below) then you just unpacked the list.
fruits = (“apple”, “banana”, “cherry”)
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
Using the below tuple, place it’s elements into 3 variables, the last variable should contain the remaining elements in a list
tup = (‘apple’, ‘blueberry’, ‘grape’, ‘banana’)
(red, blue, *pellow) = tup
Using the below tuple, put the first and last element into a respective variable, throw the ones in the middle into their own variable
tup = (‘apple’, ‘blueberry’, ‘kiwi’, ‘grape’, ‘banana’)
(red, *fruit, yellow) = tup
Double all elements of the below tuple:
tup = (‘apple’, ‘blueberry’, ‘kiwi’, ‘grape’, ‘banana’)
mytup = tup * 2
Using the below tuple, show how many times ‘apple’ was used.
Show what index apple is in
tup = (‘apple’, ‘blueberry’, ‘kiwi’, ‘grape’, ‘apple’)
tup.count(‘apple’)
tup.index(‘apple’)
Name the four principles of a {set}
Unordered
Unchangeable
- you can’t change items, but you can add and remove.
Unindexed
No duplicates
What elements are considered exactly the same when placed in a set?
True/1
False/0
So if they’re both in a set together one will be deleted
Using the below set, add ‘orange’ to it
thisset = {“apple”, “banana”, “cherry”}
We don’t want ‘orange’ anymore… take it out NOW
thisset.add(‘orange’)
thisset.remove(‘orange’)
thisset.discard(‘orange’)
thisset.pop() <- might get it, might not
Add all the items from ‘tropical’ into ‘thisset’.
ALSO, what would it output if ‘tropical’ was a list?
thisset = {“apple”, “banana”, “cherry”}
tropical = {“pineapple”, “mango”, “papaya”}
thisset.update(tropical)
It would still produce a set
The set below is trash. Remove its contents
thisset = {“apple”, “banana”, “cherry”}
In fact I don’t want to see this set EVER again. Delete it…
thisset.clear()
del thisset
We’re going to be using two sets here and we have four different things we want to accomplish:
Join the sets together into set3, show how you would do more than one
Add set2 to set1
create a new set that only contains items that match from set1 and set2
Update set1 to only contain duplicates from it and set2
update set1 to only contain elements that aren’t present in set2
in a new set, keep only elements not present in both sets
Return a new set that contains only items from the first set that aren’t present in the second
update set1 to keep items that are not present in both set
set1 = {“a”, “b”, “c”}
set2 = {1, 2, 3}
set3 = set1.union(set2, set1)
or
set3 = set1 | set2
set1.update(set2)
set3 = set1.intersection(set2)
or
set3 = set1 & set2
set1.intersection_update(set2)
set3 = set1.difference(set2)
or
set3 = set1 - set2
set1.difference_update(set2)
set3 = set1.symmetric_difference()
or
set3 = set1 ^ set2
set1.symmetric_difference_update(set2)
Create a dictionary, and then pull the value for one of the keys. There’s two ways to get a value.
thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = thisdict[“model”]
x = thisdict.get(‘model)
get all the keys for thisdict
Next, get all the values
x = thisdict.keys()
x = thisdict.values()
Add a new key and value of
color = white to the below
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
car[‘color’] = ‘white’
or
car.update({‘color’: ‘white’})
Get the below dictionary in the form of a list of tuples. Each tuple will have a key and value
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = car.items()
Using an if statement, see if the key ‘model’ exists in the below dictionary
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
if ‘model’ in car:
print(‘yea’)
Change the value of the year below in two different ways
thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
thisdict[‘year’] = 2018
or
thisdict.update({‘year’: 2020})
or if you had dictionaries nested:
thisdict[client1].update({‘year’:2020})
If you didn’t want the ‘model’ key:value in the below dictionary, what would you do?
How do you remove a random item
remove the dictionaries contents
thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
thisdict.pop(‘model’)
or
del thisdict[‘model’]
thisdict.popitem()
thisdict.clear()
Loop through the keys of a dictionary
Now do it for the values
Loop through both keys and values
for x in thisdict:
print(x)
or
for x in thisdict.keys():
print(x)
for x in thisdict:
print(thisdict[x])
or
for x in thisdict.values():
print(x)
for x, y in thisdict.items():
print(x, y)
Copying a dictionary doesn’t work because the second dictionary will only be a reference to the first and if the first changes those changes apply to the new one.
how would you do a copy properly?
newdict = thisdict.copy()
or
newdict = dict(thisdict)
What is a nested dictionary?
If you have three dictionaries, and wanted to combine them into one, how would you do this
get the name of the child2 from your new aggregated dictionary
child1 = {
“name” : “Emil”,
“year” : 2004
}
child2 = {
“name” : “Tobias”,
“year” : 2007
}
child3 = {
“name” : “Linus”,
“year” : 2011
}
child1 = {
“name” : “Emil”,
“year” : 2004
}
child2 = {
“name” : “Tobias”,
“year” : 2007
}
child3 = {
“name” : “Linus”,
“year” : 2011
}
myfamily = {
“child1” : child1,
“child2” : child2,
“child3” : child3
}
print(myfamily[‘child2’][‘name’])