FIND YOUR NEXT FEW LINES

Results for “Python”

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
PythonDates & time zones

Skip weekends when advancing dates

Python: Walk calendar dates and count only Monday through Friday.

Python✓ Tested
PythonIteration & sequences

Split a list into fixed-size batches

Python: Slice a sequence at regular offsets; the final batch can be smaller.

Python✓ Tested
PythonGetting Started

Store a value in a named variable

Python: Give a value a descriptive name and print it.

Python✓ Tested
PythonIteration & sequences

Take a limited number of iterator values

Python: Use islice to consume only the requested prefix of a potentially unbounded iterator.

Python✓ Tested
PythonGetting Started

Update a running count

Python: Change a variable after its initial value has been assigned.

Python✓ Tested
PythonText validation & encoding

Validate an entire identifier

Python: Use fullmatch so a regex must consume the complete input.

Python✓ Tested
PythonIteration & sequences

Visit adjacent pairs

Python: Use itertools.pairwise to process neighboring items without indexing.

Python✓ Tested
PythonGetting Started

Visit every item in a collection

Python: Print each element in the collection’s original order.

Python✓ Tested
PythonFunctions & error handling

Yield values on demand

Python: Use a generator to produce a sequence without storing all intermediate values.

Python✓ Tested
PythonFiles & paths

Append a line to a file

Add a new line to the end of a text file without replacing its existing contents. Append mode also creates the file if it does not exist.

file✓ Tested
PythonCollections

Count occurrences in a list

Count how often each value appears using Counter from the standard library.

count✓ Tested
PythonFiles & paths

Create nested folders

Create a folder and any missing parent folders in one call. Running the code again is safe when the folder already exists.

directory✓ Tested