Python type() Function

Python’s built-in type() function has two purposes. First, you can pass an object as an argument to check the type of this object. Second, you can pass three arguments—name, bases, and dict—to create a new type object that can be used to create instances of this new type. Usage Learn by example! Here’s an example … Read more

Python object() Function

Python’s built-in object() function takes no argument and returns a new featureless object—the base and parent of all classes. As such it provides all methods that are common to all Python class instances such as __repr__() and other “dunder” methods. However, unlike for all non-object instances, you cannot assign arbitrary attributes to an instance of … Read more

Python repr() Function — A Helpful Guide with Example

Python’s built-in repr(obj) function returns the standard string representation of the provided object. This often includes the type and memory address of the object—for lack of further information. For example, the returned string representation may be ‘<main.Car object at 0x000001F66D11DBE0>’ for a custom object of type Car. The function internally calls the method obj.__repr__() which … Read more

Python str() Function

Python’s built-in str(x) function converts the object x to a string using the x.__str__() method or, if non-existent, the repr(x) built-in function to obtain the string conversion. Syntax str() Syntax: str(object) # –> Most common case: convert an object to a string str(object=b”, encoding=’utf-8′, errors=’strict’) # –> Not so common case: Converts a bytes or … Read more

Python round() — A Simple Guide with Video

Python’s built-in round() function takes two input arguments: a number and an optional precision in decimal digits. It rounds the number to the given precision and returns the result. The return value has the same type as the input number—or integer if the precision argument is omitted. Per default, the precision is set to 0 … Read more

Python reversed() — A Simple Guide with Video

Python’s built-in reversed(sequence) function returns a reverse iterator over the values of the given sequence such as a list, a tuple, or a string. Usage Learn by example! Here are some examples of how to use the reversed() built-in function. The most basic use is on a Python list: You can see that the return … Read more

Python frozenset() — A Simple Guide with Video

Python’s built-in frozenset() function creates and returns a new frozenset object. A frozenset is an immutable set—so you cannot change the frozenset after creation and set methods such as add() or remove() don’t work on the frozenset. Without an argument, frozenset() returns an empty frozenset. With the optional argument, frozenset(iter) initializes the new frozenset with … Read more

Python set() Function — A Simple Guide with Video

Python’s built-in set() function creates and returns a new set object. A set is an unordered collection of unique elements. Without an argument, set() returns an empty set. With the optional argument, set(iter) initializes the new set with the elements in the iterable. Read more about sets in our full tutorial about Python Sets. Usage … 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 … Read more

Python format() Function: No-BS Guide by Example

The web is filled with shitty tutorials about Python’s formatting feature. At times, it can become really confusing—and it’s hard to see the forest for the trees. In this tutorial, I’ll try to gradually build a basic understanding of the built-in format() function, what it does, and how you can use it to become a … Read more