Waivio

pytricks

Experts

boyanpro

25.41
SteemPeak

Python Tricks #29 - Python's built-in HTTP server

Serve current folder by simply running python command. # Python has a HTTP server built into the # standard library. This is super handy ...

boyanpro

25.41
SteemPeak

Python Tricks #27 - f-strings Python3.6+

# f-strings are flexible way to do string interpolation available in Python 3.6+ # The old way with "format": user = "Jan...

boyanpro

25.41
SteemPeak

Python Tricks #26 - Your Dict Size

# Empty dict size. >>> d = {} >>> import sys >>> sys.getsizeof(d) 240 # It's the same size for first eight sl...

boyanpro

25.41
SteemPeak

Python Tricks #25 - Functions Superiority

# Functions are first-class citizens in Python: # They can be passed as arguments to other functions, # returned as values from other fu...

boyanpro

25.41
SteemPeak

Python Tricks #24 - Taking a string input

# For example "1 2 3 4" and return [1, 2, 3, 4] # Remember list being returned has integers in it. # Don't use more than one l...

boyanpro

25.41
Partiko

Python Tricks #23 - is vs ==

# "is" vs "==" >>> a = [1, 2, 3] >>> b = a >>> a is b True >>> a == b True >...

boyanpro

25.41
SteemPeak

Python Tricks #22 - Dict Tricks

# Inverting a dictionary using zip >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> m.items() [('a', 1), ('c', 3), ('b', 2)...

boyanpro

25.41
SteemPeak

Python Tricks #21 - Manipulating Lists

# Negative indexing >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> a[-1] 10 >>> a[-3] 8 # List slices (a[star...

boyanpro

25.41
SteemPeak

Python Tricks #20 - Unpacking

>>> a, b, c = 1, 2, 3 >>> a, b, c (1, 2, 3) >>> a, b, c = [1, 2, 3] >>> a, b, c (1, 2, 3) >>>...

boyanpro

25.41
SteemPeak

Python Tricks #19 - Oneliner to swap values between variables

>>> a=7 >>> b=5 >>> b, a =a, b >>> a 5 >>> b 7

boyanpro

25.41
SteemPeak

Python Tricks #18 - Store values of a list into new variables

>>> a = [1, 2, 3] >>> x, y, z = a >>> x 1 >>> y 2 >>> z 3

boyanpro

25.41
Steemit

Python Tricks #17 - Measure the execution time of small bits of Python code with the "timeit" module

# The "timeit" module lets you measure the execution # time of small bits of Python code >>> import timeit >>&g...

boyanpro

25.41
Steemit

Python Tricks #16 - All or Any

x = [True, True, False] if any(x): print("At least one True") if all(x): print("Not one False") if any(x) ...

boyanpro

25.41
Steemit

Python Tricks #15 - Convert list of list into single list

# import the itertools import itertools # Declaring the list geek geek = [[1, 2], [3, 4], [5, 6]] # chain.from_iterable() funct...

boyanpro

25.41
Steemit

Python Tricks #14 - Binary search is faster than linear

# Given list B = [2,5,7,8,9,11,14,16] find if 14 is present in this list or not. def binarySearch(ls,data): first = 0 last = len(l...

boyanpro

25.41
Busy

Python Tricks #13 - Find The Most Frequent Value In A List

# Most frequent element in a list >>> a = [1, 2, 3, 1, 2, 3, 2, 2, 4, 5, 1] >>> print(max(set(a), key = a.count)) 2 # ...

boyanpro

25.41
Busy

Python Tricks #12 - Pretty print dictionaries

# The standard string repr for dicts is hard to read: >>> my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee} >>> my_mapping...

boyanpro

25.41
Busy

Python Tricks #11 - Function argument unpacking

# Why Python Is Great: # Function argument unpacking def myfunc(x, y, z): print(x, y, z) tuple_vec = (1, 0, 1) dict_vec = {'x': 1, ...

boyanpro

25.41
Busy

Python Tricks #10 - Printing a list

# Declaring the list geek >>> geek = ['Geeks', 'Programming', 'Algorithm', 'Article'] # Directly printing the list >>&...

boyanpro

25.41
Busy

Python Tricks #9 - Python List Comprehension

# Python program to demonstrate list comprehension in Python # below list contains square of all odd numbers from # range 1 to 10 odd...

boyanpro

25.41
Partiko

Python Tricks #8 - import this

>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is bet...