Python in One Hour
Contents
Introduction
(This obviously doesn't count as part of the hour.)
This document explains all of Python, It should take less than an hour to read through it all. However, I doubt anyone will truly grasp all of the concepts in Python in that hour.
This is intended to be a complete map to the entire Python language.
Overview
In Part 1, we cover the syntax. This is the fundamental element of the language, namely, what series of letters and numbers are valid and what they mean.
In Part 2, we examine the basic functions and types.
In Part 3, we cover functions. That is, how they are defined and called.
In Part 4, we cover objects, how they are defined and used.
Part 1: Syntax
Python programs are files that consist of doc strings, comments, and statements.
File Doc Strings
Doc strings are string literals that appear at the very beginning of the file. This documents the file.
Comments
Comments begin with a '#' and end at the end of the line. ('#' inside of string literals are ignored.)
Statements
Statements are one of the following types of statements:
- Expressions
- Simple statements
- Complex statements
Expressions
Expressions can be:
- A literal
- A list comprehension
- A generator comprehension
- An attribute reference
- A subscription
- A call
- One of the many operators
Literals
A literal may be:
- A string literal
- A numeric literal
- A list display
- A tuple display
- A dict display
String Literals
String literals are enclosed by single quotes ('), double quotes ("), or triple single or double quotes ( or """). They may be prepended with 'u' or 'U' (meaning unicode), or 'r' or 'R' (meaning raw), or a combination of the two ('UR' or 'ur', etc... as long as 'u' comes first.)
Normal and unicode string literals may contain escape sequences. The following escape sequences are valid:
\newline Ignored \\ Backslash (\) \' Single quote (') \" Double quote (") \a ASCII Bell (BEL) \b ASCII Backspace (BS) \f ASCII Formfeed (FF) \n ASCII Linefeed (LF) \N{name} Character named name in the Unicode database (Unicode only) \r ASCII Carriage Return (CR) \t ASCII Horizontal Tab (TAB) \uxxxx Character with 16-bit hex value xxxx (Unicode only) \Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only) \v ASCII Vertical Tab (VT) \ooo Character with octal value ooo \xhh Character with hex value hh
Unrecognized backslash escape sequences are left in the string as-is.
Raw string literals do not use any escape sequences. Raw string literals cannot end in a backslash. If you need to end in a backslash, you must use a normal string.
String literals followed by another string literal (separated by optional whitespace) are concatenated together.
Numeric Literals
Numeric literals may be:
- Decimal integers: 12345
- Octal integers: 012345 or 0o12345
- Hex integers: 0x1234
- Binary integers: 0b010101
- Long integers: One of the above followed by 'L' or 'l'. ('L' is preferred.)
- Floats: a decimal integer, followed by an optional sequence of the fractional part (which is a decimal point '.' followed by an option decimal integer), followed by an optional exponential part ('e' or 'E' followed by an optional sign followed by a decimal integer.) Either the decimal point or the exponential part must be present.
Tuple Display
(This gets confused with parentheses all the time.)
A tuple display starts with a parentheses '(', followed by expressions separated by commas, ending with an optional comma, and followed by a closing parentheses ')'.
If there is only one item in the tuple display, then it must be followed by a comma. If not, then it is interpreted as the grouping parentheses.
List Display
List displays are just like tuple displays except they are surrounded with square brackets '[' and ']'. The trailing comma after a single item is not necessary.
Dict Display
Dict displays are curly braces '{' and '}' with dict items separated by commas. Each dict item is the key expression followed by a colon ':' followed by the value expression. Trailing commas are optional.
List Comprehension
A list comprehension is surrounded by square braces like the list display. However, inside is an expression followed by at least one 'for' and then zero or more 'if' clauses.
The 'for' clauses are of the form "for variables in expression". The 'variables' is one or more identifiers separated by commas. The expression should evaluate to some kind of tuple, list, or other iterable object.
The 'if' clause is followed by a single expression.
It is interpreted as follows.
The first 'for' expression is evaluated. The result is modified into an iterable. Then the first item is pulled off and assigned to the variables in parallel. Then the second 'for' is evaluated likewise, within the newly created namespace, and so on until the last 'for' expression is evaluated. If any of these are exhausted, then the next iteration of the next higher up 'for' expression is obtained. When the first 'for' loop is exhausted, then
Part 2: Basic Functions and Types
Part 3: Functions
Part 4: Objects
Conclusion
From here, your next task is to familiarize yourself with the standard library, as well as third party libraries that are available for Python.