def func(a, b=[]): b.append(a) return b print(func(1)) print(func(2))
x = [1, 2, 3] y = x.copy()
a = [1, 2, 3] b = [4, 5] print([a[i] + b[i] for i in range(min(len(a), len(b)))])
import threading def worker(): print("Working...") for _ in range(3): thread = threading.Thread(target=worker) thread.start()
def f(a, b, *args, **kwargs): print(a, b, args, kwargs) f(1, 2, 3, 4, x=5, y=6)
from collections import Counter c = Counter(['a', 'b', 'c', 'a', 'b', 'b']) print(c.most_common(1))
try: while True: pass except KeyboardInterrupt: print("Interrupted!")
def f(x, y): return x + y result = (lambda x, y: x * y)(2, 3) print(result)
import os print(os.path.join('folder', 'file.txt'))
class Example: def __str__(self): return "str method" def __repr__(self): return "repr method" print(str(Example())) print(repr(Example()))
x = (1, 2, 3) y = [1, 2, 3] z = {'a': 1, 'b': 2}
import random print(random.randint(1, 10))
class MyClass: @staticmethod def my_static_method(): return "Hello, World!" print(MyClass.my_static_method())
matrix = [[1, 2], [3, 4], [5, 6]] flat = [num for row in matrix for num in row] print(flat)
def f(x=[]): x.append(1) return x print(f()) print(f())
class MyClass: pass obj = MyClass() print(isinstance(obj, MyClass))
with open('file.txt', 'w') as f: f.write('Hello, World!')
gen = (x ** 2 for x in range(3)) for i in gen: print(i)
class MyClass: def final_method(self): pass # Attempt to override class MySubClass(MyClass): def final_method(self): pass
def test(x): assert x > 0, "Value must be positive!" print(test(-1))