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: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with … Read more

How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”

If you run the following code, you’ll experience a special ValueError: The output will be this error message: Try It Yourself in our interactive browser shell: Solution: Using the Numpy functions called logical_and() and logical_or() instead of Python’s logical operators (“and” and “or”) is the solution. Master the basics and join the “Python Built-in Functions” … Read more

Python Comments — 2-Minute Guide with Exercise

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term! There are two types of comments: one-line comments and multi-line … 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

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

Hello World! A Python One-Liner to Get Started with Python Quickly

The “hello world program” is used in programming languages to set up a minimal programming environment and execute the first trivial program in it. It helps you get your environment going and take your first steps towards a more complicated program. This short tutorial will show you the fastest possible way to write your first … Read more