""" ========================================= 🐍 Python Funny & Weird Tricks Collection ========================================= A collection of cursed, funny, and surprising Python behaviors. Made for GitHub 😈 """ print("\n==============================") print("1. THE ZEN OF PYTHON") print("==============================") import this print("\n==============================") print("2. HELLO SECRET MODULE") print("==============================") import __hello__ print("\n==============================") print("3. STRING MULTIPLICATION") print("==============================") print("LOL " * 5) print("\n==============================") print("4. WEIRD COMPARISONS") print("==============================") print("1 < 2 < 3 ->", 1 < 2 < 3) print("1 < 2 > 3 ->", 1 < 2 > 3) print("\nActually Python checks:") print("(1 < 2) and (2 > 3)") print("\n==============================") print("5. TUPLE CONFUSION") print("==============================") a = (1) b = (1,) print("(1) type ->", type(a)) print("(1,) type ->", type(b)) print("\n==============================") print("6. BOOLEAN MATH") print("==============================") print("True + True =", True + True) print("True * 10 =", True * 10) print("False + 5 =", False + 5) print("\nBecause:") print("True == 1") print("False == 0") print("\n==============================") print("7. LIST MULTIPLICATION CURSE") print("==============================") a = [[]] * 3 print("Before:", a) a[0].append(1) print("After :", a) print("\nWHY DID ALL LISTS CHANGE?! 😭") print("\n==============================") print("8. MUTABLE DEFAULT ARGUMENT") print("==============================") def add_item(item, my_list=[]): my_list.append(item) return my_list print(add_item(1)) print(add_item(2)) print(add_item(3)) print("\nSame list reused every function call 😈") print("\n==============================") print("9. FLOATING POINT PAIN") print("==============================") print("0.1 + 0.2 =", 0.1 + 0.2) print("\nExpected: 0.3") print("Reality :", 0.1 + 0.2) print("\n==============================") print("10. NaN IS WEIRD") print("==============================") x = float("nan") print("x == x ->", x == x) print("\nNaN is NOT equal to itself 🤯") print("\n==============================") print("11. EMPTY LIST IS FALSE") print("==============================") if []: print("This runs") else: print("Empty list is False") print("\n==============================") print("12. PYTHON SWAP MAGIC") print("==============================") a = 5 b = 10 print("Before swap:", a, b) a, b = b, a print("After swap :", a, b) print("\nNo temporary variable needed 😎") print("\n==============================") print("13. EVERYTHING IS OBJECT") print("==============================") print(type(10)) print(type("hello")) print(type(print)) print("\nEven functions are objects 👀") print("\n==============================") print("14. INFINITE LOOP (DON'T RUN)") print("==============================") print(""" while True: print("help") """) print("\n==============================") print("15. BRACES ARE NOT WELCOME") print("==============================") print(""" from __future__ import braces # SyntaxError: not a chance """) print("\n==============================") print("16. SET REMOVES DUPLICATES") print("==============================") nums = [1, 1, 1, 2, 2, 3] print("Original :", nums) print("Unique :", list(set(nums))) print("\n==============================") print("17. SAME MEMORY REFERENCE") print("==============================") x = y = [1, 2, 3] y.append(4) print("x =", x) print("y =", y) print("\nBoth variables point to SAME list 😭") print("\n==============================") print("18. PRINTING WITHOUT print()") print("==============================") import sys sys.stdout.write("Hello from sys.stdout.write\\n") print("\n==============================") print("19. GOODBYE") print("==============================") print("Python is beautiful...") print("Python is cursed...") print("Python is both 😌🐍")