This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Transpose 2d list""" | |
a = [["a", "b"], ["c", "d"], ["e", "f"]] | |
transposed = zip(*a) | |
to_list = list(transposed) | |
print("Transpose list from {} to {}".format(a, to_list)) | |
# Transpose list from [['a', 'b'], ['c', 'd'], ['e', 'f']] to [('a','c', 'e'), ('b', 'd', 'f')] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Chain comparison""" | |
min_num = 5 | |
max_num = 10 | |
search = 3 | |
is_middle = search < min_num < max_num | |
is_name = search == 3 < min_num < max_num | |
print("is number {} in middle {} and {} ? {}".format(search, min_num, max_num, is_middle)) | |
# is number 3 in middle 5 and 10 ? True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Ternary operator""" | |
a = 15 | |
b = 12 | |
is_a_greater = False if b > a else True | |
print("Is a is greater than b ? {}".format(is_a_greater)) | |
# Is a is greater than b ? True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Chain function call""" | |
def sum_product(a, b): | |
return a + b | |
def reduce_product(a, b): | |
return a - b | |
need_to_sum = True | |
a = 10 | |
b = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from copy import deepcopy | |
"""Clone or copy object to new ones""" | |
a = [1, 2, 3, 4] | |
b = deepcopy(a) | |
"""Try to adding new value in list a""" | |
a.append(5) | |
"""Now print this value""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Get dictionary value and set default value when not exists""" | |
a = dict() | |
a['apple'] = 10 | |
a['banana'] = 12 | |
print(a) | |
# {'apple': 10, 'banana': 12} | |
"""Print print wheneather not exists value and set default value""" | |
print(a.get('pear', 5)) | |
# 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Sort dict by keys or values""" | |
a = dict() | |
a['banana'] = 5 | |
a['apple'] = 2 | |
a['orange'] = 3 | |
"""Before sort""" | |
print("Before sort = {}".format(a)) | |
# Before sort = {'banana': 5, 'apple': 2, 'orange': 3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""For else""" | |
a = [1, 2, 3, 4] | |
# Search number in list | |
search_num = 10 | |
for num in a: | |
if num > search_num: | |
break | |
else: | |
print("Sorry but number you're trying to find is not found") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""List string""" | |
a = ["a", "b", "c", "d"] | |
string_separated = ",".join(a) | |
print("Join string from list to {}".format(string_separated)) | |
# Join string from list to a,b,c,d | |
"""List Number""" | |
b = [1, 2, 3, 4, 5] | |
number_separated = ",".join(map(str, b)) | |
print("Join number from list to {}".format(number_separated)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Merge dict""" | |
a = {"apple": 10} | |
b = {"banana": 3} | |
example1 = {**a, **b} | |
print("Merge dict 1 to {}".format(example1)) | |
# Merge dict 1 to {'apple': 10, 'banana': 3} | |
example2 = dict(a.items() | b.items()) | |
print("Merge dict 2 to {}".format(example2)) |