ord() Function in Python (With Examples)

ord() function in Python: Command Prompt output of ord on letters, digits and symbols and chr converting codes back

The ord() function in Python takes a single character and returns its Unicode code point, an integer. chr() does the reverse, so the two always round-trip: I ran each example on Python 3.12.5 in a Windows Command Prompt, including the accented characters and symbols, so the numbers you see are exactly what Python returned. You’ll … Read more >>

If Else Statement in Python (With Examples)

If else statement in Python: Command Prompt output of an if elif else chain choosing shipping tiers

The if else statement in Python runs one block of code when a condition is true and another block when it’s false. You write the condition after if, end the line with a colon, and indent what should run: Online-store rules like shipping tiers and sales tax make good examples, so that’s what I used. … Read more >>

Constants in Python: How to Define and Use Them

Constants in Python: Command Prompt showing a typing.Final constant reassigned at runtime and mypy reporting cannot assign to final name

Constants in Python are a convention, not a language feature: you name a variable in UPPER_CASE at module level, and everyone agrees not to change it. Python won’t stop a reassignment, but typing.Final lets a type checker catch one: One screenshot below shows mypy catching a Final violation that Python itself lets through; I used … Read more >>

reduce() in Python: functools.reduce With Examples

reduce in Python: Command Prompt output showing functools.reduce adding sales one step at a time

reduce() in Python combines the items of an iterable into a single value by applying a two-argument function over and over: first to items one and two, then to that result and item three, and so on. It lives in functools: I printed the intermediate steps so you can watch reduce() work; the screenshots come … Read more >>

sum() Function in Python (With Examples)

sum() function in Python: Command Prompt comparing sum of ten 0.10 floats on Python 3.12 and 3.11 with math.fsum and Decimal

The sum() function in Python adds up the numbers in any iterable, such as a list, tuple, range or generator, and returns the total. An optional second argument sets the starting value: I ran every example on Python 3.12.5, and one of them on Python 3.11 too, because sum() changed how it adds floats in … Read more >>

Tuple of Tuples in Python (With Examples)

Tuple of tuples in Python: Command Prompt output indexing a nested tuple of US cities

A tuple of tuples in Python is a tuple whose items are themselves tuples, so you get rows of fixed values you can’t change. You build one with nested parentheses and read it with two indexes: These examples use a small table of US cities, and the screenshots come from Python 3.12.5 running in a … Read more >>

len() Function in Python (With Examples)

len() function in Python: Command Prompt output of len on a string, list, tuple, dictionary, set and range

The len() function in Python returns how many items an object holds: characters in a string, elements in a list or tuple, keys in a dictionary. It works on any sequence or collection, and it’s instant, because Python stores the size rather than counting: Every output here comes from Python 3.12.5 in the Windows Command … Read more >>

How to Install and Use Multiple Python Versions

Install multiple Python versions: Command Prompt running the same script on Python 3.11, 3.12 and 3.13

You can install multiple Python versions side by side, each in its own folder. On Windows, the Python install manager (py install 3.11) adds versions and py -3.11 picks one. On any system, uv or pyenv install and switch them for you. For the screenshots, this machine has Python 3.12 from python.org plus 3.11 and … Read more >>

Environment Variables in Python: Read, Set and Check Them

Environment variables in Python: Command Prompt setting STORE_REGION and MAX_RETRIES and Python reading them with os.environ and os.getenv

Environment variables in Python live in os.environ, a dictionary-like object. Read a required one with os.environ[“NAME”], an optional one with os.getenv(“NAME”, default), and check for one with “NAME” in os.environ: Every screenshot here is from the Windows Command Prompt running Python 3.12.5 with python-dotenv 1.2.4, and every variable is one I created for the demo. … Read more >>

Check the Type of a Variable in Python: type() vs isinstance()

Check the type of a variable in Python: Command Prompt output of type() on an int, float, string, bool, None, list and dict

Checking a variable’s type in Python takes one of two built-ins. type(x) tells you exactly what x is, and isinstance(x, SomeType) tells you whether it’s that type or a subclass of it: I tested everything below on Python 3.12.5 with NumPy 2.5.3; the screenshots are the real Command Prompt output. Use isinstance() for checks in … Read more >>