[🐍PyTricks]: Python list slice syntax fun
Source : Dan at Real Python <info@realpython.com>
# Python's list slice syntax can be used without indices # for a few fun and useful things: # You can clear all elements from a list: >>> lst = [1, 2, 3, 4, 5] >>> del lst[:] >>> lst [] # You can replace all elements of a list # without creating a new list object: >>> a = lst >>> lst[:] = [7, 8, 9] >>> lst [7, 8, 9] >>> a [7, 8, 9] >>> a is lst True # You can also create a (shallow) copy of a list: >>> b = lst[:] >>> b [7, 8, 9] >>> b is lst False
[🐍PyTricks]: Python 3.5+ type annotations
Source : Dan at Real Python <info@realpython.com>
# Python 3.5+ supports 'type annotations' that can be # used with tools like Mypy to write statically typed Python: def my_add(a: int, b: int) -> int: return a + b
[🐍PyTricks]: Python's list comprehensions are awesome
Source : Dan at Real Python <info@realpython.com>
# Python's list comprehensions are awesome. vals = [expression for value in collection if condition] # This is equivalent to: vals = [] for value in collection: if condition: vals.append(expression) # Example: >>> even_squares = [x * x for x in range(10) if not x % 2] >>> even_squares [0, 4, 16, 36, 64]
[🐍PyTricks]: Python's built-in HTTP server
Source : Dan at Real Python <info@realpython.com>
# Python has a HTTP server built into the # standard library. This is super handy for # previewing websites. # Python 3.x $ python3 -m http.server # Python 2.x $ python -m SimpleHTTPServer 8000 # (This will serve the current directory at # http://localhost:8000)
Dicts can be used to emulate switch/case statements
Source : Dan at Real Python <info@realpython.com>
# Because Python has first-class functions they can # be used to emulate switch/case statements def dispatch_if(operator, x, y): if operator == 'add': return x + y elif operator == 'sub': return x - y elif operator == 'mul': return x * y elif operator == 'div': return x / y else: return None
def dispatch_dict(operator, x, y): return { 'add': lambda: x + y, 'sub': lambda: x - y, 'mul': lambda: x * y, 'div': lambda: x / y, }.get(operator, lambda: None)() >>> dispatch_if('mul', 2, 8) 16 >>> dispatch_dict('mul', 2, 8) 16 >>> dispatch_if('unknown', 2, 8) None >>> dispatch_dict('unknown', 2, 8) None
Functions are first-class citizens in Python
Source : Dan at Real Python <info@realpython.com>
# Functions are first-class citizens in Python: # They can be passed as arguments to other functions, # returned as values from other functions, and # assigned to variables and stored in data structures. >>> def myfunc(a, b): ... return a + b ... >>> funcs = [myfunc] >>> funcs[0] <function myfunc at 0x107012230> >>> funcs[0](2, 3) 5
"is" vs "=="
Source : Dan at Real Python <info@realpython.com>
# "is" vs "==" >>> a = [1, 2, 3] >>> b = a >>> a is b True >>> a == b True >>> c = list(a) >>> a == c True >>> a is c False # • "is" expressions evaluate to True if two # variables point to the same object # • "==" evaluates to True if the objects # referred to by the variables are equal
Python's shorthand for in-place value swapping
Source : Dan at Real Python <info@realpython.com>
# Why Python Is Great: # In-place value swapping # Let's say we want to swap # the values of a and b... a = 23 b = 42 # The "classic" way to do it # with a temporary variable: tmp = a a = b b = tmp # Python also lets us # use this short-hand: a, b = b, a
Measure the execution time of small bits of Python code with the "timeit" modul
Source : Dan at Real Python <info@realpython.com>
# The "timeit" module lets you measure the execution # time of small bits of Python code >>> import timeit >>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) 0.3412662749997253 >>> timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000) 0.2996307989997149 >>> timeit.timeit('"-".join(map( str, range(100)))', number=10000) 0.24581470699922647
Subscribe to:
Posts (Atom)