Bring Your Logic using Python
341 subscribers
109 photos
1 video
4 files
544 links
Logic building using Python : Materials, links, e-books and Code snippets
Download Telegram
#Tip #Python

bin() and int() convert between a binary string (prefixed with "0b") and an integer number:

>>> bin(101)
'0b1100101'
>>> int('0b00011001', 2)
25


That could be useful for bitwise binary operators like AND, OR or XOR:

>>> a, b = 101, 25
>>> a & b
1
>>> a | b
125
>>> a ^ b
124
Did you know that you can give #Python's print an iterator and a separator?

So in this example you don't need the for loop
Elegant #Python: use * to unpack a sequence.
#Python functions are first-class objects so you can access their attributes like this.
The #Python requests library lets you conveniently choose if you want to raise an exception or not.
Did you know #Python's random module has a function called "randrange" that lets you choose a random item from range(start, stop[, step]) ?
In #Python you can chain comparison operators ...

Instead of writing "1 < a and a < 10" you can write "1 < a < 10", a bit more concise.
👴 #Python wisdom for the day: you can directly call methods on integers (remember, Python is fully object-oriented; no automatic boxing needed here 😉)
🐍Python Tip: To convert your strings to be URL friendly. For example, when constructing an API request/query strings, you can use urllib's quote method like so 👆🏻
#networkautomation #python #python3
Did you know that #Python's round() built-in accepts a negative number that lets you rounds up to the next 10, 100, 1000, ... ? 🐍🤯
#Python set operations are powerful, concise and elegant:
Download a YouTube video with #Python in 6 lines of code!

🐍🔥
Common #Python #interview question:
How to flatten a list of lists (several nested levels)?

Seems itertools.chain only goes one level deep, so let's use some recursion here.
#Python default argument values are evaluated once so mutability can bite you, use "None" instead.