Syntax Notes

  • Conditional statements are completed using the colon.
  • Code blocks are defined by indentation.

If Statements

  • Can use != or not for evaluations.
  • Can check for multiple conditions with and & or
cond = True
if cond:
    print("It is true!")
else:
    print("Negatory Captain")

age = 40
if age >= 41:
    print("Geez you're old")
else:
    print("Spring chicken.")

if age == 40 and cond:
    print("Multiple conditions")
else:
    print("Technically still multiple conditions, just didn't eval.")

Truthy & Falsy Values

  • Any number other than zero has a truthy value.
  • Any string that has a value, i.e. not empty, is truthy.
  • These will evaluate to True
  • Useful for evaluating lists, to determine if there are any items in the list.
  • Useful for determing that a value is defined, when you don't care about the value.
  • Work for boolean as well.
  • None evaluates the same as false.
number = 234
if not number:
    print("Number is not zero")

not_empty = "This is defo not empty"
if not_empty:
    print("This string has cred")

Ternary If Statements

x = 23
y = 43
print("Bigger") if x > y else print("Smaller")

Operators

Comparison

Operator Description
< Less than
<= Less than or equal too
== Equal
> Greater than
>= Greater than or equal too
!= Not equal
<> Not equal

Arithmetic

Operator Description Example
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponent
// Floor division

Loops

  • Two main loops

For Loops

teams = ["888", "DJR Team Penske", "Erebus"]
# Works like a foreach in Powershell.
for team in teams:
    print(f"Go the mighty {team}")
    print("Go the mighty {0}".format(team))
  • Specifying the number of iterations, i.e. when not working with a list.
# Print the index for 10 iterations.  Starts at 0
for index in range(10):
    print (index)

# Print the index for iterations from first arg (5) to second arg (10) in range function.
for index in range(5, 10):
    print (index)

# Print the index for iterations from first arg (5) to second arg (30) in range function, in increments of third arg (5)
for index in range(5, 30, 5):
    print(index)

Break & Continue

  • To exit the loop when a disired list item is found.
teams = ["DJR Team Penske", "Erebus", "888", "WAU"]
for team in teams:
    if team == "888":
        print(f"Go the mighty {team}")
        break
    print("Searching")
  • To skip an item in the list
  • Everything in the codeblock below continue wont run.
teams = ["DJR Team Penske", "Erebus", "888", "WAU"]
for team in teams:
    if team == "WAU":
        continue
        print(team) # This wont run.
    print(team)

for team in teams:
    if team != "WAU":
        print(team)

While Loops

  • Have to manually increment the index.
  • Check for a condition before executing the loop.
index = 0
while index != 10:
    print("Index: {0}".format(index))
    index += 1

Exceptions

  • try/except blocks
  • Provides graceful handling of errors.
  • Can handle unhandled exception handler.

Without exception handling.

team = {"name": "888", "bossman": "Rolly", "car": "ZB"}
print(team["driver"])

With Exception handling

team = {"name": "888", "bossman": "Rolly", "car": "ZB"}
try:
    print(team["driver"])
except KeyError:
    print("Error finding 'drive'")
    
print("Showing that the script continues.")

Chaining Exception handling

team = {"name": "888", "bossman": "Rolly", "car": "ZB", "driver": "J-Dub"}
try:
    goat = team["driver"]
    num_goat = 234 + goat
except KeyError:
    print("Error finding 'drive'")
except TypeError:
    print("Can add string to int")

print("Showing that the script continues.")

Catch all

  • Not Ideal in production.
  • Try and catch specific errors.
  • Useful for debugging.
team = {"name": "888", "bossman": "Rolly", "car": "ZB", "driver": "J-Dub"}

try:
    goat = team["driver"]
    num_goat = 234 + goat
except KeyError:
    print("Error finding 'drive'")
except TypeError:
    print("Can add string to int")
except Exception as error:
    print("Unknown Error")
    print(error) # Displays some error details.

print("Showing that the script continues.")