String Slicing in Python

String slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(string), step=1). For example, the … Read more

Arbitrary Argument Lists in Python

An arbitrary argument list is a Python feature to call a function with an arbitrary number of arguments. It’s based on the asterisk “unpacking” operator *. To catch an arbitrary number of function arguments in a tuple args, use the asterisk syntax *args within your function definition. For example, the function def f(*args): … allows … Read more

How to Assign a Function to a Variable in Python?

Challenge: Given is function f. How to assign the function to variable g, so that you can call g() and it runs function f()? Your desired output is function f‘s output: How to accomplish this in the most Pythonic way? Overview: We examine two methods to accomplish this challenge. You can run them in our … Read more

Recursive Power Function: Are You Stuck With This Sololearn Code?

If you learn Python with the excellent Sololearn app, you may find yourself with this code snippet: What’s the output of this code snippet? And, most importantly, how does it work? This short guide will tell you! The code creates a function that returns x^y. It leverages the important programming concept of recursion: it calls … Read more

Python Modulo

 When I studied computer science, the professors pushed us to learn the theory behind modulo operations and residual classes. But many of us lacked motivation. We could not see why calculating the remainder of the division, i.e., modulo, is such an important concept. Yet, many practical code projects later, I have gained the experience that … Read more

Why Slicing With Index Out Of Range Works In Python?

Python slicing means to access a subsequence of a sequence type using the notation [start:end]. A little-known feature of slicing is that it has robust end indices. Slicing is robust even if the end index is greater than the maximal sequence index. The slice just takes all elements up to the maximal element. If the … Read more

Python String Concatenation Without ‘+’

When I first saw this, I was sure it’s a bug. Well—it’s a feature! In today’s short article, you’ll learn about a small Python trick that I call “string concatenation without +”. Let’s start with some code! Plus vs. Adjacent String Literal Concatenation There are two ways to concatenate string literals in Python: Using the … Read more

String Concatenation – An Interactive Guide

Python has powerful built-in capabilities for string manipulation. That’s why web companies like Google love Python—it’s a perfect fit for the text-based web. This guide shows you how to use string concatenation operators using multiple forms of education: Text Puzzle Video Exercise Ready to learn string concatenation? Let’s get started! A Textual Introduction to String … Read more

Python Slicing Bootscamp

Slicing is one of the most popular Python features. Thus, understanding slicing is key to understand existing code bases. In this tutorial, I’m going to train your slicing skills. Ready? So, let’s go! ? Watch the Video Python Slicing Read About Slicing Slicing is a Python-specific concept for accessing a range of values in sequence … 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