Login
Discover
Waves
Communities
Write
Login
Signup
Topics
#
pytricks
Explore topics
#pytricks
Global
Trending
Hot
New
Payout
Muted
Promoted
#pytricks
New
#pytricks
Follow to get notified about new posts with this tag
Get #pytricks by email
Follow
boyanpro
programming
7y
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 for # previewing websites. # Python 3.x $ python3 -m http.server
$ 0.041
10
1
1
boyanpro
programming
7y
Python Tricks #28 - Use dicts as switch/case
If you ever wondered how to do switch in Python, here is your answer. # Because Python has first-class functions they can # be used to emulate switch/case statements def dispatch_if(operator, x, y): if
$ 0.132
6
1
boyanpro
programming
7y
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 = "Jane Doe" action = "buy" log_message = 'User {} has logged
boyanpro
programming
7y
Python Tricks #26 - Your Dict Size
# Empty dict size. >>> d = {} >>> import sys >>> sys.getsizeof(d) 240 # It's the same size for first eight slots for key-value pairs. # sys.getsizeof returns size of the data
jongolson
The Kingdom
3d
Promoted
The Paragraph Style KJV - Review
Note: This is a repurposed video from my #bible review channel on YouTube. I always upload this content to the blockchain to make sure it has a home forever on chain! It's what exactly? I never knew this
boyanpro
programming
7y
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 functions, and # assigned to variables and stored in data structures.
boyanpro
programming
7y
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 line of code. >>> result = map(lambda x:int(x)
boyanpro
programming
7y
Python Tricks #23 - is vs ==
# "is" vs "==" >>> a = [1, 2, 3] >>> b = a >>> a is b True >>> a == b True >>> c = list(a) >>> a == c True >>> a is c
boyanpro
programming
7y
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), ('d', 4)] >>> zip(m.values(), m.keys()) [(1, 'a'), (3,
aleximprovement
Hive Book Club
20h
Published via Ecency
Promoted
Glucose Revolution 🥗 Book review ENG/PL
Witam i zapraszam! :) Dzisiaj przychodzę z recenzją książki, która ostatnio zawróciła mi w głowie. Mowa o Glukozowej rewolucji - streszczając, można powiedzieć, że to poradnik o zdrowym odżywianiu, ale
boyanpro
programming
7y
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[start:end]) >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
boyanpro
programming
7y
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) >>> a, b, c = (2 * i + 1 for i in range(3)) >>> a, b, c (1,
boyanpro
programming
7y
Python Tricks #19 - Oneliner to swap values between variables
>>> a=7 >>> b=5 >>> b, a =a, b >>> a 5 >>> b 7
boyanpro
programming
7y
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
darth-azrael
photography
18h
Published via Ecency
Promoted
Vintage Photos - Lot 6 (445-448)
All of the photos in this set were taken in the late 1960s. They were all likely taken in the Lansing, Michigan area.
boyanpro
programming
7y
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 >>> timeit.timeit('"-".join(str(n) for n in range(100))',
boyanpro
programming
7y
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) and not all(x): print("At least one True and one False")
boyanpro
programming
7y
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() function returns the elements of nested list # and iterate from first list of iterable
boyanpro
programming
7y
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(ls)-1 while first<=last: mid = (first+last)//2 if ls[mid] == data: return
cagolistic
Town Square
2d
Published via Ecency
Promoted
What Makes Elixir Intresting To Me Beyond The Price
Why ELIXIR Overview/Remarks I have closely followed the development and growth of Elixir tokens over the past 3 months. What interests me most is the flexibility, consistent updates, the community’s candid
boyanpro
programming
7y
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 # Using Counter from collections >>> from collections import
boyanpro
programming
7y
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 {'b': 42, 'c': 12648430. 'a': 23} # 😞 # The "json" module
boyanpro
programming
7y
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, 'y': 0, 'z': 1} >>> myfunc(*tuple_vec) 1, 0, 1 >>>
boyanpro
programming
7y
Python Tricks #10 - Printing a list
# Declaring the list geek >>> geek = ['Geeks', 'Programming', 'Algorithm', 'Article'] # Directly printing the list >>> print ("Simple List:", geek) Simple List: ['Geeks',
Older →
Discover communities
Explore communities
Create your community