Variable declaration doesn't require type definition (dynaically typed).

answer = 42
name = "The Dude"

The downside to dynamic types is that it is possible to put an incorrect type into an equation, which will only become apparent at runtime.

def add_numbers(a, b):
    return a + b

print(add_numbers(1, "word"))

Type Hinting can asssit with that, but is more for the benefit of the editor so it can highlight errors. Python still lets you put the incorrect type in.

def add_numbers(a: int,b: int) -> int:
    return a + b

print(add_numbers(1, "word"))

Integers & Floats

integer_variable = 234
float_variable = 2.987
integer_variable + float_variable # no need to worry about conversion

# can cast or convert variable to type
int(float_variable) == 3
float(integer_variable = 234.0)

Strings

  • Unicode text in Python3.
  • Strings can be defined using single or double quotes.
string_variable = "This is a string"

String Methods

string_variable = "This is a string"
string_variable.capitalise()
string_variable.replace("a", "b")
string_variable.isalpha()
string_variable.isdigit() # Useful if you need to convert to an int.
string_variable.split(",") # Useful for seperating data, i.e. csv row.  Return variable type?

String Format Function

  • Allows you to define the variables to display in the output at the end of the string.
  • Number is relative to the order of variables in the format arguments.
  • Numbers can be used multiple times.
name = "Dude"
other_name = "The Dudette"
"{0}, I'm the {1}".format(name, other_name)

String Interpolation

name = "Dude"
other_name = "The Dudette"
f"{name}, I'm the {other_name}"

Other options for the start of the string

Option Purpose
r Raw string. Don't escape characters such as slashes
u Unicode String

Boolean & None

  • True or False.
  • Start with a capital.
active = True
inactive = False
int(active) == 1
int(inactive) == 0
  • None is similar to null.
  • Variable is defined, but not populated with any data.
found = None

Lists

  • Python's implmentation of an array.
  • Can have multiple data types in a single list.
  • Probably best practise to contain to single types.

Population a list

teams = [] # empty python list
teams = ["888", "DJR Team Penske", "Erebus"] # populated list.
print(teams[0]) # Displays first item in the list
print(teams[-1]) # Displays last item in the list.
teams[1] = "WAU" # Updates the second (list starts at 0) item in the list.
teams.append("DJR Team Penske") # Add new item to the end of the list.

Searching a list

# Conditional can be used to search the list.
if "888" in teams:
    print("Found 888")

print(len(teams)) # Displays integer of how many items in list.
del teams[1] # Deletes second (list starts at 0) item in the list, moves everything else left.

Slicing a list

  • Doesn't modify the list.
  • Temporarily ignores items in the list.
print(teams[1:]) # Ignore the first element in the list.
print(teams[1:-1]) # Ignore the first and last element in the list.

Dictionaries

  • Key/Value pairs
  • Value updates and deletes work the same as lists.
# Define a key/value pair
team = {
    "name": "888",
    "bossman": "Rolly",
    "car": "ZB"
}
print(team["bossman"]) # Displays Rolly

# Define a list of key/value pairs.
multiple_teams = [
    {"name": "888", "bossman": "Rolly", "car": "ZB"},
    {"name": "Erebus", "bossman": "Betty", "car": "ZB"}
    {"name": "WAU", "bossman": "Ryan", "car": "ZB"}
    {"name": "DLR Team Penske", "bossman": "The Captain", "car": "Mustang"}
]
print(more_teams[1]["bossman"]) # Displays Betty

for team in multiple_teams:
    print(f"{team['name']} drive the {team['car']}")

# displays
888 drive the ZB
Erebus drive the ZB
WAU drive the ZB
DLR Team Penske drive the Mustang

# Display all the keys
print(multiple_teams[0].keys())

# Display all the values
print(multiple_teams[0].values())