Welcome!

Writing Efficient Python Code

Logan Thomas

Scientific Software Technical Trainer, Enthought

Course overview

  • Your code should be a tool used to gain insights
    • Not something that leaves you waiting for results
  • In this course, you will learn:
    • How to write clean, fast, and efficient Python code
    • How to profile your code for bottlenecks
    • How to eliminate bottlenecks and bad design patterns
Writing Efficient Python Code

Defining efficient

  • Writing efficient Python code
    • Minimal completion time (fast runtime)
    • Minimal resource consumption (small memory footprint)

alt="Equation with a stopwatch next to an arrow pointing down plus a memory stick next to an arrow pointing down equalling the word efficient"

Writing Efficient Python Code

Defining Pythonic

  • Writing efficient Python code
    • Focus on readability
    • Using Python's constructs as intended (i.e., Pythonic)

# Non-Pythonic
doubled_numbers = []

for i in range(len(numbers)):
    doubled_numbers.append(numbers[i] * 2)

# Pythonic doubled_numbers = [x * 2 for x in numbers]
Writing Efficient Python Code

The Zen of Python by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
...
Writing Efficient Python Code

Things you should know

Writing Efficient Python Code

Let's get started!

Writing Efficient Python Code

Preparing Video For Download...