Skip to content

Instantly share code, notes, and snippets.

View FerdinaKusumah's full-sized avatar
🧑‍🚀
✌🏻

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
@FerdinaKusumah
FerdinaKusumah / merge_list.py
Created July 28, 2020 13:41
[Python] merge list
"""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))}')
@FerdinaKusumah
FerdinaKusumah / for_else.py
Created July 28, 2020 13:36
[Python] For Else
"""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')
@FerdinaKusumah
FerdinaKusumah / sort_dict.py
Created July 28, 2020 13:34
[Python] Sort Dictionary
"""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}
@FerdinaKusumah
FerdinaKusumah / dict_default_value.py
Created July 28, 2020 13:29
[Python] Get default value from dict
"""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
@FerdinaKusumah
FerdinaKusumah / clone_variable.py
Created July 28, 2020 13:26
[Python] Clone variable
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]
@FerdinaKusumah
FerdinaKusumah / chain_function_call.py
Created July 28, 2020 13:23
[Python] Chain function Call
"""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
@FerdinaKusumah
FerdinaKusumah / ternary_operator.py
Created July 28, 2020 13:20
[Python] Ternary operator
"""Ternary operator"""
a = 5
b = 10
is_greater = False if a > b else True
# True
@FerdinaKusumah
FerdinaKusumah / chain_comparison.py
Created July 28, 2020 13:17
[Python] Chain comparison
"""Chain comparison"""
a = 5
b = 10
find = 3
middle = find < a < b
# True
found = find == 3 < a < b
# True
@FerdinaKusumah
FerdinaKusumah / transpose_list.py
Created July 28, 2020 13:14
[Python] Transpose List
"""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')]
@FerdinaKusumah
FerdinaKusumah / reverse_list.py
Created July 28, 2020 13:11
[Python] Reverse List
"""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']