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 list""" | |
"""string list""" | |
words = ["a", "b", "c", "d"] | |
print(f'merge string list is {",".join(words)}') | |
# merge string list is a,b,c,d | |
"""number list""" | |
number = [1, 2, 3, 4, 5, 6] | |
print(f'merge number list is {",".join(map(str, number))}') |
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""" | |
arr = [1, 2, 3, 4] | |
find_num = 10 | |
for num in arr: | |
if find_num == num: | |
break | |
else: | |
print(f'{find_num} is not exists in array') |
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 dictionary by key or value""" | |
fruits = dict() | |
fruits["apple"] = 3 | |
fruits["banana"] = 2 | |
fruits["mango"] = 5 | |
"""Sort by key""" | |
sort_key = sorted(fruits.items(), key=lambda f: f[0]) | |
print(f'Sort by key = {dict(sort_key)}') | |
# Sort by key = {'apple': 3, 'banana': 2, 'mango': 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
"""Get value from dictionary with default value""" | |
fruits = dict() | |
fruits["apple"] = 2 | |
fruits["banana"] = 3 | |
# {'apple': 2, 'banana': 3} | |
pear = fruits.get("pear", 3) | |
print(pear) | |
# 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
from copy import deepcopy | |
"""Copy or clone variable""" | |
a = [1, 2, 3] | |
b = deepcopy(a) | |
"""Try to add another data in a""" | |
a.append(4) | |
# a = [1, 2, 3, 4] | |
# b = [1, 2, 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
"""Chain function call""" | |
def sum_number(x: int, y: int) -> int: | |
return x + y | |
def sub_number(x: int, y: int) -> int: | |
return x - y |
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 = 5 | |
b = 10 | |
is_greater = False if a > b else True | |
# 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 comparison""" | |
a = 5 | |
b = 10 | |
find = 3 | |
middle = find < a < b | |
# True | |
found = find == 3 < a < 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
"""Transpose 2D List""" | |
arr = [["a", "b"], ["b", "c"], ["d", "e"]] | |
transpose = list(zip(*arr)) | |
print(f'Transpose list from {arr} to {transpose}') | |
# Transpose list from [['a', 'b'], ['b', 'c'], ['d', 'e']] to [('a', 'b', 'd'), ('b', 'c', 'e')] |
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
"""Reverse List""" | |
arr = ["1", "2", "b", "c", "d"] | |
reverse_arr_1 = arr[::-1] | |
# ['d', 'c', 'b', '2', '1'] | |
"""First way""" | |
# !! emosewa si nohtyp | |
"""Second way""" | |
reverse_arr_2 = list(reversed(arr)) | |
# ['d', 'c', 'b', '2', '1'] |