Practicing Coding Interview Questions in Python
Kirill Smirnov
Data Science Consultant, Altran
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
nums_new = []
for i in range(1, 6):
nums_new.append(2*i)
print(nums_new)
[2, 4, 6, 8, 10]
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
for num in range(1, 6)
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
[ for num in range(1, 6)]
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
[(2 * num) for num in range(1, 6)]
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
nums_new = [(2 * num) for num in range(1, 6)]
print(nums_new)
[2, 4, 6, 8, 10]
List comprehension is defined by:
List comprehension is defined by:
[ for num in range(1, 6)]
List comprehension is defined by:
[(2 * num) for num in range(1, 6)]
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
1 2 3 4 5 6 7 8 9 10
nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
1 2 3 4 5 6 7 8 9 10 $\rightarrow$ 2 4 6 8 10
nums_new = [num for num in range(1, 11)]
print(nums_new)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums_new = [num for num in range(1, 11) if num % 2 == 0]
print(nums_new)
[2, 4, 6, 8, 10]
text = 'list COMPREHENSION is A way TO create LISTS'
Task:
Create a list that contains the length of each lowercased word.
list
, is
, way
, create
$\rightarrow$ [4, 2, 3, 6]
text = 'list COMPREHENSION is A way TO create LISTS'
Task:
Create a list that contains the length of each lowercased word.
list
, is
, way
, create
$\rightarrow$ [4, 2, 3, 6]
output = [ for word in text.split() ]
text = 'list COMPREHENSION is A way TO create LISTS'
Task:
Create a list that contains the length of each lowercased word.
list
, is
, way
, create
$\rightarrow$ [4, 2, 3, 6]
output = [ for word in text.split() if word.islower()]
text = 'list COMPREHENSION is A way TO create LISTS'
Task:
Create a list that contains the length of each lowercased word.
list
, is
, way
, create
$\rightarrow$ [4, 2, 3, 6]
output = [len(word) for word in text.split() if word.islower()]
print(output)
[4, 2, 3, 6]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
Create all the possible pairs between numbers
and letters
:
[
(1, 'a'), (1, 'b'), (1, 'c'),
(2, 'a'), (2, 'b'), (2, 'c'),
(3, 'a'), (3, 'b'), (3, 'c'),
]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [ for i in numbers ]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [ for i in numbers for j in letters]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers for j in letters]
print(pairs)
[
(1, 'a'), (1, 'b'), (1, 'c'),
(2, 'a'), (2, 'b'), (2, 'c'),
(3, 'a'), (3, 'b'), (3, 'c'),
]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers for j in letters]
pairs = []
for i in numbers:
for j in letters:
pairs.append((i, j))
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for j in letters]
pairs = []
for j in letters:
pairs.append((i, j))
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers ]
pairs = []
for i in numbers:
pairs.append((i, j))
pairs = [ (i, j) for i in numbers for j in letters]
pairs = [[(i, j) for i in numbers] for j in letters]
print(pairs)
[
[(1, 'a'), (2, 'a'), (3, 'a')],
[(1, 'b'), (2, 'b'), (3, 'b')],
[(1, 'c'), (2, 'c'), (3, 'c')]
]
pairs = [[(i, j) for i in numbers] for j in letters]
pairs = []
for j in letters:
temp = []
for i in numbers:
temp.append((i, j))
pairs.append(temp)
pairs = [[(i, j) for i in numbers] ]
pairs = []
temp = []
for i in numbers:
temp.append((i, j))
pairs.append(temp)
pairs = [[(i, j) ] for j in letters]
pairs = []
for j in letters:
temp = []
temp.append((i, j))
pairs.append(temp)
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [[(i, j) for i in numbers] for j in letters]
print(pairs)
[
[(1, 'a'), (2, 'a'), (3, 'a')],
[(1, 'b'), (2, 'b'), (3, 'b')],
[(1, 'c'), (2, 'c'), (3, 'c')]
]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [[(i, j) for i in letters] for j in numbers]
print(pairs)
[
[('a', 1), ('b', 1), ('c', 1)],
[('a', 2), ('b', 2), ('c', 2)],
[('a', 3), ('b', 3), ('c', 3)]
]
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers for j in letters]
pairs = [[(i, j) for i in numbers] for j in letters]
pairs = [[(i, j) for i in letters] for j in numbers]
Practicing Coding Interview Questions in Python