Some useful tips for practicing Python

Hello, Habr! Today I offer you a translation of a Duomly article on the study and practice of programming in Python.







Introduction



Learning Python is not much different than learning other programming languages. One way to become a professional in Python (or in any other language) mainly depends on your experience and knowledge. This means that experienced programmers are already familiar with the basic concepts of programming, while ordinary ones use different methods for solving problems, when they are not newcomers.







No matter what, there are several things that are common to all, one of them is that you need to practice, practice a lot!







Fortunately, Python has a wide, friendly, and communicative community. These are Open-Source projects that really help in development.







There are many good tips on how to practice Python. This article will cover only 10 of them:









Choose the right environment



To get started, you should use regular Python 3 and the package manager on the operating system you are using. Windows, Mac OS, or any Linux distribution is suitable for this. (Even Android with QPython, although the possibilities are limited compared to the original Python). You can later install Anaconda on Windows, Mac OS, or Linux. It contains a Python interpreter, a Conda package, dependencies, and a package manager. With it, you can find many third-party libraries that may seem useful to you.







You must select the appropriate IDE (integrated development environment). Most popular IDEs such as Visual Studio, VSCode, Emacs, Vim, Sublime Text, etc. have really good support for Python. If you like JetBrains products, you can try using PyCharm.

Also included in the Anaconda distribution package is an IDE called Spyder.







IPython and Jupyter Notebook are good tools that offer convenient interactive functionality. There is also an interactive Python interpreter, but you may find it more useful for testing simple pieces of code than writing large programs.







You can also check out this article to learn more about the Python IDE.

Among other things, it is a good idea to install and configure linters for Python (Pylint, flake8 and others). They are small packages that simply highlight the problems of your code (syntax, or related to non-compliance with the programming style in Python - PEP8).







Make sure you have a sufficient base to learn more complex things.



If you want to learn any programming language, you need to study well to learn the basics, on top of which you will build your career in this language. This means that you need to at least know: the basic syntax of the language, understand the basic concepts of programming, be familiar with standard types and data structures, and so on.







In Python, you should make sure that you understand conditional constructs ( if



, else,



elif



), loops ( for



, while



), functions, numbers, strings, tuples, sets, lists, dictionaries, and so on. Some other data types, such as complex numbers, named tuples, immutable sets, can also be useful.







You should find the right book for your level about Python.

Duomly offers a Python tutorial that you can use to learn these concepts and more.

You can also start with a tutorial on the official website.







Write and improve code



Writing a lot of code for yourself is an integral part of learning programming languages.

Start with code from books and tutorials. Then try to modify it, for example, to simplify it, or more suitable for some purpose. After that, try writing your own small but useful programs.

But most importantly, remember one of the most important things about programming: it's not about typing code, it's about understanding!







You will make mistakes. All programmers do them. And this is not bad at all. The most important thing in this is to find out what went wrong and of course not to repeat these mistakes in the future. Each time you receive and correct a bug, you become a slightly better programmer than you were before.

Sometimes, it’s important to go back to the old code and try to improve it. I hope you see where you became better than when you wrote this code.







Think about the problems you would like to solve. Ideally, these are the issues that you are passionate about. Emotional investing usually improves results. Start your small to medium sized projects and try to improve them as often as possible. Once you learn something new, ask yourself how you can apply it to the problem you want to solve.







Read the documentation



Documentation is very important in Python. You should make it a habit to read it often. It will be ideal, before using any existing function, or method, to read the documentation about it.







Fortunately, the standard Python library and most of the popular third-party libraries have good, detailed documentation available on their websites. You can also get documentation about the Python object (class, method, etc) programmatically using the .doc attribute:







 >>> print(slice.__doc__) slice(stop) slice(start, stop[, step]) Create a slice object. This is used for extended slicing (eg a[0:10:2]).
      
      





The documentation may contain more details at some points.







You can learn a lot from the documentation. You can see how to use a function, method or class, what arguments you need to pass, which will return the function call, and so on. It also often contains related examples that may be more informative and sometimes instructive.







Learn more complicated things after the basics.



Once you understand the basics, you can begin to learn more complex things. Do not worry, you will not forget the basics. You will always need them.







Topics such as exception handling, unpacking and packing arguments, * args and ** kwargs, decorators, modules and packages, OOP and generators are often used and you need to know and understand them at a good level.

