From 13eeec9dd7f5abf48cf1d27510515aaafcec50d4 Mon Sep 17 00:00:00 2001 From: apanik Date: Wed, 6 Aug 2025 01:02:57 +0600 Subject: [PATCH] Add examples of handy Python tricks --- python_tricks.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 python_tricks.py diff --git a/python_tricks.py b/python_tricks.py new file mode 100644 index 0000000..7b43415 --- /dev/null +++ b/python_tricks.py @@ -0,0 +1,34 @@ +# A few handy Python tricks + + +def swap_variables(): + """Swap two variables without a temporary variable.""" + a, b = 1, 2 + a, b = b, a + return a, b + + +def unpack_iterable(): + """Unpack first, middle and last elements of a list.""" + numbers = [1, 2, 3, 4] + first, *middle, last = numbers + return first, middle, last + + +def merge_dictionaries(): + """Merge two dictionaries using the | operator.""" + a = {"x": 1} + b = {"y": 2} + return a | b + + +def list_comprehension(): + """Return squares of even numbers using list comprehension.""" + return [i * i for i in range(10) if i % 2 == 0] + + +if __name__ == "__main__": + print("swap_variables:", swap_variables()) + print("unpack_iterable:", unpack_iterable()) + print("merge_dictionaries:", merge_dictionaries()) + print("list_comprehension:", list_comprehension())