How to Stop a For Loop in Python

Python provides three ways to stop a for loop: The for loop ends naturally when all elements have been iterated over. After that, Python proceeds with the first statement after the loop construct. The keyword break terminates a loop immediately. The program proceeds with the first statement after the loop construct. The keyword continue terminates … Read more

3 Best Ways to Generate a Random Number with a Fixed Amount of Digits in Python

Coding Challenge ⚔️ Challenge: Given an integer d representing the number of digits. How to create a random number with d digits in Python? Here are three examples: my_random(2) generates 12 my_random(3) generates 389 my_random(10) generates 8943496710 I’ll discuss three interesting methods to accomplish this easily in Python—my personal favorite is Method 2! Shortest Solution … Read more

How to Check If a List of ENS (.eth) Domains are Available? (Python)

You can check programmatically in Python whether a certain ENS ‘.eth‘ domain name is available by using the urlopen() function from urllib.request module to access the URL ‘https://etherscan.io/enslookup-search?search=example.eth’, replacing the URL suffix ‘example.eth’ with your desired domain. In the following example code snippet, I show how you could check a list of names, one domain … Read more

If-Then-Else in One Line Python

Quick answer: How to put a simple if-then-else statement in one line of Python code? To put an if-then-else statement in one line, use Python’s ternary operator x if c else y. This returns the result of expression x if the Boolean condition c evaluates to True. Otherwise, the ternary operator returns the alternative expression … Read more

Python One Line Ternary

The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y. Ternary … Read more