Use superpowers wisely: Three things everyone should learn about Python
by Ruslan Botnar
There are many uses for Python, but some are particularly good at it. Let's figure out what can be done in this language. If you are going to learn Python or have just started learning it, you wonder exactly what you can do with it. The question is not easy, as this language is used in many areas.
Rating of programming languages
TIOBE has published an updated ranking of programming languages. This rating is updated once a month, allowing you to monitor the dynamics of various programming languages' popularity. The data used for the assessment is collected worldwide.
To build the final ranking, authors use search engines, including Google, Bing, Yahoo !, Wikipedia, Amazon, YouTube, and Baidu. It should be noted that this rating should by no means be used to analyze the quality of the language. It only represents a rough estimate of the popularity and relevance of specific programming languages around the world.
Based on the published data, business representatives can judge which language to choose for a particular company.
So, at the moment, the first, places in the rating are occupied by Java, C, and Python. The latter is gradually becoming more in demand. Interesting. That even though Python dropped to third in the rankings, this does not mean that developers began to abandon it. The graph shows that in the early 2000s it was much more popular, then the trend of its use began to decline, and only now it began to recover. But Python is breaking records of its popularity.
Three wishes for a genie
There are three most popular uses for Python:
- Web development;
- Data science: machine learning, data analysis, and visualization;
- Process automation.
Each of them deserves separate consideration.
Web development with Python
Recently, Python frameworks such as Django and Flask have become very popular in web development. They facilitate the process of writing server-side code in Python. This code runs on the server, not on users' devices and browsers (front-end code). Suppose you are not familiar with the differences between backend and front-end development. In that case, you will be interested in the note at the end of the article.
Why do you need a web framework? Frameworks make it easy and quick to create basic backend logic. It includes mapping different URLs to pieces of Python code, working with databases, and creating HTML views for display on user devices. Which Python framework should you choose? Django and Flask are two of the most popular web frameworks built for the Python language. A beginner should choose one of them.
The main differences are:
- Flask provides simplicity, flexibility, and complete control over your project. It allows the user to decide how to implement certain things independently.
- Django is an all-inclusive service. Out of the box, it already has an admin panel, database interfaces, ORM (object-relational mapping), and a directory structure for your projects.
Choose Flask for more experience and learning opportunities. Or if you need maximum control over all the components used, for example, databases.
Choose Django if you are interested in the final product. Especially if you are working with simple applications such as a news site, store, blog and want each task to be solved in one obvious way.
In other words, Flask is arguably the best choice for a beginner developer as it contains fewer components. Besides, it is worth choosing if you need to fine-tune the project. Flask, due to its flexibility, is better suited for building REST APIs. On the other hand, if you're looking to get things done quickly and easily, Django is probably the best choice.
Data Science: Machine Learning, Data Analysis, and Visualization
First of all, you need to understand what machine learning is. Suppose you want to develop a program that will automatically detect what is shown in the picture. For example, by offering her this image, you want the program to recognize that the image is of the dog.
And then you want for the program to recognize that the dog should see a table.
You might be thinking that you can write an image analysis code to solve this problem. For example, if there are many light brown pixels in the picture, we conclude that this is a dog. Or you can learn to define edges and boundaries in an image. Then a picture with a lot of straight borders is likely to be a table. However, this is a rather complicated and ill-conceived approach. What if the photo shows a white dog without brown spots? Or if the picture is a round table?
This is where machine learning comes into play. It usually implements an algorithm that automatically detects a familiar pattern among the input data. You can offer a machine learning algorithm, say, 1000 images of a dog and 1000 pictures of tables. It will learn the difference between these objects. When you give it a new image of a table or a dog, it can determine what exactly is depicted on it.
This is very similar to how small children learn. How exactly do they know that one thing is like a table and the other is like a dog? From a large number of examples. After all, you do not give a child clear instructions: "If something is fluffy and light brown, then it is a dog." On the contrary, you say, "This is a dog. This is also a dog. And this. And this is a table. And this is also a table." Machine learning algorithms basically work similarly.
This technology can be applied:
- Referral services (think YouTube, Amazon, and Netflix)
- Face and voice recognition systems.
Some of the most popular machine learning algorithms you've probably heard of are:
- Neural networks;
- Deep learning;
- Support vector machine;
- Random forest.
Any of the above algorithms can be used to solve the problem with dogs and tables in images.
Ways to Use Python for Machine Learning
There are various libraries and frameworks for machine learning in Python. The two most popular are Scikit-learn and TensorFlow. Scikit-learn has several popular learning algorithms built-in out of the box. TensorFlow is a lower-level library. It allows you to create custom algorithms. Machine learning beginners are better off starting with Scikit-learn. For more experienced developers who are struggling with efficiency issues, TensorFlow should be considered.
How do you learn machine learning?
Courses from Stanford or Caltech (California Institute of Technology) are perfect for introducing the basics of the subject. We should also note that basic knowledge of mathematical analysis and linear algebra is required to understand the material.
Then you can move on to practice on the Kaggle platform. It is a site where data science researchers create various machine learning algorithms to solve real-world problems. The winners receive substantial cash prizes. They also have great beginner tutorials.
Data analysis and visualization with Python
To understand what this is about, you should turn to a simple example. Let's say you are a data analyst for a company that sells products over the Internet. You can get a graph like this:
From this graph, you can understand that this Sunday man bought more than 400 units of the product, and women - about 300. Your task as an analyst is to come up with several possible explanations for this difference.
One of the apparent options is that this product is more popular with men than women. Another explanation may be related to the too-small sample size, which led to unreliable results. The third option is that men tend to buy the product on Sundays for whatever reason. To figure out what's wrong, you can look at the data for the whole week and create a new graph.
As you can see on the diagram, the difference is relatively stable and manifests itself not only on Sundays. We can conclude that the most convincing explanation is the fundamentally great interest of men in this product. On the other hand, a weekly chart might look different. How can you explain the big difference in sales on Sunday? You might assume that men tend to buy more at the end of the week. Or it might just be a coincidence. This is a simplified example of what accurate data analysis looks like.
How to use Python for data analysis and visualization
One of the most popular rendering libraries is Mathplotlib. Beginners should start learning with it for two reasons:
- Low threshold of entry;
- Mastering mathplotlib will allow in the future to quickly understand more complex libraries based on it, for example, seaborn.
One of Python's most popular uses is writing small scripts to automate various work steps and processes. An example is the email processing system. To collect statistics and analyze data, you need to count the number of incoming emails containing specific keywords. This can be done manually, or you can write a simple script that will calculate everything by itself. There are several reasons for using Python for automation tasks:
- Simple syntax for fast scripting
- Ease of debugging since the code is not compiled before running.
- Built-in applications
- Python is the most popular programming language for Raspberry Pi.
Backend and front-end code
Let's say you want to do something that looks like Instagram. You need to create a front-end code for each device type that needs to be supported. For this, different programming languages can be used, for example:
- Swift for iOS;
- Java for Android;
Each type of device will run its own set of codes. It will determine the format of the application, its appearance, etc. However, you do need to store personal data and photographs. You want to use your server for this, not the users' devices so that subscribers can view each other's photos. To solve this problem, you need a backend code (server-side). It will perform the following operations:
- Track friendships and subscriptions;
- Compress photos so that they take up less storage space;
- Analyze requests and issue recommendations to each user.
This is the difference between front-end and backend code.
Why Python is good for web development?
Suppose we want to make a website or a service. In that case, we use a combined approach: we have some kind of back-end that implements the logic and creates web pages, and some front-end draws these web pages in the browser. And when we need to collect all this on something quickly, then there is not much choice. Let's take a look at the possible options.
C #. Microsoft is great. They made .NET Core and are promoting it in every possible way. But, firstly, this is a new cross-platform technology, and not everything is smooth there yet. Secondly, it is very costly, and there are few C # developers - simply because it is unpopular. Java. It's too complicated. Making an average website in Java is not ten lines of code like in Python. This is a lot of code, these are frameworks, and you need to know the specifics of configuring Java servers. In general, in the end, all you get pain and suffering.
- PHP. In the latest versions, it's great. I'll even say this: PHP 7.2 is not worse than Python. But you can't just go and use PHP 7.2. Suppose an ordinary, non-top developer makes a PHP website. In that case, he will not write only in 7.2: you still have to read some textbooks, tutorials, there is a lot of legacy code everywhere, and this is not very good.
- Let's skip Ruby, Haskell, Erlang, and other niche stuff, and we're left with Python. A language with a consistent syntax, uniform standard library, better documentation, popular lightweight frameworks, a mega-popular Django harvester. It turns out that, despite the broadest choice, if you hire ordinary, non-top developers, you will get a "typical" website unless your ordinary developers use Python. Thus making Python into a superpower, capable of turning an average web developer into an "extraordinary website maker."