Create an Empty Dictionary in Python ({} vs dict())

Create an empty dictionary in Python: the Command Prompt showing dict.fromkeys giving every key the same list object

Creating an empty dictionary in Python takes one pair of braces: Both give you a real dictionary with no keys in it. The braces are the common choice, and they are marginally faster, but the two are interchangeable. What follows covers filling it afterwards, starting one with keys already in place, and the three ways … Read more >>

Fix SyntaxError: invalid syntax in Python

invalid syntax

When I started working with Python, I often ran into the dreaded SyntaxError: invalid syntax message. It was frustrating because sometimes the error didn’t make sense at first glance. I felt like my code looked perfectly fine, but Python kept complaining. Over the years, I’ve learned that syntax errors are usually caused by small mistakes, … Read more >>

Append to a Dictionary in Python (There Is No append Method)

Python dictionary append: the Command Prompt showing the AttributeError that a dict object has no attribute append

Python dictionaries have no .append() method. You add to one by assigning to a key: Calling .append() on a dictionary raises AttributeError, because that method belongs to lists. A list adds to the end; a dictionary needs you to say which key. What follows covers adding one key, adding several, merging two dictionaries, and the … Read more >>

Check if an Array Contains a Value in Python (in operator)

Check if an array contains a value in Python: the Command Prompt showing the in operator returning True and False

Checking whether a Python array contains a value is done with the in operator: Note the vocabulary. What most people call an array, Python calls a list, and in works on both that and a NumPy array. There is no includes() or contains() method. Below: conditions rather than exact values, checking several at once, NumPy … Read more >>

Python bool() Function: A Practical Guide With Examples

Python bool() Function

When I build small reporting scripts, one common job is deciding whether the script should continue. Does the CSV contain rows? Did the API return data? Did a user enter a value? The Python bool() function gives a clean, reliable answer: True or False. You will use bool() constantly in if statements, loops, input checks, … Read more >>

Python callable() Function: Check Callables with Examples

Python callable() Function

You are building a small reporting script for a Chicago sales team. The script accepts a formatting rule from a settings file, then applies it to each sales record. Before calling that rule, you need to know whether it is actually something Python can run. That is where the callable() function helps. I have used it in … Read more >>

Python classmethod() Function: Complete Guide With Examples

Python classmethod() Function

When I build small reporting tools, I often need to create the same kind of object from different inputs. One report may come from a CSV file, another from a dictionary returned by an API, and another from values entered in the terminal. Creating separate setup code outside the class gets messy fast. The Python classmethod() function … Read more >>

Python delattr() Function: Remove Object Attributes

Python delattr() Function

When I build small automation scripts, I often create objects to hold temporary report data—such as a customer’s draft notes, an API token, or a calculated status. Once that information is no longer valid, I do not want it sitting on the object and getting reused accidentally. That is where the Python delattr() function helps. … Read more >>

Python dir() Function: A Practical Guide With Examples

Python dir() Function

When I build a small reporting script, I often receive an unfamiliar object from a library, API response, or custom class. I need to know what that object contains before I can use it safely. Rather than guessing method names or digging through code first, I use the Python dir() function. The dir() function gives … Read more >>

Check if a Key Exists in a Python Dictionary (in, get and More)

Python dictionary contains: Command Prompt showing get returning None for an existing key and the in operator

To check whether a Python dictionary contains a key, use in: "state" in customer returns True or False. It checks keys, not values, and it’s the fastest way to ask: Values, get(), several keys at once and nested JSON each have a catch, so I’ll walk through them with real output from Python 3.14.7. Check … Read more >>