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 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
"""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
"""Reverse list""" | |
a = [1, 2, 3, 4, 5] | |
reversed_list = a[::-1] | |
print("Reverse from {} to {}".format(a, reversed_list)) | |
# Reverse from [1, 2, 3, 4, 5] to [5, 4, 3, 2, 1] | |
"""Iterate over reversed list efficiency""" | |
for num in reversed(a): | |
print(num) |
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 string""" | |
a = "python" | |
reversed_words = a[::-1] | |
print("reversed {} to {}".format(a, reversed_words)) | |
# reversed python to nohtyp | |
"""Reverse number""" | |
b = 123456789 | |
reversed_num = str(b) | |
print("reversed {} to {}".format(b, reversed_num)) |
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 collections import Counter | |
"""Check if two words is anagram""" | |
a = "listen" | |
b = "silent" | |
is_anagrams = Counter(a) == Counter(b) | |
print("Is {} and {} is Anagrams ? {}".format(a, b, is_anagrams)) | |
# Is listen and silent is Anagrams ? 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
from collections import Counter | |
"""Find most frequent in list""" | |
arr = [1, 2, 3, 1, 2, 4, 6, 7, 4, 3 ,2, 3, 4, 5] | |
cnt = Counter(arr) | |
print("""Showing all frequent in list""") | |
print(cnt) | |
# Counter({2: 3, 3: 3, 4: 3, 1: 2, 6: 1, 7: 1, 5: 1}) | |
print("""Show only frequent in 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
"""Join list to single string""" | |
arr = ["Python", "is", "awesome", "and", "i", "love", "it"] | |
print(" ".join(arr)) | |
# Python is awesome and i love it |
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
"""Simple variable""" | |
a = 10 | |
print("Simple variable a = {}".format(a)) | |
# Simple variable a = 10 | |
"""Multiple variable""" | |
a, b = 10, True | |
print("Multiple variable a = {}, b = {}".format(a, b)) | |
# Multiple variable a = 10, b = True |