Practicing Coding Interview Questions in Python
Kirill Smirnov
Data Science Consultant, Altran
Data Structure - a specialized format to organize and store data.
Main Data Structures in Python:
list - an ordered mutable sequence of items (e.g. numbers, strings etc.)
my_list = [1, 2, 3, 4, 5]
print(my_list)
[1, 2, 3, 4, 5]
my_list = [1, 2, 3, 4, 5]
print(my_list[2])
3
print(my_list[-1])
5
print(my_list[1:4])
[2, 3, 4]
print(my_list[2:])
[3, 4, 5]
my_list = [1, 2, 3, 4, 5]
my_list[2] = 30
print(my_list)
[1, 2, 30, 4, 5]
my_list[:2] = [10, 20]
print(my_list)
[10, 20, 30, 4, 5]
my_list = [10, 20, 30, 40, 50]
my_list.append(60)
print(my_list)
[10, 20, 30, 40, 50, 60]
my_list.remove(60)
print(my_list)
[10, 20, 30, 40, 50]
my_list = [10, 20, 30, 40, 50]
my_list.pop()
50
print(my_list)
[10, 20, 30, 40]
my_list.count(40)
1
tuple - an ordered immutable sequence of items (e.g. numbers, strings etc.)
my_tuple = (1, 'apple', 2, 'banana')
print(my_tuple)
(1, 'apple', 2, 'banana')
my_tuple = 1, 'apple', 2, 'banana'
print(my_tuple)
(1, 'apple', 2, 'banana')
Modifying items in a tuple is not possible.
my_tuple[0] = 10
TypeError
set - an unordered collection with no duplicate items (e.g. numbers, strings etc.)
my_set = set([1, 2, 3, 4, 5])
print(my_set)
{1, 2, 3, 4, 5}
my_set = set([1, 1, 1, 2, 3, 4, 5, 5, 5])
print(my_set)
{1, 2, 3, 4, 5}
my_set1 = set([1, 2, 3, 4, 5])
my_set2 = set([3, 4, 5, 6, 7])
my_set1.add(6)
print(my_set1)
{1, 2, 3, 4, 5, 6}
my_set1.remove(6)
print(my_set1)
{1, 2, 3, 4, 5}
my_set1.union(my_set2)
{1, 2, 3, 4, 5, 6, 7}
my_set1.intersection(my_set2)
{3, 4, 5}
my_set1.difference(my_set2)
{1, 2}
dictionary - a collection of key-value pairs where keys are unique and immutable
$\text{key} \rightarrow \text{value}$
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
print(fruits)
{'apple': 10, 'banana': 9, 'orange': 6}
fruits = dict([('apple', 10), ('orange', 6), ('banana', 9)])
print(fruits)
{'apple': 10, 'banana': 9, 'orange': 6}
Accessing a value for a key:
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits['apple']
10
fruits['grapefruit']
KeyError: 'grapefruit'
fruits['apple'] = 20
print(fruits)
{'apple': 20, 'orange': 6, 'banana': 9}
fruits['grapefruit'] = 11
print(fruits)
{'apple': 20, 'orange': 6, 'banana': 9, 'grapefruit': 11}
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits.items()
dict_items([('apple', 10), ('orange', 6), ('banana', 9)])
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
list(fruits.items())
[('apple', 10), ('orange', 6), ('banana', 9)]
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits.keys()
dict_keys(['apple', 'orange', 'banana'])
fruits.values()
dict_values([10, 6, 9])
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
list(fruits.keys())
['apple', 'orange', 'banana']
list(fruits.values())
[10, 6, 9]
fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits.popitem()
9
print(fruits)
{'apple': 10, 'orange': 6}
my_list = [1, 2, 3, 4, 5]
len(my_list)
5
my_tuple = (1, 2, 3, 4, 5)
len(my_tuple)
5
my_set = set([1, 2, 3, 4])
len(my_set)
4
my_dict = {'a': 1, 'b': 2, 'c': 3}
len(my_dict)
3
my_list = [1, 2, 3, 4, 5]
2 in my_list
True
my_tuple = (1, 2, 3, 4, 5)
2 in my_tuple
True
my_set = set([1, 2, 3, 4])
5 in my_set
False
my_dict = {'a': 1, 'b': 2, 'c': 3}
'b' in my_dict
True
Practicing Coding Interview Questions in Python