How to Learn Python for Machine Learning

Last Updated on March 28, 2022

Python has become a de facto lingua franca for machine learning. It is not a difficult language to learn, but if you are not particularly familiar with the language, there are some tips that can help you learn faster or better.

In this post, you will discover what the right way to learn a programming language is and how to get help. After reading this post, you will know:

  • The right mentality to learn Python for use in machine learning
  • Good resources to learn Python
  • How to find answers for questions related to Python

Let’s get started.

How to Learn Python for Machine Learning
Photo by Federico Di Dio, some rights reserved.

How to Learn Python

There are many ways to learn a language, whether for natural languages like English or programming language like Python. Babies learn a language from listening and mimicking. Slowly, when they learn the pattern and some vocabulary, they can make up their own sentences. On the contrary, when college students learn Latin,  they probably start with grammar rules—singular and plural, indicative and subjunctive, nominative and accusative. Then they can build up to forming a sentence in Latin.

Similarly, when learning Python or any programming language, you can either read other people’s code, try to understand, and then modify from it. Or you can learn the language rules and build up a program from scratch. The latter would be beneficial if your ultimate goal is to work on the language, such as writing the Python interpreter. But usually, the former approach is faster to get some results.

My suggestion is to learn from examples first. But strengthen your foundation in understanding the language by revisiting the language rules from time to time. Let’s look at an example from Wikipedia:

This Python code is implementing the secant method to find a root for a function. If you are new to Python, you should look at the example and see how much you can understand. If you have prior knowledge from other programming languages, you would probably guess def defines a function. But if you do not, you might feel confused. If this is the case, it is best for you to start from a beginner’s book on programming to learn about the concept of functions, variables, loops, etc.

The next thing you might think you can do is modify the functions. For example, what if we are not using the secant method to find the root but instead use Newton’s method? You might guess how to modify the equation on line 4 to do it. What about the bisection method? You would need to add a statement of if f(x2)>0 to decide which way we should go. If we look at the function f_example, we see the symbol **. This is the exponent operator to mean $x$ to the power of 2 there. But should it be $x^2 – 612$ or $x^{2-612}$? You would need to go back and check the language manual to see the operator precedence hierarchy.

Therefore, even with a short example like this, you can learn a lot of language features. By learning from more examples, you can deduce the syntax, get used to the idiomatic way of coding, and do some work even if you cannot explain it in detail.

What to Avoid

If you decide to learn Python, it is inevitable you will want to learn from a book. Just picking up any beginner’s book on Python from your local library should work. But when you read, keep the bigger picture of your learning goal in mind. Do some exercises while you read, try out the codes from the book, and make up your own. It is not a bad idea to skip some pages. Reading a book cover to cover may not be the most efficient way to learn. You should prevent yourself from drilling too deep into a single topic because this will make you lose track of the bigger goal of using Python to do useful things. Topics such as multithreading, network sockets, and object-oriented programming can be treated as advanced topics for later.

Python is a language that is decoupled from its interpreter or compiler. Therefore, different interpreters may behave a bit differently. The standard interpreter from is CPython, also called the reference implementation. A common alternative is PyPy. Regardless of which one you use, you should learn with Python 3 rather than Python 2 as the latter is an obsolete dialect. But bear in mind that Python gained its momentum with Python 2, and you may still see quite a lot of Python 2 programming around.


Reading Resources

If you cannot go to the library to pick up a printed book, you can make use of some online resources instead. I would highly recommend beginners read The Python Tutorial. It is short but guides you through different aspects of the language. It lets you take a peek at what Python can do and how to do it.

After the tutorial, you probably should keep the Python Language Reference and the Python Library Reference handy. You will reference them from time to time to check the syntax and lookup function usages. Do not force yourself to remember every function.

Programming Environment

Python is built-in in macOS, but you may want to install a newer version. In Windows, it is common to see people using Anaconda instead of installing just the Python interpreter. But if you feel it is too much hassle to install an IDE and the Python programming environment, you might consider using Google Colab. This allows you to write Python programs in a “notebook” format. Indeed, many machine learning projects are developed in the Jupyter notebook as it allows us to quickly explore different approaches to a problem and visually verify the result.

You can also use an online shell at to try out a short snippet. The downside compared to the Google Colab is that you cannot save your work.

Asking for Help

