Python Worksheet For Effective Coding
Python Worksheet For Effective Coding
by Alex Kelin
**entity could be
variable = [***result*** for element in entity if
Concept range(), list, any
condition]
iterable value…
String call of the names = [‘Bob’, ‘Mike’, ‘John’, ‘Jerry’] >>> print(new_list)
first char new_list = [ x[0] for x in names] ['B', 'M', 'J', ‘J’]
a = [5,1,6]
b = [3,2,4]
>>> print(united)
Unite two lists united = [ x for y in [a, b] for x in y]
[5, 1, 6, 3, 2, 4]
or
united = [x for x in a + b]
one = ['Jack', 'Brit', 'Lucas', 'Ben']
two = [10, 15, 4, 6] >>> print(nl)
Create nested list nl = [[name, age] for name, age in zip(one, two)] [['Jack', 10], ['Brit',
15], ['Lucas', 4],
or ['Ben', 6]]
nl = [[one[i], two[i]] for i in range(len(one))]
a = [5, 1, 6]
Sum integers two >>> print(new)
b = [3, 2, 4]
lists [8, 3, 10]
new = [x + y for x, y in zip(a, b)]
Conditional
one = [1, 2, 3, 4, 5, 6, 7] >>> print(new)
comprehension I,
new = [x if x % 2 == 0 else x * 2 for x in one] [2, 2, 6, 4, 10, 6, 14]
ternery operator
>>> print(check)
names = ['Bob', 'Mike', 'John', 'Jerry']
Check for value I [False, False, True,
check = [x == 'John' for x in names]
False]
one = [1, 2, 3, 4, 5]
two = ['a', 'b', 'c', 'd', ‘e'] >>> print(hm)
Merge two lists hm = dict([(one[i], two[i]) for i in range(len(one))]) {1: 'a', 2: 'b', 3:
or 'c', 4: 'd', 5: 'e'}
hm = {one[i]: two[i] for i in range(len(one))}
one = [1, 2, 3, 4, 5]
two = ['a', 'b', 'c', 'd', 'e']
>>> print(hm)
Merge two lists hm = {key: value for (key, value) in zip(one, two)}
with zip() or {1: 'a', 2: 'b', 3:
'c', 4: 'd', 5: 'e'}
hm = dict(zip(one, two))
or
hm = {a: b for a, b in zip(one, two)}
>>> print(united)
one = {1: 'a', 2: 'b', 3: 'c'} {1: 'a', 2: 'b', 3:
two = {4: 'd', 5: 'e', 6: 'f'} 'c', 4: 'd', 5: 'e',
6: ‘f'}
united = {**one, **two} >>> print(one)
or {1: 'a', 2: 'b', 3:
Merge dicts
one.update(two) 'c', 4: 'd', 5: 'e',
6: ‘f’}
or
>>> print(united)
one.update(two)
{1: 'a', 2: 'b', 3:
united = one 'c', 4: 'd', 5: 'e',
6: ‘f’}
one = [4, 1, 2, 2, 3, 1]
two = ['a', 'a', 'c', 'c', 'e']
>>> print(result)
Unique values only new_two = []
(order preserved) {4: 'a', 1: 'c', 2:
uv = [new_two.append(i) for i in two if i not in 'e'}
new_two]
result = {i: j for i, j in zip(one, new_two)}
Conditional
comprehension I a = {'mike': 10, 'jack': 32, 'rachel': 55} >>> print(a)
new_dict = {k: v for (k, v) in a.items() if v % 2 == {'mike': 10, 'jack':
0} 32}
names = {'jack': 38, 'tina': 48, 'ron': 57, 'john': >>> print(new_dict)
Conditional 33} {'jack': 'young',
comprehension II, new_dict = {x: ('old' if y > 40 else ‘young') for (x, 'tina': 'old',
ternery operator y) in names.items()} ‘Ron': 'old', 'john':
'young'}
>>> print(res)
{'a': {1: 'a', 2: 'a',
keys =['a', 'b', 'c', 'd'] 3: 'a'}, 'b': {1: 'b',
Nested dictionary
comprehension I values = [1, 2, 3] 2: 'b', 3: 'b'}, 'c':
res = {k1: {k2: k1 for k2 in values} for k1 in keys} {1: ‘c', 2: 'c', 3:
'c'}, 'd': {1: 'd', 2:
'd', 3: 'd'}}
>>> print(counted)
Find length of names = ['Alex', 'Tom', 'Johnson', 'Bi', 'Foobar'] {'alex': 4, 'tom': 3,
variable counted = {x.lower(): len(x) for x in names if x} 'johnson': 7, 'bi': 2,
'foobar': 6}
Python lambda λ : functions
by Alex Kelin
check_num = (
>>> print(check_num(6))
Check single value lambda x: f'{x} is greater than 5' if x > 5
6 is greater than 5
else f'{x} is not greater than 5')
check_num = (
>>> print(check_num(2,3))
Check multiple lambda x, y: f'{x} and {y} are greater than 5' if
2 and 3 are not greater than
values x > 5 and y > 5
5
else f'{x} and {y} are not greater than 5')
>>> print(a)
[<function
lib = [3, 1, 2]
<listcomp>.<lambda> at
a = [lambda x=_: x + 1 for _ in lib]
0x104761a80>, ……]
b = [(lambda x: x * 2)(_()) for _ in a]
>>> print(b)
[8, 4, 6]
>>> print(c)
lib = [3, 1, 2, 4]
Operations with [3, 1, 2, 4]
c = list(map(lambda x: x, lib))
list >>> print(d)
d = list(map(lambda x: x / 2, lib))
[1.5, 0.5, 1.0, 2.0]
>>> print(e)
lib = ['Bob', 'Mike', 'John', 'Jerry']
[' Hi, Bob', ' Hi, Mike', ' Hi,
e = list(map(lambda x: f' Hi, {x}', lib))
John', ' Hi, Jerry']