Show Menu
Cheatography

Python Core Language Cheat Sheet (DRAFT) by

Python core language cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Lexical Analysis

Line Structure
Joining Lines
Blank Lines
Comments
Encoding Declar­ation
Indent­ation

Input and Output

Printing output
print(­*ob­jects, sep=' ', end='\n', file=s­ys.s­tdout, flush=­False)
print(­"­Hello world!­", end=" ")
Getting input
input(­[pr­ompt])
input(­"­Enter your age")

Indent­ation

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indent­ation level of the line, which in turn is used to determine the grouping of statem­ents.

Encoding Declar­­ation

If a comment in the first or second line of the Python script matches the regular expres­sion, this comment is processed as an encoding declar­ation; the first group of this expression names the encoding of the source code file.
#!/usr­/bi­n/env python
# -*- coding: utf-8 -*-

Comments

Hash
#
# some comment
DocString
"­"­" "­"­"
"­"­"­Dem­ons­trates docstrings and does nothing really."""

Blank Lines

A logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored.
During intera­ctive input of statem­ents, handling of a blank line may differ depending on the implem­ent­ation of the read-e­val­-print loop.
In the standard intera­ctive interp­reter, an entirely blank logical line terminates a multi-line statement.

Joining Lines

Explicit line joining
Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of­-line character.
if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1
Implicit line joining
Expres­sions in parent­heses, square brackets or curly braces can be split over more than one physical line without using backsl­ashes.
month_­names =
['Januari', 'Febru­ari', 'Maart', # These are the
'April', 'Mei', 'Juni', # Dutch names 'Juli', 'Augus­tus', 'Septe­mber', # for the months 'Oktober', 'Novem­ber', 'Decem­ber'] # of the year
Remember:
-> A line ending in a backslash cannot carry a comment.
-> A backslash does not continue a comment.
-> A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backsl­ash).
-> A backslash is illegal elsewhere on a line outside a string literal.

Line Structure

Logical Lines
A logical line is what Python sees as a single statement
Here is one logical on a physical line:
1) my_list = [1, 2, 3, 4]
2) print(­"­Hello world!­")
Physical Lines
A physical line is what you see when you write the program. Two or more physical lines may be joined into logical lines using backslash charac­ters.
Here is one logical line on two physical lines:
1) my_list = [1, 2,
3, 4]
2) s = 'This is a string. \ This continues the string.'

Introd­uction

Founder
Guido van Rossum
First release
February 20, 1991
Developers
Python Software Foundation
Paradigm
Multi-­par­adigm: functi­onal, impera­tive, object­-or­iented, struct­ured, reflective
Type Discipline
Duck, dynamic, gradual
Language Type
Interp­reted
 

Identi­fiers

Lu - uppercase letters
A..Z
UPPERC­ASENAME
Ll - lowercase letters
a...z
LOWERC­ASENAME
Lt - titlecase letters
a..zA..z
titleC­aseName
Nl - letter numbers
a...zA...z­1...n
name2000 | NAME999
Pc - connector punctu­ations
a_zA_Z
name_c­onn­ector | NAME_C­ONN­ECTOR
A Python identifier is a name used to identify a variable, function, class, module or other object.
Identifier naming standards are mentioned above.

Keywords and Reserved words

False
await
else
import
pass
None
break
except
in
raise
True
class
finally
is
return
and
continue
for
lambda
try
as
def
from
nonlocal
while
assert
del
global
not
with
async
elif
if
or
yield
Keywords cannot be used as ordinary identi­fiers.

Variables

Definition
Variable is a reserved memory location to store values
Syntax
IDENTI­FIE­R_NAME = VALUE(1)
Assignment Operator
=
Walrus Operat­or/­Ass­ignment Expres­sions
:=
Example
number = 10 | name = "Some name" | print(­walrus := True)
Multiple Assignment
a = b = c = 1 | a,b,c = 1,2,"jo­hn"
Deleting variable
del VARIAB­LE_NAME
(1) VALUE => any built-in type or user-d­efined class or function

Built-in Types

Boolean types
Truthy Falsy values
Scalar
Numeric types
Integer, Float, Complex
Scalar
Sequence types
List, Tuple, Range
Compound
Text Sequence types
String
Scalar
Mapping types
dictionary
Compound
Binary Sequence types
bytes, bytesa­rray, memoryview
Compound
Set types
set, frozenset
Compound
Other types
functions, modules, class, methods, code objects, type objects, elipsis object, null object, notImp­lem­ented objects
Special attributes

Boolean Type

Reference
Link

Numeric Type

Integer
Link
Float
Link
Complex
Link

Sequence Types

Common sequence operations
Link
List
Link
Tuple
Link
Range
LInk
String
Link

Mapping type

Dictionary
Link

Binary Sequence Types

Bytes
Link
Bytesarray
Link
Memoryview
Link

Sets ypes

Set
Link
Frozenset
Link

Other Types

Functions
Link
Class
Link
Modules
Link
Elipsis
Link
Null
Link
NotImp­lem­ented
Link
Special Attributes
Link