Python Built-in Functions

How to Find the Maximum Value in a Python Dict?

There are three problem variants of finding the maximum value in a Python dictionary: Find the maximum value and return this maximum value Find the maximum value and return a (key, value) tuple of both the key and the max value itself Find the maximum value and return only the key assigned to the max …

How to Find the Maximum Value in a Python Dict? Read More »

Python’s __import__() Function — Dynamically Importing a Library By Name

Python’s built-in “dunder” function __import__() allows you to import a library by name. For example, you may want to import a library that was provided as a user input, so you may only have the string name of the library. For example, to import the NumPy library dynamically, you could run __import__(‘numpy’). In this tutorial, …

Python’s __import__() Function — Dynamically Importing a Library By Name Read More »

Python enumerate() — A Simple Illustrated Guide with Video

If you’re like me, you want to come to the heart of an issue fast. Here’s the 1-paragraph summary of the enumerate() function—that’s all you need to know to get started using it: Python’s built-in enumerate(iterable) function allows you to loop over all elements in an iterable and their associated counters. Formally, it takes an …

Python enumerate() — A Simple Illustrated Guide with Video Read More »

Python dir() — A Simple Guide with Video

If used without argument, Python’s built-in dir() function returns the function and variable names defined in the local scope—the namespace of your current module. If used with an object argument, dir(object) returns a list of attribute and method names defined in the object’s scope. Thus, dir() returns all names in a given scope. Usage Learn …

Python dir() — A Simple Guide with Video Read More »

Python dict() — A Simple Guide with Video

Python’s built-in dict() function creates and returns a new dictionary object from the comma-separated argument list of key = value mappings. For example, dict(name = ‘Alice’, age = 22, profession = ‘programmer’) creates a dictionary with three mappings: {‘name’: ‘Alice’, ‘age’: 22, ‘profession’: ‘programmer’}. A dictionary is an unordered and mutable data structure, so it …

Python dict() — A Simple Guide with Video Read More »

Python getattr()

Python’s built-in getattr(object, string) function returns the value of the object‘s attribute with name string. If this doesn’t exist, it returns the value provided as an optional third default argument. If that doesn’t exist either, it raises an AttributeError. An example is getattr(porsche, ‘speed’) which is equivalent to porsche.speed. Usage Learn by example! Here’s an …

Python getattr() Read More »

Python setattr()

Python’s built-in setattr(object, string, value) function takes three arguments: an object, a string, and an arbitrary value. It sets the attribute given by the string on the object to the specified value. After calling the function, there’s a new or updated attribute at the given instance, named and valued as provided in the arguments. For …

Python setattr() Read More »