Python Tuple

How to Create a Tuple from a String and a List of Strings

Tuples, Strings, and Lists Before we look at creating tuples, as a starting point it would be useful to clarify the meaning of the various terms we will be using throughout this article:  A string is a sequence of characters, and represents one of the main Python data types. A list is used to store …

How to Create a Tuple from a String and a List of Strings Read More »

How Does Tuple Comparison Work In Python?

A Quick Introduction To Tuples Python consists of 4 built-in data types that are used to store collections of data. These data types are: List Set Dictionary Tuple A tuple allows you to store multiple items within a single variable. Hence, it is a collection that is ordered and unchangeable/immutable. Also, tuples are heterogeneous as …

How Does Tuple Comparison Work In Python? Read More »

Python sum() – A Simple Illustrated Guide

Summing up numbers is one of those repetitive tasks you need to do over and over again in your practical code projects. To help you accomplish this task in a concise, readable, and efficient way, Python’s creators have added the built-in sum() function. It sums over all elements in a Python list—or any other iterable …

Python sum() – A Simple Illustrated Guide Read More »

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 »