diff --git a/README.md b/README.md index c6833f3..8128119 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,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" @@ -271,7 +271,7 @@ Makes sense, right? --- ### ▶ Splitsies ^ - + ```py >>> 'a'.split() ['a'] @@ -308,7 +308,7 @@ Makes sense, right? ### ▶ Time for some hash brownies! - + 1\. ```py some_dict = {} @@ -353,7 +353,7 @@ So, why is Python all over the place? --- ### ▶ The disorder within order ^ - + ```py from collections import OrderedDict @@ -451,7 +451,7 @@ What is going on here? --- ### ▶ Keep trying? * - + ```py def some_func(): try: @@ -511,7 +511,7 @@ Iteration 0 --- ### ▶ Deep down, we're all the same. * - + ```py class WTF: pass @@ -561,7 +561,7 @@ True --- ### ▶ For what? - + ```py some_string = "wtf" some_dict = {} @@ -614,7 +614,7 @@ for i, some_dict[i] in enumerate(some_string): --- ### ▶ Evaluation time discrepancy ^ - + 1\. ```py array = [1, 8, 15] @@ -680,7 +680,7 @@ array_4 = [400, 500, 600] --- ### ▶ Messing around with `is` operator^ - + The following is a very famous example present all over the internet. 1\. @@ -798,7 +798,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['', '', ''] @@ -843,7 +843,7 @@ We can avoid this scenario here by not using `row` variable to generate `board`. --- ### ▶ The sticky output function - + ```py funcs = [] results = [] @@ -897,7 +897,7 @@ Even when the values of `x` were different in every iteration prior to appending --- ### ▶ The chicken-egg problem ^ - + 1\. ```py >>> isinstance(3, int) @@ -945,7 +945,7 @@ False --- ### ▶ `is not ...` is not `is (not ...)` - + ```py >>> 'something' is not None True @@ -961,7 +961,7 @@ False --- ### ▶ The surprising comma - + **Output:** ```py >>> def f(x, y,): @@ -991,7 +991,7 @@ SyntaxError: invalid syntax --- ### ▶ Strings and the backslashes\ ^ - + **Output:** ```py >>> print("\"") @@ -1034,7 +1034,7 @@ True --- ### ▶ not knot! - + ```py x = True y = False @@ -1061,7 +1061,7 @@ SyntaxError: invalid syntax --- ### ▶ Half triple-quoted strings - + **Output:** ```py >>> print('wtfpython''') @@ -1086,7 +1086,7 @@ wtfpython --- ### ▶ Midnight time doesn't exist? - + ```py from datetime import datetime @@ -1116,7 +1116,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 @@ -1181,7 +1181,7 @@ for item in mixed_list: --- ### ▶ Class attributes and instance attributes - + 1\. ```py class A: @@ -1251,7 +1251,7 @@ True --- ### ▶ yielding None - + ```py some_iterable = ('a', 'b') @@ -1280,7 +1280,7 @@ def some_func(val): --- ### ▶ Mutating the immutable! - + ```py some_tuple = ("A", "tuple", "with", "values") another_tuple = ([1, 2], [3, 4], [5, 6]) @@ -1313,7 +1313,7 @@ But I thought tuples were immutable... --- ### ▶ The disappearing variable from outer scope - + ```py e = 7 try: @@ -1393,7 +1393,7 @@ NameError: name 'e' is not defined --- ### ▶ When True is actually False - + ```py True = False if True == False: @@ -1413,7 +1413,7 @@ I've lost faith in truth! --- ### ▶ Lossy zip of iterators - + ```py >>> numbers = list(range(7)) >>> numbers @@ -1462,7 +1462,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 = { @@ -1490,7 +1490,7 @@ Most methods that modify the items of sequence/mapping objects like `list.append --- ### ▶ Subclass relationships * - + **Output:** ```py >>> from collections import Hashable @@ -1514,7 +1514,7 @@ The Subclass relationships were expected to be transitive, right? (i.e., if `A` --- ### ▶ The mysterious key type conversion * - + ```py class SomeClass(str): pass @@ -1570,7 +1570,7 @@ str --- ### ▶ Let's see if you can guess this? - + ```py a, b = a[b] = {}, 5 ``` @@ -1631,7 +1631,7 @@ a, b = a[b] = {}, 5 ## Section: Appearances are deceptive! ### ▶ Skipping lines? - + **Output:** ```py >>> value = 11 @@ -1667,7 +1667,7 @@ The built-in `ord()` function returns a character's Unicode [code point](https:/ --- ### ▶ Teleportation * - + ```py import numpy as np @@ -1697,7 +1697,7 @@ Where's the Nobel Prize? --- ### ▶ Well, something is fishy... - + ```py def square(x): """ @@ -1742,7 +1742,7 @@ Shouldn't that be 100? ### ▶ Modifying a dictionary while iterating over it - + ```py x = {0: None} @@ -1777,7 +1777,7 @@ Yes, it runs for exactly **eight** times and stops. --- ### ▶ Stubborn `del` operation * - + ```py class SomeClass: def __del__(self): @@ -1820,7 +1820,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] @@ -1880,7 +1880,7 @@ Can you guess why the output is `[2, 4]`? --- ### ▶ Loop variables leaking out! - + 1\. ```py for x in range(7): @@ -1944,7 +1944,7 @@ print(x, ': x in global') --- ### ▶ Beware of default mutable arguments! - + ```py def some_func(default_arg=[]): default_arg.append("some_string") @@ -2001,7 +2001,7 @@ def some_func(default_arg=[]): --- ### ▶ Catching the Exceptions - + ```py some_list = [1, 2, 3] try: @@ -2076,7 +2076,7 @@ SyntaxError: invalid syntax --- ### ▶ Same operands, different story! - + 1\. ```py a = [1, 2, 3, 4] @@ -2118,7 +2118,7 @@ a += [5, 6, 7, 8] --- ### ▶ The out of scope variable - + ```py a = 1 def some_func(): @@ -2157,7 +2157,7 @@ UnboundLocalError: local variable 'a' referenced before assignment --- ### ▶ Be careful with chained operations - + ```py >>> (False == False) in [False] # makes sense False @@ -2202,7 +2202,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 @@ -2246,7 +2246,7 @@ class SomeClass: --- ### ▶ Needles in a Haystack ^ - + 1\. ```py x, y = (0, 1) if True else None, None @@ -2351,7 +2351,7 @@ b = "javascript" --- ### ▶ Wild imports - + ```py # File: module.py @@ -2413,7 +2413,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 @@ -2431,7 +2431,7 @@ Sshh.. It's a super secret. --- ### ▶ `goto`, but why? * - + ```py from goto import goto, label for i in range(9): @@ -2459,7 +2459,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 @@ -2484,7 +2484,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 @@ -2515,7 +2515,7 @@ There we go. --- ### ▶ Even Python understands that love is complicated * - + ```py import this ``` @@ -2572,7 +2572,7 @@ True --- ### ▶ Yes, it exists! - + **The `else` clause for loops.** One typical example might be: ```py @@ -2616,7 +2616,7 @@ Try block executed successfully... --- ### ▶ Ellipsis ^ - + ```py def some_func(): Ellipsis @@ -2676,7 +2676,7 @@ Ellipsis --- ### ▶ Inpinity * - + The spelling is intended. Please, don't submit a patch for this. **Output (Python 3.x):** @@ -2695,7 +2695,7 @@ The spelling is intended. Please, don't submit a patch for this. --- ### ▶ Let's mangle ^ - + 1\. ```py class Yo(object): @@ -2783,7 +2783,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) @@ -2799,7 +2799,7 @@ AttributeError: 'A' object has no attribute '__variable' --- ### ▶ Let's make a giant string! - + ```py def add_string_with_plus(iters): s = "" @@ -2892,7 +2892,7 @@ Let's increase the number of iterations by a factor of 10. --- ### ▶ Explicit typecast of strings - + ```py a = float('inf') b = float('nan') @@ -2931,7 +2931,7 @@ nan --- ### ▶ Minor Ones - + * `join()` is a string operation instead of list operation. (sort of counter-intuitive at first usage) **💡 Explanation:**