Source

Adapted from my own handwritten and hand-typed notes from Year 1 to Year 3 of my EEE degree

Standard Data Types

TypeMeaningExample
intinteger7
floatfloating point3.14
complexcomplex number2 + 3j
strtext in '...' or "..."'Hello'
boolTrue or FalseTrue
listordered, mutable, mixed types allowed[0, 3.2, 'a']
tupleordered, items cannot be changed('Paris', 'Rome')
dictkey value pairs{'Amy': 8}
setunordered, 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 ** 3 gives 8
  • // for floor division, e.g. 7 // 2 gives 3

Comparison

Operators that return a bool:

  • a == b and a != b for equal and not equal
  • >, >=, <, <= for ordering, which cannot be used on complex

Floats

Avoid a == b, a >= b, a <= b on floats. Rounding error means 0.1 + 0.2 == 0.3 is False.

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)   # True

Arithmetic 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 counting

When mixed types meet in an operation, Python picks the result type itself, covered in Type Conversion Identity and Membership.