A
Text in a program may need a small change or check before you use it. Python string methods are operations you call on text to trim its edges or split it into parts. I’ll show how to choose a method by the result you need and how to use common methods in a short text-cleaning […]
A
When a list of records includes entries that pass a condition and entries that do not, selecting the matches calls for a test. Python’s filter() applies a test, called a predicate, to each item and returns an iterator containing the items that pass. I’ll show how to write that test and consume the iterator, then […]
A
As you split a Python project into packages, you may see a leading dot in an import and wonder which module it names. Python relative imports use those dots to resolve a name from the importing module’s package. I’ll explain how each dot changes the starting point and show how to run a package module. […]
A
An unfamiliar Python value can leave you unsure what operations it supports. type() reports the value’s class, which defines what its instances can do, or creates a class when called with three arguments. I’ll walk through both forms of type() and show when to use type() or isinstance(). TL;DR: What Python type() Does Python type() […]
A
As a function grows, callers may need to supply values beyond its named parameters. In Python, *args collects extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary. This article explains how to collect and expand those arguments, then how to forward them through another function. TL;DR: *args and **kwargs […]
A
When one Python file needs to use a function defined in another, you can put that function in a module. A Python module is a source file that another file can import. In this piece, I’ll show how to save reusable code in a .py file and import it from another script. TL;DR A Python […]
A
To reuse a function across Python files, you can put it in one file and import that file where you need it. A Python module is a file of statements and definitions, usually with a .py extension, that another file can access with an import. I’ll show how to create and import a module, then […]
A
When a calculation uses text as a number, the conversion or the division can fail. Python’s except clauses let a program catch errors raised inside a try block and choose how to respond. This article shows how to handle several exception types with one clause when they share a response, and how to use separate […]
A
Calendar-based tasks often need every day between a start date and an end date. Python’s date and timedelta classes let you move forward one day at a time to build that sequence. I’ll show how to create a range that includes the end date, then explain when to stream the dates or store them. TL;DR: […]
A
Python code that calculates with calendar dates or clock times can keep those values as objects until it needs display text. The built-in datetime module provides the types and operations for that work. In this piece, I’ll show you how to create date and time values, then handle local clock changes with a named time […]
A
When a script saves a file inside a new folder, that folder has to exist first. Python’s standard library provides pathlib for working with paths, and Path.mkdir() creates a folder while parents=True tells it to create any missing folders above it. I’ll show how to create a directory and check where a path written from […]
A
A Python application can use different settings in development and deployment without changing its source code. Environment variables carry those settings into a program when it starts, and Python lets your code read or change them. I’ll show you how to choose what happens when a setting is missing and how to read or set […]
A
A CSV file may need a second copy in another directory while the original stays available. Python’s shutil module provides file-copy functions, and shutil.copy() writes the file at the destination while leaving the source in place. I’ll show you how to copy a file with shutil.copy() and read the output to see that the source […]
A
When values from two iterables need to go into one Python list, the order of the loops determines the order of the results. A list comprehension builds a new list by evaluating an expression for the values its clauses supply. I’ll explain how the clauses map to nested loops and show how the expression controls […]
A
A loop that builds a list from changed input values can leave you wondering whether there is a more direct way to write it. A Python list comprehension builds a new list by applying an expression to items from an iterable, with an optional filter. I’ll show how to turn that loop into a comprehension […]
A
Sooner or later a Python script that talks to the web needs to send its requests from somewhere else. You may be checking how an API answers from another country, running a monitor that should not share your office IP, or collecting public data from a site that limits requests per address. A proxy is […]