Data Types - General - Beginner Concepts Flashcards

1
Q

Explain what an iterable is and list all the different types of iterables

A

It’s any object that you can loop over.

String
List
Tuple
Set
Dictionary
range(n)

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

Which of the below iterables can be sliced?

String
List
range(n)

A

All of these can be sliced

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

What data types can the for loop be used on

A

All iterables

E.g., for item in iterable:

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

What iterables can the “in” keyword be used on and why

A

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:

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

What iterables can sum() be used on

A

Any iterable provided all items are numeric…

… thus String is the only iterable that can never utilise sum()

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

What iterables can max() & min() be used on

A

All iterables

Though Dictionary operates on the keys

Items MUST be “comparable”

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

What iterables can len() be used on

A

All iterables

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

What iterables can sorted() be used on

A

All iterables

Items MUST be “comparable”

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

What iterables can join() be used on

A

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

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

What does it mean if an iterables is indexed

A

Each item in a collection is given a numeric position (an index number)

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

What does it mean if an iterable is ordered

A

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

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

Complete this table

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

Complete this table

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

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

A List is mutable.
A Tuple is immutable.

Use a tuple when your data should not changed “in place”

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

What does it mean if an iterable is mutable or immutable

A

Immutable means you cannot modify the object “in place”. To “change” it, you must create a modified copy and reassign it to the variable.

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

What is the MAIN difference between a List and a String

A

A list is a collection of any type of items.
A string is a collection of characters only.

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

How can you find the data type of an object

A

print(type(object))

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

x = 10

delete variable x from memory

A

del x

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

Can methods like .append(), .remove(), .pop() be used on both mutable & immutanbe iterables

A

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

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

Can assignment operators like += & *= be used on both mutable & immutable iterables

A

Yes, but…

On mutable iterables → they change the object in place

On immutable iterables → they create and return a new object

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

iterable[2] = “d”

Does the above item assignment work on all iterables

A

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)

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

del iterable[2]

Does the above deletion work on all iterables

A

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)

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

iterable.sort()

Does the above work on all iterables

A

No, List is the only object with the attribute ‘sort’

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

sorted(iterable)

Does the above work on all iterables

A

