Python Slice Assignment

Slice Assignment

Slice assignment is a little-used, beautiful Python feature to replace a slice with another sequence. Simply select the slice you want to replace on the left and the values to replace it on the right side of the equation. For example, the slice assignment list[2:4] = [42, 42] replaces the list elements with index 2 … Read more

Python One Line If Not None

To assign the result of a function get_value() to variable x if it is different from None, use the Walrus operator if tmp := get_value(): x = tmp within a single-line if block. The Walrus operator assigns the function’s return value to the variable tmp and returns it at the same time, so that you … Read more

Python List: Remove Duplicates and Keep the Order

Removing duplicates from a list is pretty simple. You can do it with a Python one-liner:Β  Python set elements have to be unique so converting a list into a set and back again achieves the desired result. What if the original order of the list is important though? That makes things a bit more complicated … 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

How to Create a Singleton in Python?

Master coders behave like architects that connect and build upon various design patterns to create a functional whole. One of the most important design patterns is a singleton—a class that has only one instance. You may ask: How does that look like? Let’s have a look at the code implementing a singleton in our interactive … 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

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