Source
Adapted from my own handwritten and hand-typed notes from Year 1 to Year 3 of my EEE degree
Standard Data Types
| Type | Meaning | Example |
|---|---|---|
int | integer | 7 |
float | floating point | 3.14 |
complex | complex number | 2 + 3j |
str | text in '...' or "..." | 'Hello' |
bool | True or False | True |
list | ordered, mutable, mixed types allowed | [0, 3.2, 'a'] |
tuple | ordered, items cannot be changed | ('Paris', 'Rome') |
dict | key value pairs | {'Amy': 8} |
set | unordered, unique items | {'Amy', 'Ben'} |
int, float and complex are the numeric data types.
Arithmetic
Operators that return a number (int, float or complex):
+,-,*,/for basic arithmetic (/always gives a float)**for exponent, e.g.2 ** 3gives8//for floor division, e.g.7 // 2gives3
Comparison
Operators that return a bool:
a == banda != bfor equal and not equal>,>=,<,<=for ordering, which cannot be used oncomplex
Floats
Avoid
a == b,a >= b,a <= bon floats. Rounding error means0.1 + 0.2 == 0.3isFalse.
Logical Operators
not, and, or work on booleans and return a bool:
not False # True
False and True # False
True and True # True
False or True # True
False or False # False
(3 > 2) and not (1 == 2) # TrueArithmetic on Booleans
True behaves like int(1) and False like int(0):
True + True # 2
True == 1 # True
sum([True, False, True]) # 2, handy for countingWhen mixed types meet in an operation, Python picks the result type itself, covered in Type Conversion Identity and Membership.