Python HTML Get Parameter

Problem Formulation: How to perform an HTTP get call in Python? Solution: Use Python’s requests library. This is semantically equivalent to issuing an HTTP get call: http://example.com?param_1=value_1&param_2=value_2 In fact, you can obtain this exact URL by using the r.url attribute on the request object: You can find the text response by using the r.text attribute … Read more

How to Switch Keys and Values in a Python Dictionary?

Problem: Given a dictionary in Python; how to switch Keys and Values?  Method 1: Using a for loop   Method 2: Using the zip function Method 3: Using the map and reverse functions Method 4: Using a dictionary comprehension Summary: Use one of the following methods to switch between the keys and values in a dictionary with unique values. … Read more

How to Split a Byte String into Lines?

Problem Formulation: Given a byte string that contains new-line characters ‘\n’. How to split the byte string into a list of lines? Example: You want to transform the byte string b’your\nbyte\nstring’ into the list of byte strings [b’your’, b’byte’, b’string’] using b’\n’ as a newline separator. Given: b’your\nbyte\nstring’ Goal: [b’your’, b’byte’, b’string’] Solution: To split … Read more

How to Calculate Percentiles in Python

This article deals with calculating percentiles. Percentiles are statistical indicators that are used to describe specific portions of a sample population. The following sections will explain what percentiles are, what they are used for and how to calculate them, using Python. As you will see, Python allows solving this problem in multiple ways, either by … Read more

Python frozenset() — A Simple Guide with Video

Python’s built-in frozenset() function creates and returns a new frozenset object. A frozenset is an immutable set—so you cannot change the frozenset after creation and set methods such as add() or remove() don’t work on the frozenset. Without an argument, frozenset() returns an empty frozenset. With the optional argument, frozenset(iter) initializes the new frozenset with … 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 … Read more