How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Problem Formulation 🔏Problem: Given a string in Python; how to split the string and also keep the separators/delimiter? A sequence of one or more characters used to separate two or more parts of a given string or a … Read more

Python Keyboard Errors

When you’re learning and working with Python, you’re going to encounter errors and there’s no getting around that fact. So how do you get around them? It helps to learn about the errors. We’re going to take the opportunity to learn one of them today. What is “Keyboard Interrupt”? This is an error that you’re … Read more

Python One Line Class

Do you hate those lengthy class definitions with __init__ and too many whitespaces and newlines? Python One-Liners to the rescue! Luckily, you can create classes in a single line—and it can even be Pythonic to do so! Sounds too good to be true? Let’s dive right into it! Problem: How to create a Python class … Read more

Replacements For Switch Statement In Python?

Summary: Since switch-case statements are not a part of Python, you can use one of the following methods to create a switch-case construct in your code : Using a Python Dictionary Creating a Custom Switch Class Using if-elif-else Conditional Statements Using a Lambda Function Problem: Given a selection control switch case scenario in Python; how … Read more

Python Define Multiple Variables in One Line

In this article, you’ll learn about two variants of this problem. Assign multiple values to multiple variables Assign the same value to multiple variables Let’s have a quick overview of both in our interactive code shell: Exercise: Increase the number of variables to 3 and create a new one-liner! Let’s dive into the two subtopics … Read more

Python One Line Docstring

A docstring describes a module, function, class, or method in plain English to help other coders understand the meaning better. You must define the docstring at the beginning of the module, function, class, or method definition. By doing so, the docstring becomes the __doc__ special attribute of that object. You can access the docstring of … Read more

Python Control Flow Statements

Among the ingredients that make a programming language powerful are control flow statements. The Python for loop is one such control flow statement. The if statement is another one. In this tutorial, you’ll learn about both! Python For Loop The world around us is built around repetition. The sun goes up every morning and after … Read more

How To Resolve UnboundLocalError On Local Variable When Reassigned After The First Use?

Summary: To resolve an UnboundLocalError when the local variable is reassigned after the first use, you can either use the global keyword or the nonlocal keyword. The global keyword allows you to modify the values of a global variable from within a function’s local scope while the nonlocal keyword provides similar functionality in case of … Read more