diff --git a/README.md b/README.md index 5b9977b..07f1a5c 100644 --- a/README.md +++ b/README.md @@ -223,7 +223,7 @@ Now, just run `wtfpython` at the command line which will open this collection in ## Section: Strain your brain! ### ▶ Strings can be tricky sometimes * - + 1\. ```py >>> a = "some_string" @@ -274,7 +274,7 @@ Makes sense, right? --- ### ▶ Splitsies ^ - + ```py >>> 'a'.split() ['a'] @@ -312,7 +312,7 @@ Makes sense, right? ### ▶ Time for some hash brownies! - + 1\. ```py some_dict = {} @@ -357,7 +357,7 @@ So, why is Python all over the place? --- ### ▶ The disorder within order ^ - + ```py from collections import OrderedDict @@ -455,7 +455,7 @@ What is going on here? --- ### ▶ Keep trying? * - + ```py def some_func(): try: @@ -515,7 +515,7 @@ Iteration 0 --- ### ▶ Deep down, we're all the same. * - + ```py class WTF: pass @@ -565,7 +565,7 @@ True --- ### ▶ For what? - + ```py some_string = "wtf" some_dict = {} @@ -618,7 +618,7 @@ for i, some_dict[i] in enumerate(some_string): --- ### ▶ Evaluation time discrepancy ^ - + 1\. ```py array = [1, 8, 15] @@ -684,7 +684,7 @@ array_4 = [400, 500, 600] --- ### ▶ Messing around with `is` operator^ - + The following is a very famous example present all over the internet. 1\. @@ -802,7 +802,7 @@ Similar optimization applies to other **immutable** objects like empty tuples as --- ### ▶ A tic-tac-toe where X wins in the first attempt! - + ```py # Let's initialize a row row = [""]*3 #row i['', '', ''] @@ -847,7 +847,7 @@ We can avoid this scenario here by not using `row` variable to generate `board`. --- ### ▶ The sticky output function - + ```py funcs = [] results = [] @@ -901,7 +901,7 @@ Even when the values of `x` were different in every iteration prior to appending --- ### ▶ The chicken-egg problem ^ - + 1\. ```py >>> isinstance(3, int) @@ -949,7 +949,7 @@ False --- ### ▶ `is not ...` is not `is (not ...)` - + ```py >>> 'something' is not None True @@ -965,7 +965,7 @@ False --- ### ▶ The surprising comma - + **Output:** ```py >>> def f(x, y,): @@ -995,7 +995,7 @@ SyntaxError: invalid syntax --- ### ▶ Strings and the backslashes\ ^ - + **Output:** ```py >>> print("\"") @@ -1038,7 +1038,7 @@ True --- ### ▶ not knot! - + ```py x = True y = False @@ -1065,7 +1065,7 @@ SyntaxError: invalid syntax --- ### ▶ Half triple-quoted strings - + **Output:** ```py >>> print('wtfpython''') @@ -1090,7 +1090,7 @@ wtfpython --- ### ▶ Midnight time doesn't exist? - + ```py from datetime import datetime @@ -1120,7 +1120,7 @@ Before Python 3.5, the boolean value for `datetime.time` object was considered t --- ### ▶ What's wrong with booleans? - + 1\. ```py # A simple example to count the number of booleans and @@ -1185,7 +1185,7 @@ for item in mixed_list: --- ### ▶ Class attributes and instance attributes - + 1\. ```py class A: @@ -1255,7 +1255,7 @@ True --- ### ▶ yielding None - + ```py some_iterable = ('a', 'b') @@ -1284,7 +1284,7 @@ def some_func(val): --- ### ▶ Mutating the immutable! - + ```py some_tuple = ("A", "tuple", "with", "values") another_tuple = ([1, 2], [3, 4], [5, 6]) @@ -1317,7 +1317,7 @@ But I thought tuples were immutable... --- ### ▶ The disappearing variable from outer scope - + ```py e = 7 try: @@ -1397,7 +1397,7 @@ NameError: name 'e' is not defined --- ### ▶ When True is actually False - + ```py True = False if True == False: @@ -1417,7 +1417,7 @@ I've lost faith in truth! --- ### ▶ Lossy zip of iterators - + ```py >>> numbers = list(range(7)) >>> numbers @@ -1466,7 +1466,7 @@ Where did element `3` go from the `numbers` list? --- ### ▶ From filled to None in one instruction... - + ```py some_list = [1, 2, 3] some_dict = { @@ -1494,7 +1494,7 @@ Most methods that modify the items of sequence/mapping objects like `list.append --- ### ▶ Subclass relationships * - + **Output:** ```py >>> from collections import Hashable @@ -1518,7 +1518,7 @@ The Subclass relationships were expected to be transitive, right? (i.e., if `A` --- ### ▶ The mysterious key type conversion * - + ```py class SomeClass(str): pass @@ -1574,7 +1574,7 @@ str --- ### ▶ Let's see if you can guess this? - + ```py a, b = a[b] = {}, 5 ``` @@ -1635,7 +1635,7 @@ a, b = a[b] = {}, 5 ## Section: Appearances are deceptive! ### ▶ Skipping lines? - + **Output:** ```py >>> value = 11 @@ -1671,7 +1671,7 @@ The built-in `ord()` function returns a character's Unicode [code point](https:/ --- ### ▶ Teleportation * - + ```py import numpy as np @@ -1701,7 +1701,7 @@ Where's the Nobel Prize? --- ### ▶ Well, something is fishy... - + ```py def square(x): """ @@ -1746,7 +1746,7 @@ Shouldn't that be 100? ### ▶ Modifying a dictionary while iterating over it - + ```py x = {0: None} @@ -1781,7 +1781,7 @@ Yes, it runs for exactly **eight** times and stops. --- ### ▶ Stubborn `del` operation * - + ```py class SomeClass: def __del__(self): @@ -1824,7 +1824,7 @@ Okay, now it's deleted :confused: --- ### ▶ Deleting a list item while iterating - + ```py list_1 = [1, 2, 3, 4] list_2 = [1, 2, 3, 4] @@ -1884,7 +1884,7 @@ Can you guess why the output is `[2, 4]`? --- ### ▶ Loop variables leaking out! - + 1\. ```py for x in range(7): @@ -1948,7 +1948,7 @@ print(x, ': x in global') --- ### ▶ Beware of default mutable arguments! - + ```py def some_func(default_arg=[]): default_arg.append("some_string") @@ -2005,7 +2005,7 @@ def some_func(default_arg=[]): --- ### ▶ Catching the Exceptions - + ```py some_list = [1, 2, 3] try: @@ -2080,7 +2080,7 @@ SyntaxError: invalid syntax --- ### ▶ Same operands, different story! - + 1\. ```py a = [1, 2, 3, 4] @@ -2122,7 +2122,7 @@ a += [5, 6, 7, 8] --- ### ▶ The out of scope variable - + ```py a = 1 def some_func(): @@ -2161,7 +2161,7 @@ UnboundLocalError: local variable 'a' referenced before assignment --- ### ▶ Be careful with chained operations - + ```py >>> (False == False) in [False] # makes sense False @@ -2206,7 +2206,7 @@ While such behavior might seem silly to you in the above examples, it's fantasti --- ### ▶ Name resolution ignoring class scope - + 1\. ```py x = 5 @@ -2250,7 +2250,7 @@ class SomeClass: --- ### ▶ Needles in a Haystack ^ - + 1\. ```py x, y = (0, 1) if True else None, None @@ -2415,7 +2415,7 @@ Same result, that didn't work either. --- ### ▶ Wild imports - + ```py # File: module.py @@ -2478,7 +2478,7 @@ NameError: name 'some_weird_name_func_' is not defined This section contains few of the lesser-known interesting things about Python that most beginners like me are unaware of (well, not anymore). ### ▶ Okay Python, Can you make me fly? * - + Well, here you go ```py @@ -2496,7 +2496,7 @@ Sshh.. It's a super secret. --- ### ▶ `goto`, but why? * - + ```py from goto import goto, label for i in range(9): @@ -2524,7 +2524,7 @@ Freedom! --- ### ▶ Brace yourself! * - + If you are one of the people who doesn't like using whitespace in Python to denote scopes, you can use the C-style {} by importing, ```py @@ -2549,7 +2549,7 @@ Braces? No way! If you think that's disappointing, use Java. Okay, another surpr --- ### ▶ Let's meet Friendly Language Uncle For Life ^ - + **Output (Python 3.x)** ```py >>> from __future__ import barry_as_FLUFL @@ -2580,7 +2580,7 @@ There we go. --- ### ▶ Even Python understands that love is complicated * - + ```py import this ``` @@ -2637,7 +2637,7 @@ True --- ### ▶ Yes, it exists! - + **The `else` clause for loops.** One typical example might be: ```py @@ -2681,7 +2681,7 @@ Try block executed successfully... --- ### ▶ Ellipsis ^ - + ```py def some_func(): Ellipsis @@ -2741,7 +2741,7 @@ Ellipsis --- ### ▶ Inpinity * - + The spelling is intended. Please, don't submit a patch for this. **Output (Python 3.x):** @@ -2760,7 +2760,7 @@ The spelling is intended. Please, don't submit a patch for this. --- ### ▶ Let's mangle ^ - + 1\. ```py class Yo(object): @@ -2848,7 +2848,7 @@ AttributeError: 'A' object has no attribute '__variable' ### ▶ `+=` is faster - + ```py # using "+", three strings: >>> timeit.timeit("s1 = s1 + s2 + s3", setup="s1 = ' ' * 100000; s2 = ' ' * 100000; s3 = ' ' * 100000", number=100) @@ -2864,7 +2864,7 @@ AttributeError: 'A' object has no attribute '__variable' --- ### ▶ Let's make a giant string! - + ```py def add_string_with_plus(iters): s = "" @@ -2957,7 +2957,7 @@ Let's increase the number of iterations by a factor of 10. --- ### ▶ Explicit typecast of strings - + ```py a = float('inf') b = float('nan') @@ -2996,7 +2996,7 @@ nan --- ### ▶ Minor Ones - + * `join()` is a string operation instead of list operation. (sort of counter-intuitive at first usage) **💡 Explanation:**