How to practice Python — a few useful tips
This article was originally published at: https://www.blog.duomly.com/how-to-practice-python-skills/
Learning Python doesn’t differ much from learning other programming languages. One path to becoming proficient in Python (or any other language) mostly depends on the existing programming knowledge and experience. This means that experienced programmers — already familiar with main programming concepts — usually use different strategies than novices.
However, there are few things in common for all, and one of them is that you need to practice a lot!
Fortunately, Python has a wide, friendly, and dedicated community. It’s open-source and provides several environments that really facilitate development.
There is much good advice on how to practice Python. This article focuses on these 10:
- Choose the proper environment
- Make sure you have strong fundamentals
- Write and improve code
- Read documentation
- Build upon the basics
- Catch standards, advice, tips, and tricks
- Analyze source code
- Get interested in libraries
- Become a part of the community
- Learn a second programming language
Choose a Proper Environment
For the beginning, you should be fine with a regular Python 3 interpreter and package manager on whatever operating system you use. Windows, Mac OS, or any Linux distro will do the job. (Even Android with QPython, although its capabilities are limited compared to the original Python.) Later, you can install Anaconda on Windows, Mac OS, or Linux. It contains a Python interpreter, Conda package, dependency, and environment management system, as well as many third-party packages you might find useful. You should choose an appropriate IDE (integrated development environment). Many general-purpose IDEs like Visual Studio, Visual Studio Code, Emacs, Vim Sublime Text, or Atom have really good support for Python. If you like the products of JetBrains, you can use PyCharm. Spyder is a convenient Python-only IDE included in Anaconda. IPython and Jupyter are really great to begin with since they offer nice interactive functionalities. Finally, there’s Python Console, but you might find it more useful to check some short code quickly rather write full programs. You can check this article to learn more about Python IDEs. Another good idea is to consider installing and customizing Python linters like Pylint or Pyflakes. They are small packages that highlight issues in your code related to syntax or style.
Make Sure You Have Strong Fundamentals
If you want to practice any programming language, you should learn basics well so you can build upon them. This means that you need to know at least basic syntax, understand main programming concepts, get familiar with the built-in types and data structures, and so on.
In Python, you should check if you understand the if clause, while and for loops, functions, integers, floats, strings, tuples, sets, lists, dictionaries, etc. Some other types like complex numbers, named tuples, frozen sets might be helpful as well.
You should find a suitable Python book or even maybe tutorial to get started. Duomly offers the Python tutorial you can use to learn these concepts and much more. There’s also the official Python tutorial simply called The Python Tutorial.
Write and Improve Code
Writing a lot of code on yourself is an essential part of learning programming languages.
Start with the code from books and tutorials. Then try to modify it, make it more general, or suitable for some other purpose. After that, try to combine what you’ve learned into small but meaningful programs.
However, remember one of the most important things about programming: it’s not about typing, it’s about thinking!
You’ll make errors. All programmers do. That’s not bad at all. What’s very important is to try to figure out what went wrong, that is to learn from your own mistakes. Every time you get and fix an error, you’ll become a slightly better programmer than you’ve been before.
Sometimes, it’s beneficial to get back to the old code of yours and try to improve it. Hopefully, you’ll notice that you’re much better than before when you wrote the original code.
Think of the problems you’d like to solve. Ideally, these are the issues you’re passionate about. Emotional involvement usually improves the results. Define your small and mid-size projects and try to work on them often. As soon as you learn something new, ask yourself how you can apply it to some problem you’re interesting in solving.
The documentation is critical in Python. You should make a habit to read it often. Ideally, before you apply some existing function, or method, you should check the corresponding documentation.
Fortunately, Python built-in libraries and the most popular third-party packages usually come with comprehensive documentation available on their web sites and documents you can download. You can also get the documentation of a Python entity (class, method, function, and so on) programmatically by calling the attribute .__doc__:
slice(start, stop[, step])
Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).
The documentation can contain more details in some cases.
You can learn a lot from the documentation. You can figure out how to use a function, method or class, what arguments to provide, what return values to expect, etc. It often contains related examples that can be quite informative and sometimes enlightening.
Build upon the Basics
Once, you’re OK with the basics, you can start expanding your knowledge.
Don’t worry, you’re not going to forget the fundamentals. You’ll need them all the time.
The topics like exception handling, packing and unpacking, *args and **kwargs, function decorators, modules and packages, object-oriented programming, and generators are often used and should be well understood and heavily practiced.
There are more advanced topics to cover like special methods, coroutines, asynchronous programming, multithreading and multiprocessing, regular expressions, unit testing, and so on. It’s likely that you’re not going to need all of them at the beginning. So you can start with what seems useful. For example, if you need some hard-core strings manipulation, you can tackle regular expressions. If you’re working on some large-scale scientific computing project, you might find multiprocessing interesting.
Catch Advises, Standards, Tips, and Tricks
There are many Python-specific features of the language, and learning them isn’t a trivial task. Luckily, there are some resources that sublime many of them.
The official Python documentation contains much information. The PEP 20, also called The Zen of Python by T. Peters, defines the guiding principles of Python:
>>> import this
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.
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.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
The PEP 8 or Style Guide for Python Code by G. van Rossum, B. Warsaw, and N. Coghlan brings the stylistic guide for Python code. It’s often beneficial to read it and try to stick to it as much as possible. It might improve the readability of your code and facilitate you and other developers maintaining it.
Similarly, the PEP 257, that is Docstring Conventions by D. Goodger and G. van Rossum explains how to write the documentation that can later be obtained with the .__doc__ attribute.
The books The Little Book of Python Anti-Patterns by A. Dewes and C. Neumann (free and available online), as well as Python Tricks: A Buffet of Awesome Python Features by D. Bader offer a lot of information on how to write clean, efficient, maintainable, and Pythonic code.
Analyze Source Code
Python is open-source, as well as most of its popular libraries. This means that you can access and read source code. It’s often available on GitHub, but there are other places where you can find the source as well.
Reading and understanding code for a library of interest enables better insight into its capabilities and implementation details.
In addition, you can learn a lot by analyzing the source written by other (hopefully good) programmers. You can get ideas, see their ways of implementation, learn new patterns and tricks, and so on.
Get Interested Into Libraries
Python comes with a lot of useful libraries for different purposes: regular expressions, math, statistics, random numbers generation, unit testing, iterating, functional programming, multithreading and multiprocessing, defining abstract classes, and many more. There are some very useful third-party libraries from many different fields of science and technology.
Obviously, you can’t learn how to use them all, but you can focus on a few of them that look interesting for the area you want to specialize in.
If you want to be a data scientist or machine learning practitioner, you’ll need to start with NumPy, which is a fundamental library for handling single- and multi-dimensional arrays in an efficient and concise way. It’s fast and enables array operations without explicitly writing Python for loops:
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
array([1, 2, 3, 4, 5])
>>> b = 2**a
array([ 2, 4, 8, 16, 32])
>>> a + b
array([ 3, 6, 11, 20, 37])
>>> b / 2
array([ 1., 2., 4., 8., 16.])
NumPy provides many functions to manipulate arrays. It also has a limited number of routines for linear algebra, statistics, random number generation, and more.
SciPy is a scientific computing library built on top of NumPy that contains additional routines for linear algebra and statistics, but also for optimization, interpolation, integration, using special functions, Fourier transformations, and more.
Pandas is one of the most popular libraries generally. It’s also built on top of NumPy and works well with NumPy and SciPy. It enables working with labeled one- and two-dimensional data.
Scikit-learn is a fundamental library for machine learning with many algorithms that share a consistent API (application programming interface). TensorFlow, Theano, Pytoch, and Keras are used for artificial neural networks.
Matplotlib and Bokeh are good options for data visualization.
All these libraries have very good documentation. You can also learn about some of them in the Duomly machine learning tutorial.
If you want to deal with web development, you can learn and practice working with some of the Python web frameworks for the back-end. The most popular one is Django that comes with most required parts included. It’s very convenient for large web applications with usual requirements. The other is Flask — a powerful, flexible, and handy microframework with many extensions. Both Django and Flask are among the most popular web frameworks in general.
Python has other web frameworks like Pyramid, Bottle, Tornado, and so on.
SQLAlchemy is a package that enables working with databases in an object-oriented way. It’s heavily used by web frameworks, but also in data science.
Become a Part of the Community
As already said, Python has a large, dedicated, and friendly community. You can become a part of it. Read posts, comment, ask questions, search for explanations, etc.
Once you have a sufficient level of knowledge, you can start contributing either as a developer on open-source projects or as someone who writes interesting posts and tutorials. This contribution is highly valued by the community and by many potential employers.
Learn A Second Programming Language
Python is a general-purpose programming language, and in many situations, it can fulfill ones requirements completely.
However, it’s always beneficial to learn other programming languages. Like this, you can deepen your understanding of general programming paradigms and broaden your horizons. Once you know one language, learning the other is easier. Most of the good programmers know several programming languages.
Examples of Projects for Practice
There are many potential small projects you can use to practice Python.
For example, you can try to automate boring tasks. Knowing strings, regular expressions, and eventually, a templating library like Jinja can help you effectively manipulate text files: searching, matching, replacing, or comparing files.
If you often work with Microsoft Office Excel, you can try to use XLWings, NumPy, and Pandas to speed up the calculations.
You can use Python to create games. For example, you can apply the library randomly and try to simulate rolling dice or working with cards. If you want to build a nice graphical user interface, you can use the libraries like PyQt or Tkinter. Some of the usual applications for beginners are creating the calculator (more options is better) or the well-known game called Tic Tac Toe.
If you like to jump to web development, you can give Flask a try. It requires only five lines of code to get the most basic but functional web app. The official Flask web site contains excellent documentation and tutorial you can use to learn this great framework.
Duomly machine learning tutorial has the instructions you can use to practice data science and machine learning applications. If you want to find and analyze some cool datasets, check the article 15 Best Free Datasets for Machine Learning.
Artificial intelligence is an area where Python is widely applied. You can play around with image recognition, natural language processing, or web scraping. To find more information on how you can start performing these demanding but interesting tasks easily, you can check:
You’ve just read a few advises how to practice Python. Hopefully, they’ll help you to become Python programmer. Remember to keep coding, write interesting programs, try to learn from errors, and engage as a part of the community. Finally, always have in mind that programming is not about typing. It’s all about thinking.
Thank you for reading.
This article was provided by our teammate Mirko.