There are also many advanced topics that you need to study. For example, magic methods, coroutines, asynchronous programming, multithreading, multiprocessing, testing, and so on. This is not what you need at the beginning. So, you can start with something simpler and more useful. For example, you need a powerful tool for manipulating strings, you can try regular expressions. If you are working on a science project, you will probably find multiprocessing interesting for you.







Follow Python Standards, Tips, and Tricks



There are many specific things in programming specifically for the Python language that are not the easiest task to learn. It is fortunate that there are many resources that cover many of these topics.







The official Python documentation contains a lot of information. PEP 20, also called Zen Zen by Tim Peters, denotes the basic principles of Python:







 >>> import this The Zen of Python, by Tim Peters  ,  .  ,  .  ,  .  ,  .  ,  .  ,  .   .     ,   .     .     .     .  ,   .    , ,      .        ,    .  ,  .    ,   .     β€”  .     β€” , , .   β€”  !    !
      
      





PEP 8, or Code Writing Guide by Guido Van Rossum, B. Warsaw.

Also PEP 257, which denotes conventions for documentation lines.







Analyze the source code



Python is an open-source programming language, just like most popular libraries. This means that you can read the source code. This is often possible on Github, but there are other places where you can do this.

Reading and understanding the library code allows you to better understand how it works.







In addition, you can learn a lot by analyzing the source code of other (hopefully good) programmers. You can borrow ideas, look at different implementation paths, learn new patterns, and so on.







Be interested in libraries



Python has many useful libraries for different tasks:

regular expressions, mathematics, statistics, random number generation, testing, iteration, functional programming, multithreading and multiprocessing, abstract classes and much more.







There are also some really useful third-party libraries for many different scientific tasks.

Obviously, you cannot learn how to use them all, but you can focus on a few of them that seem more interesting to you for your specialization.







If you want to be a Scientist or engage in Machine Learning, you need to start with NumPy, which is the founding library, to manipulate one- and many-dimensional arrays in an efficient and easy way. It is fast and includes many array operations, without explicitly writing for



loops from Python.







 >>> import numpy as np >>> >>> a = np.array([1, 2, 3, 4, 5]) >>> a array([1, 2, 3, 4, 5]) >>> b = 2**a >>> b array([ 2, 4, 8, 16, 32]) >>> a + b array([ 3, 6, 11, 20, 37]) >>> b / 2 array([ 1., 2., 4., 8., 16.])
      
      







All of these libraries have excellent documentation.







If you want to do web programming, you can learn and practice working with some Python backend frameworks. The most popular of them - Django, has most of the necessary features. It is very convenient for large web applications. Flask, on the other hand, is a powerful, flexible micro-framework with many add-ons. Django and Flask are the most popular Python web frameworks.







Python also has other web frameworks like Pyramid, Bottle, Tornado and so on.







SQLAlchemy is a package that includes the ability to work with databases in an object-oriented style. It is often used in web frameworks, but also in Data Science.







Become a part of the community



As already mentioned, Python has a large and friendly community. You can become part of it. Read publications, comment, ask, seek explanations.







When you have a sufficient level of knowledge, you can start investing your knowledge in open-source projects, or help those who write articles or tutorials. These things are greatly appreciated by the community and most potential employers.







Learn a second programming language



Python is a multi-paradigm programming language, and in many situations, it is enough.

But no matter what, you always need to look for advantages in learning other languages.

With this, you can improve your knowledge in programming in general, paradigms and expand your horizons. When you learn one language, learning others will be easier. Most good programmers know several languages.







If you want to be a web programmer, you will probably need to learn JavaScript. Learning C is good for a better understanding of basic programming concepts, but you can also write very fast Python functions on it. Rust is a new and very cool language that already has good integration with Python







Examples of projects for practice



There are many small projects you can use to learn Python. For example, try automating boring tasks. Learning strings, regular expressions, and even libraries for Jinja patterns can help you write more efficient code.









The official Flask website contains great documentation and a tutorial that you can use to study it.







The Doumly Machine Learning tutorial has instructions on how you can practice Data Science and machine learning.







Conclusion



You have read some tips on how to practice Python. I hope they help you become a Python programmer. Remember that you need to program a lot, write interesting programs, try to learn from mistakes, and of course, become part of the community.







Happy coding!








All Articles