Tuples and more Flashcards

1
Q

What makes a tuple different from a list?

A

A tuple is immutable

Tuples are unchangeable and always in the same order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do you need to add if you only have one item in a tuple?

A

A coma

tuple = (‘one’,)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What constructor do you use to make something into a tuple?

A

tuple()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an array?

A

linear data structure
There are 4 array like structures in python
Must be same data type to be an array technically.

Arrays are indexed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Show a way to change a tuple item for:

x = (‘apple’, ‘banana’, ‘cherry’)

A

y = list(x)
y[1] = ‘kiwi’
x = tuple(y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show a way to add orange to the below
x = (‘apple’, ‘banana’, ‘cherry’)

A

y = list(x)
y.append(‘orange’)
x = tuple(y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Add two tuples together

A

thistuple = (“apple”, “banana”, “cherry”)
y = (“orange”,)
thistuple += y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does packing mean
What does unpacking mean

ie: packing a tuple

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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’)

A

(red, blue, *pellow) = tup

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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’)

A

(red, *fruit, yellow) = tup

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Double all elements of the below tuple:

tup = (‘apple’, ‘blueberry’, ‘kiwi’, ‘grape’, ‘banana’)

A

mytup = tup * 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using the below tuple, show how many times ‘apple’ was used.

Show what index apple is in

tup = (‘apple’, ‘blueberry’, ‘kiwi’, ‘grape’, ‘apple’)

A

tup.count(‘apple’)

tup.index(‘apple’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Name the four principles of a {set}

A

Unordered
Unchangeable
- you can’t change items, but you can add and remove.
Unindexed
No duplicates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What elements are considered exactly the same when placed in a set?

A

True/1
False/0
So if they’re both in a set together one will be deleted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Using the below set, add ‘orange’ to it
thisset = {“apple”, “banana”, “cherry”}

We don’t want ‘orange’ anymore… take it out NOW

A

thisset.add(‘orange’)

thisset.remove(‘orange’)
thisset.discard(‘orange’)
thisset.pop() <- might get it, might not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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”}

A

thisset.update(tropical)

It would still produce a set

17
Q

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…

A

thisset.clear()

del thisset

18
Q

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}

A

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)

19
Q

Create a dictionary, and then pull the value for one of the keys. There’s two ways to get a value.

A

thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = thisdict[“model”]

x = thisdict.get(‘model)

20
Q

get all the keys for thisdict

Next, get all the values

A

x = thisdict.keys()

x = thisdict.values()

21
Q

Add a new key and value of
color = white to the below

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

A

car[‘color’] = ‘white’

or
car.update({‘color’: ‘white’})

22
Q

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
}

A

x = car.items()

23
Q

Using an if statement, see if the key ‘model’ exists in the below dictionary

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

A

if ‘model’ in car:
print(‘yea’)

24
Q

Change the value of the year below in two different ways

thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

A

thisdict[‘year’] = 2018

or

thisdict.update({‘year’: 2020})

or if you had dictionaries nested:
thisdict[client1].update({‘year’:2020})

25
Q

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
}

A

thisdict.pop(‘model’)

or

del thisdict[‘model’]

thisdict.popitem()

thisdict.clear()

26
Q

Loop through the keys of a dictionary

Now do it for the values

Loop through both keys and values

A

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)

27
Q

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?

A

newdict = thisdict.copy()

or

newdict = dict(thisdict)

28
Q

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
}

A

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’])