Data Types - General - Beginner Concepts Flashcards
Explain what an iterable is and list all the different types of iterables
It’s any object that you can loop over.
String
List
Tuple
Set
Dictionary
range(n)
Which of the below iterables can be sliced?
String
List
range(n)
All of these can be sliced
What data types can the for loop be used on
All iterables
E.g., for item in iterable:
What iterables can the “in” keyword be used on and why
All iterables - because if an object is iterable, Python can loop through it to check for membership — which is what “in” does
E.g., if x in iterable:
What iterables can sum() be used on
Any iterable provided all items are numeric…
… thus String is the only iterable that can never utilise sum()
What iterables can max() & min() be used on
All iterables
Though Dictionary operates on the keys
Items MUST be “comparable”
What iterables can len() be used on
All iterables
What iterables can sorted() be used on
All iterables
Items MUST be “comparable”
What iterables can join() be used on
All iterables - provided all items inside the iterable are strings
Join() takes a string seperator, and joins strings from an iterable - seperating thoes strings by that string seperator
What does it mean if an iterables is indexed
Each item in a collection is given a numeric position (an index number)
What does it mean if an iterable is ordered
Items stay in the order you added them
If an iterable is unordered, Python may reorder the items immediately upon creation or when operations are performed
Complete this table
Complete this table
What is the main difference between a List and a Tuple, and why might this make you prefer to use a Tuple instead of a List
A List is mutable.
A Tuple is immutable.
Use a tuple when your data should not changed “in place”
What does it mean if an iterable is mutable or immutable
Immutable means you cannot modify the object “in place”. To “change” it, you must create a modified copy and reassign it to the variable.
What is the MAIN difference between a List and a String
A list is a collection of any type of items.
A string is a collection of characters only.
How can you find the data type of an object
print(type(object))
x = 10
delete variable x from memory
del x
Can methods like .append(), .remove(), .pop() be used on both mutable & immutanbe iterables
No, only on mutable iterables.
With a String, for example, these won’t work even if you attempt to store the result in a variable.
Because these methods try to modify iterables in place
Can assignment operators like += & *= be used on both mutable & immutable iterables
Yes, but…
On mutable iterables → they change the object in place
On immutable iterables → they create and return a new object
iterable[2] = “d”
Does the above item assignment work on all iterables
No, it only works on iterables that are mutable and support indexing.
Hence, it only works on List & Dictionary (though with Dictionary, “2” is the key and “d” is the value)
del iterable[2]
Does the above deletion work on all iterables
No, it only works on iterables that are both mutable and indexable.
Hence, it only works on List & Dictionary (though with Dictionary, it deletes the key)
iterable.sort()
Does the above work on all iterables
No, List is the only object with the attribute ‘sort’
sorted(iterable)
Does the above work on all iterables
Yes, because it always returns a new object