Skip to content

Instantly share code, notes, and snippets.

@jeongukjae
Last active April 19, 2023 03:35
Show Gist options
  • Save jeongukjae/56dabac5ae2d4f0bd2b160bc463daada to your computer and use it in GitHub Desktop.
Save jeongukjae/56dabac5ae2d4f0bd2b160bc463daada to your computer and use it in GitHub Desktop.
Pythons snippet
list_var = [1, 2, 3]
# Same as list_var2 = list(map(lambda x: x+1, list_var))
list_var2 = [elem + 1 for elem in list_var]
# same as list_var3 = list(filter(lambda x: x!=1, list_var))
list_var3 = [elem for elem in list_var if elem != 1]
# Tuple, immutable, ordered
a = (1,2,3)
# List, mutable, ordered
b = [1,2,3]
# Set, unordered
c = {1,2,3}
# Dict, key-value pair
d = {"first": 1, "second": 2}
class A:
field1: str
def __init__(self, ...):
"""constructor"""
pass
def __del__(self):
"""Destructor"""
pass
def method1(self, ...):
"""definition of method"""
pass
def fn_raising_error_if_val_is_one(val: int):
"""This function will raise error if val is one
Args:
val: input value
Returns: None
Raises:
ValueError: if val is one
"""
if val == 1:
raise ValueError("input param val shouldn't be one")
try:
fn_raising_error_if_val_is_one(1)
except ValueError as e:
print("ValueError is raised")
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment