Python Single vs Double Underscore (_ vs __)

The single underscore in Python “_” is used to either make a variable different from a Python keyword such as in float_=8, or to indicate that it should be used in a private context such as in _var=8. The double underscore in Python “__” (called “dunder“) is used to make an instance attribute or method … Read more

How to Check if the Current Time is in a Range in Python?

Problem Formulation Given a timestamp x such as the current time and a time interval marked with start and end timestamps. Goal: Create a function that checks whether the given timestamp falls in the interval [start, end], so that x>=start and x<=end. Constraints: If the interval is empty when start >= end and x != … Read more

Python Pass Statement

The pass statement does nothing when executed. You can use it as a placeholder for future code so that you can focus on the high-level structure first and implement the details later. For example, functions, loops, class definitions, or if statements require at least one statement in their indentation block. Think of the pass statement … Read more

How to Get the Size of a String in Python?

Problem Formulation: Given a string my_string. Find the number of characters of the string! For example, the string “what is my length?” must yield integer 18 because it has 18 characters. Here are three examples of our desired outputs: Solution: Before we dive into alternatives, here’s the quick answer to this question. To get the … Read more

[Google Interview] Find the k-th Largest Element in an Unsorted Array

Do you train for your upcoming coding interview? This question was asked by Google as reported in multiple occasions by programmers all around the world. Can you solve it optimally? Let’s dive into the problem first. Problem Formulation Given an integer array or Python list nums and an integer value k. Find and return the … Read more

How to Print a Percentage Value in Python?

To print a percentage value in Python, use the str.format() method or an f-string on the format language pattern “{:.0%}”. For example, the f-string f”{your_number:.0%}” will convert variable your_number to a percentage string with 0 digits precision. Simply run those three basic statements in your shell: your_number = 0.42 percentage = “{:.0%}”.format(your_number) print(percentage) As a … Read more

Python Class vs Instance Attributes [Tutorial+Video]

A Python class attribute is a variable that belongs to a class, not an individual instance, and is shared among all instances. An instance attribute is a variable that belongs to exactly one object. An instance attribute is defined inside the __init__() method by using the reference to the instance self, whereas a class attribute … Read more

How to Stop a While Loop in Python

Python provides three ways to stop a while loop: The while loop condition is checked once per iteration. If it evaluates to False, the program ends the loop and 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 … Read more

16 PDF Cheat Sheets for Programmers

A couple of years ago, I fell into the habit of creating cheat sheets when exploring certain areas in the programming space. Over time, hundreds of thousands of Finxters have downloaded and used them in their own learning journeys. However, the cheat sheets are largely scattered around many different locations on the Finxter ecosystem. And … Read more

Python Regex to Return String Between Parentheses

Problem Formulation Given a string s. How to find the substring s’ between an opening and a closing parentheses? Consider the following examples: Input: ‘Learn Python (not C++)’ Output: ‘not C++’ Input: ‘function(a, b, c, d)’ Output: ‘a, b, c, d’ Input: ‘(a+(b+c))’ Output: ‘a+(b+c)’ Method 1: Slicing and str.find() The simplest way to extract … Read more

Python Set Methods [Video Guide]

The set data structure is one of the most underused primary data structures in Python. I’ve seen so many intermediate-level coders who use lists when all they need is a data structure that checks membership!! ? Understanding the basics differentiates the good from the great. In this tutorial, you’ll learn about Python’s basic set methods. First, … Read more