Wei's Blog

🐒 Software engineer | 📷 Photographer | 👹 Urban explorer

[Python] Basics - Statement

The Python Conceptual Hierarchy Revisited

A way to understand the role of statements is to revisit the concept hierarchy:

  1. Programs are composed of modules.

  2. Modules contain statements, e.g., a module contains a bunch of if statements.

  3. Statements contain expressions, e.g., a if statement contains a == expression.

  4. Expressions create and process objects.

At their base, programs written in the Python language are composed of statements and expressions. Expressions process objects and are embedded in statements. Statements code the larger logic of a program’s operation—they use and direct expressions to process the objects. Moreover, statements are where objects spring into existence (e.g., in expressions within assignment statements), and some statements create entirely new kinds of objects (functions, classes, and so on). At the top, statements always exist in modules, which themselves are managed with statements.

  ...


[Operating System] Encode

ASCII is a simple form of Unicode text, but just one of many possible encodings and alphabets. Text from non-English-speaking sources may use very different letters, and may be encoded very differently when stored in files.

ASCII

ASCII, abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Most modern character-encoding schemes are based on ASCII, although they support many additional characters.

  ...


[Python] Basics - Simple Data Types - String

String

  • Strings in Python can be enclosed in either single quotes (’) or double quotes ("), or three of each (’’’ or “”")
  • Double quoted strings can contain single quotes inside them, as in "Bruce's beard"
  • and single quoted strings can have double quotes inside them, as in 'The knights who say "Ni!"'
  • triple quoted strings: strings enclosed with three occurrences of either quote symbol (either single or double quotes)

  • can even span multiple lines

  ...


[Python] Reference

Weak References

In short, a weak reference, implemented by the weakref standard library module, is a reference to an object that does not by itself prevent the referenced object from being garbage-collected. If the last remaining references to an object are weak references, the object is reclaimed and the weak references to it are automatically deleted (or otherwise notified).

This can be useful in dictionary-based caches of large objects, for example; otherwise, the cache’s reference alone would keep the object in memory indefinitely. Still, this is really just a special-case extension to the reference model. For more details, see Python’s library manual.

  ...


[Python] Collection - Set

Sets

Sets are a recent addition to the language that are neither mappings nor sequences; rather, they are unordered collections of unique and immutable objects. You create sets by calling the built-in set function or using new set literals and expressions in 3.X and 2.7, and they support the usual mathematical set operations (the choice of new {…} syntax for set literals makes sense, since sets are much like the keys of a valueless dic-tionary):

>>> X = set('spam')														# Make a set out of a sequence in 2.X and 3.X
>>> Y = {'h', 'a', 'm'}												# Make a set with set literals in 3.X and 2.7

>>> X, Y 																			# A tuple of two sets without parentheses 
({'m', 'a', 'p', 's'}, {'m', 'a', 'h'})

>>> X & Y 																		# Intersection 
{'m', 'a'}
>>> X | Y 																		# Union 
{'m', 'h', 'a', 'p', 's'}
>>> X - Y 																		# Difference
  ...