Python One-Liners

How to Get an HTML Page from a URL in Python?

This tutorial shows you how to perform simple HTTP get requests to get an HTML page from a given URL in Python! Problem Formulation Given a URL as a string. How to extract the HTML from the given URL and store the result in a Python string variable? Example: Say, you want to accomplish the …

How to Get an HTML Page from a URL in Python? Read More »

How to Replace One or More List Elements at Specific Indices in Python?

Problem Formulation: Replacing One Element Given List lst Element x Index i How to replace the element at index i in the list lst with the new element x? Solution Indexing You use simple indexing using the square bracket notation lst[i] = x to replace the element at index i in list lst with the …

How to Replace One or More List Elements at Specific Indices in Python? Read More »

How to Print an Integer with Commas as Thousands Separators in Python?

Problem Formulation: Given an integer number. How to convert the integer to a string representation for printing or other use that has thousand separators? Example: Given an integer number 1000000. You want the string representation ‘1,000,000’. Method 1: f-Strings Using the modern f-strings is, in my opinion, the most Pythonic solution to add commas as …

How to Print an Integer with Commas as Thousands Separators in Python? Read More »

How to Remove Everything After the Last Character in a String?

Problem Formulation Given string s, and character c. How to remove all characters in s after the first occurrence of c? Example Given: – string s = ‘hello world’, and – empty space character c = ‘ ‘. Desired result: ‘hello’ Method 1: string.index() + slicing To remove everything after the first occurrence of character …

How to Remove Everything After the Last Character in a String? Read More »

The Fibonacci Series in Python

The Fibonacci series was discovered by the Italian mathematician Leonardo Fibonacci in 1202 and even earlier by Indian mathematicians. The series appears in unexpected areas such as economics, mathematics, art, and nature. Algorithm Sketch In the following, we give a simple algorithm to calculate the Fibonacci numbers. The series starts with the Fibonacci numbers zero …

The Fibonacci Series in Python Read More »

Python exec() — A Hacker’s Guide to A Dangerous Function

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code. Have you ever wondered about the limits of a …

Python exec() — A Hacker’s Guide to A Dangerous Function Read More »

How to Convert an Integer List to a Float List in Python

The most pythonic way to convert a list of integers fs to a list of floats is to use the one-line fs = [float(x) for x in fs]. It iterates over all the elements in the list fs using list comprehension and converts each element of the list x to an integer value using the …

How to Convert an Integer List to a Float List in Python Read More »

Why Slicing With Index Out Of Range Works In Python?

Python slicing means to access a subsequence of a sequence type using the notation [start:end]. A little-known feature of slicing is that it has robust end indices. Slicing is robust even if the end index is greater than the maximal sequence index. The slice just takes all elements up to the maximal element. If the …

Why Slicing With Index Out Of Range Works In Python? Read More »

Python String Concatenation Without ‘+’

When I first saw this, I was sure it’s a bug. Well—it’s a feature! In today’s short article, you’ll learn about a small Python trick that I call “string concatenation without +”. Let’s start with some code! Plus vs. Adjacent String Literal Concatenation There are two ways to concatenate string literals in Python: Using the …

Python String Concatenation Without ‘+’ Read More »

Python sorted() Function

If you work in a data driven career, odds are you will at some point have to perform sorting on your data. Rather than writing your own sorting algorithm (which will most likely be far less efficient), Python provides a built-in function called sorted(). This function allows you to do basic sorting, such as arranging …

Python sorted() Function Read More »

Python Int to String with Leading Zeros

To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}’. The d flag in this expression defines that the result is a decimal value. The str(i).zfill(5) accomplishes the same string conversion of an integer with leading zeros. Challenge: Given an integer number. …

Python Int to String with Leading Zeros Read More »