Python Variable Scope: Local, Global, Nonlocal and LEGB

Python variable scope: Command Prompt output showing the LEGB rule with local, enclosing, global and built-in names

Scope decides where a Python variable can be seen: a name created inside a function is local, and a name at the top of a file is global. Python looks names up in Local, Enclosing, Global, then Built-in scope, the LEGB rule. These examples ran on Python 3.12.5, and the screenshots come straight from the … Read more >>

Recursion in Python: A Complete Guide with Examples

Recursion in Python: Command Prompt output tracing the recursive calls and returns of factorial(4) on the call stack

Recursion in Python is when a function calls itself to solve a smaller version of the same problem. Every recursive function needs a base case that stops the calls and a recursive case that moves toward it; without a base case Python stops with RecursionError after about 1,000 calls. This guide shows how Python recursion … Read more >>

Regular Expressions in Python (re Module Guide)

Regular expressions in Python: Command Prompt output of re.search, re.match, re.fullmatch, re.findall, re.finditer, re.sub and re.split

Regular expressions in Python are patterns for finding, checking, extracting and replacing text, and they are provided by the built-in re module. re.search(r”\d+”, text) finds the first number in a string, re.findall() returns every match, and re.sub() replaces them. This guide covers every re function, the pattern syntax, groups, greedy and lazy matching, flags, lookarounds, … 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 >>

Python dict() Function: A Practical Guide With Examples

Python dict() Function

When I build a small reporting script, I often need a fast way to organize related details. For example, an employee record may need a name, department, email, and monthly sales figure. Keeping those values in separate variables becomes messy quickly. That is where the Python dict() function helps. It creates a dictionary, which stores … 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 complex() Function: A Practical Guide

Python complex() Function

A few years ago, I worked on a small signal-analysis script for a Chicago engineering team. The input data included values such as 12+5j, and the script needed to calculate magnitudes, phase angles, and combined signals. Regular integers and floats were not enough. That is where Python’s built-in complex() function becomes useful. It creates complex … Read more >>

How to Initialize an Array in TypeScript

Initialize an array in TypeScript: Command Prompt output of typed arrays declared with values, empty and with the generic Array syntax

To initialize an array in TypeScript, declare the element type and give it a value: const cities: string[] = [“Austin”, “Denver”], or const ids: string[] = [] to start empty. Array<string> means the same as string[]. For a fixed number of slots use Array.from({ length: n }, …) rather than new Array(n), which leaves holes. … Read more >>

Check if an Array Is Null or Empty in TypeScript

Check if an array is null or empty in TypeScript: Command Prompt output showing the check against a filled array, an empty array, null and undefined

To check if an array is null or empty in TypeScript, write if (!items || items.length === 0), or the shorter if (!items?.length), which covers null, undefined and [] in one test. The order matters: reading .length before the null check throws at runtime. This guide shows both forms running against all four cases, the … Read more >>