3.1 Basic elements
3.1.1 names, values
3.1.2 namespaces
3.2 Expressions
3.2.1 atom
3.2.2 primaries
3.2.3 comparisons
3.2.4 precedence
3.3 Statements

Python variables, expressions and statements

3.1 Basic elements: names and values

3.1.1 Names are bound to values

Python "variables" are special.

Example 3.1

Other languages have "variables" (🎨 Time to draw!)?

Terms:

Example 3.21


        
        


        
        

"Fact"s taken from Ned Bathelder's website:

Takehome message

1. Python names are "pointers".

2. Names bound to mutable objects do not change.

3. Names bound to immutable objects can switch.

3.1.2 Namespaces

Here we distinguish two concepts: namespace and scope: A namespace is a mapping from names to objects. A scope is a textual region of a Python program where a namespace is directly accessible.


Namespaces are usefule when deciding the order of resolution of names. Python follows the "LEGB" rule to resolve names:
Local => Enclosed => Global => Built-in

Example 3.4

Further examples:

Example 3.5

Takehome message

1. Python interpreter searches from smaller namespaces to larger namespaces for a name.

LEGB rule
source: https://8thlight.com/insights/some-common-gotchas-in-python

2. The exception is the unbound local names from within a class. They "jump" from local to global namespaces.

3. A subtle point is that our code live in the __main__ module with ONE global space. For multiple .py files, each has its own global.

3.2 Expressions

Here is the official document.

3.2.1 Atom

3.2.2 Primaries

There are 5 types of primaries.
Example 3.6

3.2.3 Comparisons

Besides these, also comparisons are:

Example 3.7

Example 3.8

Takehome message

1. Comparisons involving NaN are all false, except NaN != NaN

2. None is a singleton (only one instance for its class). Use is or is not.

3. x is y should imply x == y.

4. == invokes __eq__(); is invokes id() comparison; in invoke __contains__() method if defined;

3.2.4 Operation precedence

Expressions comes first, then parimaries, then arithmatic, then bitwise, then comparison.
See here for reference.

3.3 Statements

3.3.1 Expression statement

"In interactive mode, if the value is not None, it is converted to a string using the built-in repr() function and the resulting string is written to standard output on a line by itself (except if the result is None, so that procedure calls do not cause any output.)"

3.3.2 Assignment statements

1. Name binding constructs

Example 3.9

2. Augmented assignment (eval then assign)

Example 3.10 (🎨 Time to draw!)

Augmented assignment calls different functions: for mutable objects, x+=1 is not equal to x = x + 1. The augmented add (+=) can modify objects in place.

A gotcha is to bring to your attention.

Example 3.11

3. The attribute appearing as a name always refer to the instance attribute.

3.3.3 The assert statement: assert None is None [, 1 == 2]

Assertions are primarily for debugging purposes. For a complex code where the bug lies can be difficult to pinpoint. Assertions help by providing checkpoints throughout the code, allowing you to verify that certain conditions hold true as the program executes. If an assertion fails, it raises an AssertionError and provides immediate feedback about where the assumption in the code was violated. This can save time by helping you identify the problematic section of code more quickly.

Note when not debugging, an EAFP style is preferred in the Python language than the LBYL style.

3.3.4 Use raise to throw an exception.

Example 3.12

3.3.5 The yield will be in Functions.

3.3.6 The try statement

1. Lifecycle of the except ... as ... clause.

2. finally is always executed, even after return.

3. Execution order of except: let's go back to the exception example.


1. [Reference: https://nedbatchelder.com/text/names.html]

Back⏎