When you start from an example you saw from a book and modify it, you might break the code, making it fail to run. It is especially true in machine learning examples, where you have many lines of code that cover data collection, preprocessing, building a model, training, validation, prediction, and finally presenting the result in a visualized manner. When you see an error result from your code, the first thing you need to do is pinpoint the few lines that caused the error. Try to check the output from each step to make sure it is in the correct format. Or try to roll back your code to see which change you made started to introduce errors.

It is important to make mistakes and learn from mistakes. When you try out syntax and learn this way, you should encounter error messages from time to time. If you try to make sense from it, then it will be easier to figure out what caused the error. Almost always, if the error comes from a library that you’re using, double confirm your syntax with the library’s documentation.

If you are still confused, try to search for it on the internet. If you’re using Google, one trick you can use is to put the entire error message in a pair of double quotes when you search. Or sometimes, searching  on StackOverflow might give you better answers.

Further Readings

Here I list out some pointers for a beginner. As referenced above, the Python Tutorial is a good start. This is especially true at the time of this writing when Python 3.9 rolled out recently and some new syntax was introduced. Printed books are usually not as updated as the official tutorial online.

There are many primer-level books for Python. Some short ones that I know of are:

For a bit more advanced learner, you may want to see more examples to get something done. A cookbook-style book might help a lot as you can learn not only the syntax and language tricks but also the different libraries that can get things done.


In this post, you learned how one should study Python and the resources that can help you start. A goal-oriented approach to study can help you get the result quicker. However, as always, you need to spend some significant time on it before you become proficient.

11 Responses to How to Learn Python for Machine Learning

  1. Wang Lei September 27, 2021 at 11:03 pm #

    This is a great idea to share your blogs with us because your Article helps us to learn so many things through your blogs. Thank you so much admin for uploading such amazing content with us your blog is really helpful for me. wish you all the best for upcoming comments.

  2. Łukasz Wojtów September 28, 2021 at 3:56 am #

    I’m not sure if that’s a right place to ask this. Moderators: please remove this post if it’s not.

    I’m thinking on building a web service where you can import data, train AI models, and later use these models to make predictions on unseen data. It would accept any kind data that can be expressed as numbers and an outcome (something useful to predict). This service would be simple enough to be used by non-programmers. Just point and click in the browser – no experience in data science required. The only requirement: bring your own data. Would anyone be interested in a commercial service like this?

    To give a personal example: I have a list of transactions made by a stock trading system and I trained a model to predict which trades are likely to be profitable. My initial tests show that a typical Donchian channel system can be improved from 8% to 59% average trade profit (that is, by removing some trades) and accuracy can be improved from 31% to 57%.

    • Adrian Tam September 28, 2021 at 9:47 am #

      I don’t think it is appropriate to judge the commercial value. But system like this is surely possible to create.

  3. ahmed September 28, 2021 at 10:58 am #

    in using weka, if the prediction results in indicators like R, MAE, RMSE shows that my ML classifier is better/worse than in the testing results (ex: we are comparing 3 classifiers) and didn’t keep the same order in the testing as in the prediction. what does that’s mean, I’m really confused about that? Please answer soon

    • Adrian Tam September 29, 2021 at 11:43 pm #

      How much difference is that? You cannot expect the numbers are exactly the same. What we don’t want to see is the numbers are far off from each other.

  4. Jai Dave October 30, 2021 at 12:46 am #

    Python is future. Thanks for great guide. Looking further for learning

  5. V cube December 24, 2021 at 10:41 pm #

    Thanks for creating a blog like this this is so informative i ned a blog like im bookmiarking your blog i dont want miss any article its really impressive

    • James Carmichael December 25, 2021 at 1:02 am #

      Thank you for your feedback and kind words! Please let us know if you have any specific questions regarding any of our mini-courses and/or e books.


  6. V cube December 24, 2021 at 10:43 pm #

    is python difficult to learn? which is the best institute to learn python?

    • James Carmichael December 25, 2021 at 1:01 am #

      Hi V cube…What are some projects for which you would like to apply Python? We at MachineLearningMastery feel confident that the “learn by doing” approach promote in our materials is a the fastest way to mastery.


  7. Jeetech Academy March 7, 2022 at 2:25 pm #

    Python is the most important language for advance learning. I got amazed after reading your blog. You covered each and every point in a well systematic manner. Today I get best points from your blog and going to apply in my life. Thanks for sharing best information with us.

Leave a Reply