Geeks logo

Ways to Speed Up Your Python Code

30-day roadmap to learn Python up to an intermediate level

By Bahati MulishiPublished about a year ago 4 min read

10 Ways to Speed Up Your Python Code

1. List Comprehensions

numbers = [x**2 for x in range(100000) if x % 2 == 0]

instead of

numbers = []

for x in range(100000):

if x % 2 == 0:

numbers.append(x**2)

2. Use the Built-In Functions

Many of Python’s built-in functions are written in C, which makes them much faster than a pure python solution.

3. Function Calls Are Expensive

Function calls are expensive in Python. While it is often good practice to separate code into functions, there are times where you should be cautious about calling functions from inside of a loop. It is better to iterate inside a function than to iterate and call a function each iteration.

4. Lazy Module Importing

If you want to use the time.sleep() function in your code, you don't necessarily need to import the entire time package. Instead, you can just do from time import sleep and avoid the overhead of loading basically everything.

5. Take Advantage of Numpy

Numpy is a highly optimized library built with C. It is almost always faster to offload complex math to Numpy rather than relying on the Python interpreter.

6. Try Multiprocessing

Multiprocessing can bring large performance increases to a Python script, but it can be difficult to implement properly compared to other methods mentioned in this post.

7. Be Careful with Bulky Libraries

One of the advantages Python has over other programming languages is the rich selection of third-party libraries available to developers. But, what we may not always consider is the size of the library we are using as a dependency, which could actually decrease the performance of your Python code.

8. Avoid Global Variables

Python is slightly faster at retrieving local variables than global ones. It is simply best to avoid global variables when possible.

9. Try Multiple Solutions

Being able to solve a problem in multiple ways is nice. But, there is often a solution that is faster than the rest and sometimes it comes down to just using a different method or data structure.

10. Think About Your Data Structures

Searching a dictionary or set is insanely fast, but lists take time proportional to the length of the list. However, sets and dictionaries do not maintain order. If you care about the order of your data, you can’t make use of dictionaries or sets.

If you aspire to work in top product companies, here’s my advice:

👉 For SDE-1 or SWE positions, focus on:

✔️ Continuously upskilling and improving your abilities.

✔️ Developing strong problem-solving skills.

✔️Mastering DSA – trust me, you’ll be tested on it, so aim to excel.

Also, learn how to design scalable systems and understand how to build solutions that can handle growth in users and data.

👉 For higher-level roles (SDE-2 and SDE-3), focus on:

✔️ DSA + System Design (both LLD and HLD).

✔️ Building your leadership skills, as you’ll need to lead teams and projects.

🔸I know it’s challenging to do this while working full-time, but you’ll need to carve out time to consistently upskill yourself.

Remember, your learning plan should be sensible and well-organized.

30-day roadmap to learn Python up to an intermediate level

Week 1: Python Basics

*Day 1-2:*

- Learn about Python, its syntax, and how to install Python on your computer.

- Write your first "Hello, World!" program.

- Understand variables and data types (integers, floats, strings).

*Day 3-4:*

- Explore basic operations (arithmetic, string concatenation).

- Learn about user input and how to use the input() function.

- Practice creating and using variables.

*Day 5-7:*

- Dive into control flow with if statements, else statements, and loops (for and while).

- Work on simple programs that involve conditions and loops.

Week 2: Functions and Modules

*Day 8-9:*

- Study functions and how to define your own functions using def.

- Learn about function arguments and return values.

*Day 10-12:*

- Explore built-in functions and libraries (e.g., len(), random, math).

- Understand how to import modules and use their functions.

*Day 13-14:*

- Practice writing functions for common tasks.

- Create a small project that utilizes functions and modules.

Week 3: Data Structures

*Day 15-17:*

- Learn about lists and their operations (slicing, appending, removing).

- Understand how to work with lists of different data types.

*Day 18-19:*

- Study dictionaries and their key-value pairs.

- Practice manipulating dictionary data.

*Day 20-21:*

- Explore tuples and sets.

- Understand when and how to use each data structure.

Week 4: Intermediate Topics

*Day 22-23:*

- Study file handling and how to read/write files in Python.

- Work on projects involving file operations.

*Day 24-26:*

- Learn about exceptions and error handling.

- Explore object-oriented programming (classes and objects).

*Day 27-28:*

- Dive into more advanced topics like list comprehensions and generators.

- Study Python's built-in libraries for web development (e.g., requests).

*Day 29-30:*

- Explore additional libraries and frameworks relevant to your interests (e.g., NumPy for data analysis, Flask for web development, or Pygame for game development).

- Work on a more complex project that combines your knowledge from the past weeks.

Throughout the 30 days, practice coding daily, and don't hesitate to explore Python's documentation and online resources for additional help. Learning Python is a dynamic process, so adapt the roadmap based on your progress and interests.

how tolist

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.