Yes, because it always returns a new object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What's the difference between sort() & sorted()
sort() is really .sort() & it's a method of the "List" iterable and can only be used on Lists - it modifies the List "in place". sorted() always returns a new object, hence can be used on any iterable.
26
len(iterable) Does the above work on all iterables
Yes
27
What is an easier way to do the below, num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
num_list = list(range(1, 21))
28
Give 2 effective methods of reversing the order of the List "lst"
lst[ : :-1] list(reversed(lst)) ## Footnote reversed() creates a "Reversed **Iterator**" - it does not return a list, hence is must be converted to, in this case, a List.
29
What is the difference between the below, lst[ : :-1] reversed(lst)
lst[ : :-1] returns a new reversed List reversed(lst) returns a "Reversed **Iterator**" - it does not create a new List unless you wrap it with list()
30
what is the main advantages of using, list(reversed(lst)) instead of , lst[ : :-1]
list(reversed(lst)) is more memory efficient, so usefull for large lists.
31
What is the difference between an "Iterator" & an "Iterable" (In layman's terms)
Iterables are collections of data - like String, List, Set, Tuple, Dictionary, range() An Iterator is like instructions for **how** to cycle through those items In a vending machine, the snacks are the **Iterables** and the retreival mechanism is the **Iterator**
32
Which is better & why result = "" for char in "String": result += char * 2 print(result) vs result = [] for char in "String": result.append(char * 2) print(''.join(result))
Using a List with .join() is quicker & more memory-efficient than a String with += ## Footnote Because Strings are immutable, += creates a new String for each iteration - which can become very inefficient with long Strings
33
result = "" for char in "String": result += char * 2 print(result) Without using a Generator Expression, write a script that does the above in a **quicker** & **more memory-efficient** way
result = [] for char in "String": result.append(char * 2) print("".join(result)) ## Footnote The original version (String Concatenation with +=) creates a new string object for every loop. The "List" method does not create a new object for every loop - instead .append adds new items to the list "in place".
34
for i in iterable: print(iterable[i]) What does the above print
TypeError This treats "i" like it's an index, but it actually refers to an item in the collection "iterable". If you want to print the Item, it should be typed as below, for i in iterable: print(i)
35
for i in iterable: print(i) For which iterables can you use the above
All Iterables
36
for i in iterable: print(i) The above returns the **Items** in the iterable. How should the above be modified to return the **Index** of the items.
for i in range(len(iterable)): print(i)
37
numbers = [0, 3, 4, 5] sqa_num = [] for num in numbers: sqa_num += num ** 2 What will the above return
TypeError In this case "+=" is attempting to "extend" the List with another **Iterable** You should use .append() instead to add a single number
38
a = 1 b = 2 c = 3 Modify the above assignments to be in a single line
a, b, c = 1, 2, 3
39
There a 2 variables a & b. Swap their assigned values without using a temporary variable
a, b = b, a
40
There are 3 vaiables, a, b & c In a single line, assign b to a, c to b & a to c
a, b, c = b, c, a
41
lst = [1, 2, 3] In a single line, re-arrange "lst" to the below [3, 1, 2]
lst[0], lst[1], lst[2] = lst[1], lst[2], lst[3]
42
a, b = divmod(10, 3) What does the above do
It divides 10 by 3 and assigns: 1) "a" the quotient → 10 // 3 = 3 2) "b" the remainder → 10 % 3 = 1
43
for i in range(len("abc")): print(i) What's a cleaner and more Pythonic way to write the above
for i, item in enumerate("abc"): print(i) ## Footnote Enumerate() actually retrieves both the index number and the item value, hence print(i, item) will print both
44
An easy way to check if **all** the characters in a string are digits
str.isdigit() | Returns True or False
45
"123".isdigit() What deos the above do
Checks if **ALL** characters in a String are digits | Returns True or False
46
An easy way to compare 2 Lists for similarity, returning True or False for each item
for a, b in zip(list1, list2): if a != b: return False
47
names = ['Link', 'Zelda', 'Ganon'] scores = [95, 87, 78] zipped = zip(names, scores) print(list(zipped)) What does the above print, and why does "zipped" need to first be converted to a List before printing
[('Link', 95), ('Zelda', 87), ('Ganon', 78)] It must first be converted to a List because zip() returns an "iterator", not an "iterable" ## Footnote zip() essentially pairs up elements from multiple sequences (e.g., lists, tuples, etc.) by their position — into tuples
48
char = ['a', 'b', 'c'] n = [1, 2, 3] Turn the above into... [('a', 1), ('b', 2), ('c', 3)]
result = list(zip(char, n)) ## Footnote zip() must first be converted to a List because zip() returns an "iterator", not an "iterable"
49
list1 = [1,2,3] list2 = [4,5,6] for a, b in zip(list1, list2): print(b) What does the above print
4 5 6
50
What does any() do
any() returns True if **any** element is truthy
51
for word in words: if "x" in word: return True return False Write the above using the any() function
return any("x" in word for word in words)
52
Return True if any score is greater than 50 in "scores" scores = [35, 42, 75]
if any(score > 50 for score in scores): Return True
53
Return True if any field is blank in "fields" fields = ["Alice", "", "email@example.com"]
if any(field == "" for field in fields): return True
54
Return True if any word from "words" is in "text" words = ["python", "code"] text = "I love to code."
return = any(word in text for word in words) print(return) ## Footnote The above is the "generator expression" of... for word in words: if word in text: print(True) break It might be easier to think of it in reverse "for word in words" - any() returns True if **any** "word in text" == True
55
What does all() do
all() returns True if **all** elements are truthy
56
for c in password: if c.isdigit() == False: return False return True Write the above using the all() function
return all(c.isdigit() for c in password)
57
Using the any() function, check if any number is even in "nums" nums = [1,2,3,4,5,6,7,8,9]
if any(num % 2 == 0 for num in nums): return True
58
Using the all() function, check if all numbers in "nums" are between 1 and 10 nums = [3,4,5,6,7]
if all(1 <= n <= 10 for n in nums) return True
59
text = "World" return reversed(text) Why does the above not work & how can you fix it
reversed(text) returns a reversed iterator, not a string You can convert the reversed iterator into a string using "".join(reversed(text)) str(reversed(text)) won't work because reversed(text) does not return the actual characters in reverse ## Footnote However, a more Pythonic way to reverse a string is simply to use text[ : :-1]
60
list(reversed(lst)) str(reversed(text)) Which of the above will work & why/why-not
reversed() returns an iterator str() does not loop through iterables. list() does! str() just converts the object itself to a string ## Footnote You can convert the reversed iterator into a string using "".join(reversed(text)) But a more Pythonic way to reverse a string is text[ : :-1]
61
What is a "Sequence"
An **Ordered** iterable ## Footnote E.g., **Includes;** String, List, Tuple, range() **Excludes;** Set, Dict
62
a = [1, 2, 3] b = a b.append(4) print(a) print(b) What does a & b print
a = [1, 2, 3, 4] b = [1, 2, 3, 4] ## Footnote Both the same, because **both point to the same List in memory** (b = a) and Lists are **mutable**. This is why mutability is important, because by amending "b", you are also amending "a", which could cause bugs in your code.
63
a = (1, 2, 3) b = a b.append(4) print(a) print(b) What does a & b print
Attribute Error ## Footnote Tupes are immutable, so cannot be amended in place
64
a = "hello" b = a b = b + " world" print(a) print(b) What does a & b print
a = hello b = hello world ## Footnote Because Strings are immutable, even though "b = a", "a" remains unchanged
65
Lists are mutable, Tuples are immutable. Is this the only difference?
*Mostly*, yes However, note that, for Tuples, this means that only the methods below are available, .count() .index()
66
a = (1, 2, 3) b = a b = b + (4,) print(a) print(b) What does a & b print
a = (1, 2, 3) b = (1, 2, 3, 4) ## Footnote Because Tuples are immutable, even though "b = a", "a" remains unchanged
67
a = [1, 2, 3] b = a b.append(4) If you want to copy "a" to "b" so you *can* amend "b" yet leave "a" untouched, the above will not work, since "b = a" means **both** a & b refer to the same place in memory, so changing one changes the other. Give 2 methods of "safely" making a "shallow" copy of a to b
b = a.copy() or b = a[ : ] ## Footnote The seconds method slices the entire list