Login
Discover
Waves
Communities
Raidstead
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
wiseagent
Scrobble.life
2d
Promoted
MOVIE REVIEW: “Street Fighter” (2026)
This publication was also writen in SPANISH and PORTUGUESE. IMDb Synopsis: Ryu and Ken, two of the best fighters in the world, reunite when Chun-Li recruits them for the World Warrior Tournament. However,
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,
dovycola
Music
3d
Published via Ecency
Promoted
I’m on The Voice Kids Season 12 — I Still Can’t Believe It
Hey everyone, it’s Dovydas. I’ve been wanting to share this for a while, and now I finally can: I’m taking part in The Voice Kids season 12 — the blind auditions.
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
gunting
Freewriters
2d
Published via Ecency
Promoted
A Peaceful Morning in the Rice Fields 🌾
There is something special about walking through the rice fields in the morning. The fresh air, the warm sunlight, and the endless stretch of green and golden rice plants create a peaceful atmosphere that
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
zekepickleman
sublimesunday
7d
Promoted
Another Pickleman Adventure - Autumn Escape
The long winter up here makes summer premium outdoor activity. So much so that I refuse to stop adventuring summer style well into the fall. In this tale, I am challenging myself to share an immersive
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