Python by Example Using Cloudflare Dynamic Workers

adewale1 pts1 comments

Python By Example

Python By Example

Learn Python with small, editable examples backed by the official Python 3.13 docs. Run each snippet in an isolated Dynamic Python Worker using the newest Python version currently supported by Cloudflare Workers/Pyodide.

Basics<br>Hello World<br>The first Python program prints a line of text.<br>Values<br>Python programs evaluate expressions into objects such as text, numbers, booleans, and None.<br>Literals<br>Literals write values directly in Python source code.<br>Numbers<br>Python numbers include integers, floats, and complex values.<br>Booleans<br>Booleans represent truth values and combine with logical operators.<br>Operators<br>Operators combine, compare, and test values in expressions.<br>None<br>None represents expected absence, distinct from missing keys and errors.<br>Variables<br>Names are bound to values with assignment.<br>Constants<br>Python uses naming conventions and optional types for values that should not change.<br>Truthiness<br>Python conditions use truthiness, not only explicit booleans.<br>Object Lifecycle<br>Names keep objects reachable until the last reference goes away.<br>Bytes and Bytearray<br>bytes and bytearray store binary data, not Unicode text.

Data Model<br>Equality and Identity<br>== compares values, while is compares object identity.<br>Mutability<br>Some objects change in place, while others return new values.<br>Special Methods<br>Special methods connect your objects to Python syntax and built-ins.<br>Truth and Size<br>__bool__ and __len__ decide how objects behave in truth tests and len().<br>Container Protocols<br>Container methods connect objects to indexing, membership, and item assignment.<br>Callable Objects<br>__call__ lets an instance behave like a function while keeping state.<br>Operator Overloading<br>Operator methods let objects define arithmetic and comparison syntax.<br>Attribute Access<br>Attribute hooks customize lookup, missing attributes, and assignment.<br>Bound and Unbound Methods<br>instance.method binds self automatically; Class.method is a plain function.<br>Descriptors<br>Descriptors customize attribute access through __get__, __set__, or __delete__.<br>Context Managers<br>with ensures setup and cleanup happen together.<br>Delete Statements<br>del removes bindings, items, and attributes rather than producing a value.

Text<br>Strings<br>Strings are immutable Unicode text sequences.<br>String Formatting<br>f-strings turn values into readable text at the point of use.<br>Regular Expressions<br>The re module searches and extracts text using regular expressions.

Control Flow<br>Conditionals<br>if, elif, and else choose which block runs.<br>Guard Clauses<br>Guard clauses handle boundary cases early so the main path stays flat.<br>Assignment Expressions<br>The walrus operator assigns a value inside an expression.<br>For Loops<br>for iterates over values produced by an iterable.<br>Break and Continue<br>break exits a loop early, while continue skips to the next iteration.<br>Loop Else<br>A loop else block runs only when the loop did not end with break.<br>Match Statements<br>match selects cases using structural pattern matching.<br>Advanced Match Patterns<br>match patterns can destructure sequences, combine alternatives, and add guards.<br>While Loops<br>while repeats until changing state makes a condition false.

Iteration<br>Iterating over Iterables<br>for loops consume values from any iterable object.<br>Iterators<br>iter and next expose the protocol behind for loops.<br>Iterator vs Iterable<br>Iterables produce fresh iterators; iterators are one-pass.<br>Sentinel Iteration<br>iter(callable, sentinel) repeats calls until a marker value appears.<br>Generators<br>yield creates an iterator that produces values on demand.<br>Yield From<br>yield from delegates part of a generator to another iterable.<br>Generator Expressions<br>Generator expressions use comprehension-like syntax to stream values lazily.<br>Itertools<br>itertools composes lazy iterator streams.

Collections<br>Lists<br>Lists are ordered, mutable collections.<br>Tuples<br>Tuples group a fixed number of positional values.<br>Unpacking<br>Unpacking binds names from sequences and mappings concisely.<br>Dictionaries<br>Dictionaries map keys to values for records, lookup, and structured data.<br>Sets<br>Sets store unique values and make membership checks explicit.<br>Slices<br>Slices copy meaningful ranges from ordered sequences.<br>Comprehensions<br>Comprehensions build collections by mapping and filtering iterables.<br>Comprehension Patterns<br>Comprehensions can use multiple for clauses and filters when the shape stays clear.<br>Sorting<br>sorted returns a new ordered list and key functions choose the sort value.<br>Collections Module<br>collections provides specialized containers for common data shapes.<br>Copying Collections<br>Copies can duplicate the outer container while nested objects may still be shared.

Functions<br>Functions<br>Use def to name reusable behavior and return results.<br>Keyword-only Arguments<br>Use * to require selected function arguments to be named.<br>Positional-only Parameters<br>Use / to mark parameters that callers must pass by position.<br>Args and Kwargs<br>*args collects extra positional arguments and **kwargs collects named ones.<br>Multiple Return Values<br>Python...

values python objects text expressions while

Related Articles