Algorithms

Keep It Simple, Stupid! Minimalism in Programming: How Complexity Harms Your Productivity

This article is based on a book chapter from my upcoming book “From One to Zero: A Minimalistic Approach to Programming”. My programming students often write in with their struggles and failures. Many students ultimately overcome their struggles—but a large percentage of them give up their programming ambitions after realizing how hard creating software can …

Keep It Simple, Stupid! Minimalism in Programming: How Complexity Harms Your Productivity Read More »

Yield Keyword in Python – A Simple Illustrated Guide

Introduction To yield In Python While using a function, we generally use the return keyword to return a value computed by the function. Similarly, the yield keyword also returns a value from a function, but it also maintains the state of the local variables inside the function and when the function is reused in the …

Yield Keyword in Python – A Simple Illustrated Guide Read More »

How To Eliminate All The Whitespace From A String?

In this article, you’ll learn the ultimate answer to the following question: How To Eliminate All The Whitespace From A String—on Both Ends, and In-Between Words? Summary: Use the string methods join(), split(), strip(), rstrip(), lstrip() and or replace()—in specific combinations—to remove any whitespace in a given string. The simplest way to remove all whitespaces …

How To Eliminate All The Whitespace From A String? Read More »

Python Reverse List with Slicing — An Illustrated Guide

Summary: The slice notation list[::-1] with default start and stop indices and negative step size -1 reverses a given list. Problem: Given a list of elements. How to reverse the order of the elements in the list. Example: Say, you’ve got the following list: Your goal is to reverse the elements to obtain the following …

Python Reverse List with Slicing — An Illustrated Guide Read More »

How to Remove Duplicates From a Python List While Preserving Order?

To remove duplicates from a Python list while preserving the order of the elements, use the code list(dict.fromkeys(list)) that goes through two phases: (1) Convert the list to a dict using the dict.fromkeys() function with the list elements as keys and None as dict values. (2) Convert the dictionary back to a list using the …

How to Remove Duplicates From a Python List While Preserving Order? Read More »

How to Get the Last Element of a Python List?

Problem: Given a list. How to access the last element of this list? Example: You have the list [‘Alice’, ‘Bob’, ‘Liz’] and you want to get the last element ‘Liz’. Quick solution: Use negative indexing -1. To access the last element of a Python list, use the indexing notation list[-1] with negative index -1 which …

How to Get the Last Element of a Python List? Read More »

List Changes After Assignment — How to Clone or Copy It?

Problem: If you assign a list object to a new variable using new_list = old_list, any modification to new_list changes old_list. What’s the reason for this and how can you clone or copy the list to prevent this problem? Example: Let’s consider the following example. Appending an element to the new_list also modifies the original …

List Changes After Assignment — How to Clone or Copy It? Read More »

How To Format A String That Contains Curly Braces In Python?

Summary: Use one of the following methods to format strings that contain curly braces: Use double curly braces {{}} Use the old string formatting, i.e. the % operator Use the JSON Library Use Template Strings Problem: Given a string literal with curly braces; how to format the string and ensure that the curly braces are …

How To Format A String That Contains Curly Braces In Python? Read More »

Accessing The Index Of Iterables In Python

Summary: To access the index of iterables like lists in Python, use one of the following methods: Use enumerate() function. Use a counter variable with For Loop/While Loop. Use a list comprehension. Use the NumPy library. Use the itertools module. Introduction An index can be considered as the position of an element in an ordered …

Accessing The Index Of Iterables In Python Read More »

Python One-Line Password Generator

Can you believe it? People use unknown and potentially insecure websites to generate their random passwords! This works as follows: A website generates a “random” password for them and they copy&paste it and assume this is a safe password because of the randomness of the characters. What a security flaw! Why? Because the website could …

Python One-Line Password Generator Read More »

String Formatting Comparison: format() | Percent | f-string

Summary: f-string is more readable and easier to implement than % and .format() string formatting styles. Furthermore, using f-strings is suggested for Python 3.6 and above while .format() is best suited for Python 2.6 and above. Versions prior to Python 2.6 only provide % option for string formatting. In terms of speed, % is the …

String Formatting Comparison: format() | Percent | f-string Read More »