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

Read an ISO week number

Python: Use isocalendar because ISO week years can differ from calendar years near January.

Python✓ Tested
PythonMappings & sets

Read an optional dictionary key

Python: Use get with a fallback while leaving the dictionary unchanged.

Python✓ Tested
PythonFile operations & archives

Read compressed text directly

Python: Open gzip data in text mode with an explicit encoding.

Python✓ Tested
PythonStructured data & serialization

Read elements from trusted XML

Python: Use ElementTree to inspect a small trusted XML document.

Python✓ Tested
PythonGetting Started

Read one character from ASCII text

Python: Use a valid zero-based index to read the first character.

Python✓ Tested
PythonStructured data & serialization

Read one JSON document per line

Python: Parse nonempty JSON Lines records independently instead of loading a JSON array.

Python✓ Tested
PythonGetting Started

Read the beginning of a text value

Python: Extract or display the first three ASCII characters from a known nonempty string.

Python✓ Tested
PythonGetting Started

Read the first collection element

Python: Access index zero only after establishing that the collection is nonempty.

Python✓ Tested
PythonNumbers & calculations

Reject NaN and infinite numeric inputs

Python: Use isfinite before computations that require an ordinary finite float.

Python✓ Tested
PythonText validation & encoding

Remove a known prefix safely

Python: Use removeprefix instead of lstrip when removing a literal prefix rather than a set of characters.

Python✓ Tested
PythonText validation & encoding

Remove a known suffix safely

Python: Use removesuffix to remove only a matching ending.

Python✓ Tested
PythonMappings & sets

Remove and return a dictionary entry

Python: Use pop with a default when a missing entry is an expected condition.

Python✓ Tested