Python Tuple

Python tuple() — A Simple Guide with Video

Python’s built-in tuple() function creates and returns a new tuple object. When used without an argument, it returns an empty tuple. When used with the optional iterable argument, it initializes the new tuple with the elements in the iterable. Read more about tuples in our full tutorial about Python Tuples. Usage Learn by example! Here …

Python tuple() — A Simple Guide with Video 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 …

Arbitrary Argument Lists in Python Read More »

Slice Notation – A Simple Illustrated Guide

Summary: Slicing is a Python concept to extract a subsequence from a string or list—that lies within a start and stop index range. There are two syntactical ways to define a slice. (1) The extended slice notation makes use of a colon : in string_name[start:stop:step]. (2) The slice() constructor defines the index range in string_name[slice(start:stop:step)]. …

Slice Notation – A Simple Illustrated Guide 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 »

How to Solve Python “TypeError: ‘int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: ‘int’ object is not iterable”? 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 …

Python One Line 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 …

Python Define Multiple Variables in One Line Read More »

Python One Line Conditional Assignment

Problem: How to perform one-line if conditional assignments in Python? Example: Say, you start with the following code. You want to set the value of x to 42 if boo is True, and do nothing otherwise. Let’s dive into the different ways to accomplish this in Python. We start with an overview: Exercise: Run the …

Python One Line Conditional Assignment Read More »

Asterisk Operator

What is the Asterisk / Star Operator (*) in Python?

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and …

What is the Asterisk / Star Operator (*) in Python? Read More »

The Ultimate Guide To Python Tuples

Python has several built-in data structures such as Lists, Sets and Dictionaries (check out the articles!).In this article, you’ll learn everything you need to know about tuples including real world examples. Tuple Motivating Example Did you ever have a function from which you wanted to return several values? Say, you’ve got a function translate(text, target_language) …

The Ultimate Guide To Python Tuples Read More »

The World’s Most Concise Python Cheat Sheet

Do you want to learn Python but you’re overwhelmed and you don’t know where to start? Learn with Python cheat sheets! They compress the most important information in an easy-to-digest 1-page format. Here’s the new Python cheat sheet I just created—my goal was to make it the world’s most concise Python cheat sheet!