What exactly can you do with Python?
When I told a fellow developer I was writing an article about the applications of Python, he told me: `It would be easier to list areas where Python doesn’t apply`. I can’t disagree with that. Python is used so widely nowadays, you can find it almost everywhere and that makes it a really convenient language to learn. Let’s have a look at the popular (and some a bit less popular, but interesting) applications of Python.
Machine Learning and Data Analysis
Depending on whom you ask you may see these two separated… or not. From a purpose standpoint it makes sense to separate Machine Learning and Data Analysis, but when talking strictly about technology, they share a lot of the stack. Given the fact that the base is the same, I’ll put Machine Learning and Data Analysis in one shared basket, but be warned: it’s a very, very large basket.
Python for Data Analysis
Let’s start with the core of working with data: reading, writing and manipulation. There are two libraries you need to know to even get started: Numpy and Pandas.
Numpy stands for something like numerical Python and Pandas stands for… OK, I have no idea. It probably has to do with Python, analytics, data structures and stuff. Doesn’t matter really, because Pandas just does the work.
But what work exactly? It introduces Pandas DataFrames - table-like collections of data and allows you to manipulate them in many different ways. Combined with Numpy, they are almost the ultimate duo for data cleaning, wrangling and manipulation.
Then there comes data visualization. Python offers quite a few really good libraries here, but let’s be honest: Matplotlib is the gold standard for visualization in data analysis. It’s easy to use and works well with Numpy and Pandas. If you need nice-looking charts with no additional effort, Seaborn will probably be your tool of choice.
Your choice is not limited to Matplotlib and Seaborn though: if you come from R you’ll probably find ggplot handy. There are also multiple other packages like Bokeh, Plotly or Geoplotlib.
Python for Machine Learning
Let’s now take a leap into pure machine learning, as that’s usually the next step in our data-driven journey. Python offers a lot there, no matter what you want to achieve with your data.
I would take Scikit-Learn as the primary tool here - it’s packed with most of the popular Machine Learning techniques and algorithms, providing an easy to use, unified API for all of them.
If you’d rather go for neural networks and deep learning, try Keras or TensorFlow. These are very flexible packages that allow you to use pre-trained models or to take a deep dive into the world of computational layers, or - if you wish to - you can configure your neural network from scratch.
OK, let’s stop here with Data Analysis and Machine Learning. There’s one more question to answer though: what are the alternatives to Python?
The most common is R, an environment for statistical computing and visualization. Although R is easy to start with and gets a lot of love from Microsoft, it also has messy syntax (unlike Python) and is utterly slow. There’s also Matlab (with its open source clone: Octave), but its focus is mainly on academic research and I wouldn’t treat it as production-ready.
Developing web applications in Python
Do you need a low-cost prototype web service? Or maybe you’ve committed to delivering a webpage next week and you haven’t even started? No worries, Python is here to help you. With Django, Flask or Pyramid you can build any type of web service you want, limited only by the effort and time needed for development.
Moreover it doesn’t really make a difference whether you prefer to have full control over the implementation or you just need a handy framework that will take care of all the low-level stuff, giving you the opportunity to focus on the business logic - Python always has something for you. Let’s take a high level look at the most popular options here:
As you can read on the main page of the project:
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
That kind of sums up the whole idea of this framework: it lets you start quickly; you can build a web service not worrying about stuff like database connections, user permissions, authentication, routing and so on. With the massive amount of available extensions, you can just focus on delivery. Period.
Django has its cons though. The framework is rather massive and makes a lot of decisions for you. In other words: Django sacrifices some developer freedom to achieve its goal of rapid development. If that doesn’t scare you away, take a deep dive into Django. Otherways, Python has something radically different for you.
Flask is called a microframework and this name suits it very well. Decisions underlying its design are very different from those that inspired the Django developers. It is light and it gives you freedom of choice.
On the other hand - mind you, this is only my opinion - you need to put a bit more effort into the development if you need things like authentication or REST API, therefore it might not be the best fit for large web applications.
Pyramid falls a little behind Django and Flask when it comes to counting headlines, but it’s still a framework worth mentioning, because it aims to fill the middle ground between Flask and Django.
Its purpose is to start small and finish big. It also doesn’t try to make decisions for the developer (e.g. that’s the only one of these three frameworks that doesn’t favor a templating engine). It also encourages the developer to think about scaling services by putting a lot of attention on tests.
Automate the boring things
Did you ever write a bash script that you used one time and then deleted? I bet you did. I did.
Honestly I hate writing bash scripts (that’s also true for Windows CMD and PowerShell!), but I love to automate the boring stuff. And that’s one of the reasons I love Python - it can replace all the command line languages, helping you to be more productive. Due to the fact that Python is OS-independent it allows you to write one script that will run on any OS you like. The same syntax, no matter where.
Also I find Python syntax more appealing than bash or PowerShell. It’s faster to write, easier to read and debug. And that’s still the same Python that lets you use Machine Learning or build web services.
Therefore next time you’re struggling with a script to bring order to your collection of photos, backups of your data on the external drive, or playing with Excel files, give Python a try. You won’t regret it.
Home automation with Python
Yes, that’s right: home automation is another area in which Python has found cozy niche. With a Python-based home-automation hub like Hass.io (www.home-assistant.io) and the Python-friendly Raspberry Pi, you can make your home smarter than ever before.
If you require a bit more low-level work, you may find MicroPython (micropython.org) handy - it’s an open source Python compiler dedicated to running directly on microcontrollers. There’s also an open source hardware board - called pyboard - that you can actually run Micropython scripts on.
That’s all very nice, but you may wonder what that gives you exactly. If you combine these hardware and software bits together you can make your home smarter than ever.
Web scraping with Python
Ever wanted to have a detailed history of prices from an internet store? Or maybe you need to perform an analysis of a given topic (articles? tweets? FB posts?) You’ve probably already guessed it: Python is a perfect match for web scraping. It allows you to use handy low level libraries like Requests and Beautiful Soup, or just take a web scraping framework like Scrapy and build a whole web scraping platform. Whichever approach is your preferred one, you can get all the data quickly and with the minimal effort.
Why we used Python in our app for predictive maintenance for wind turbines
If you’re still not convinced, let me say that we’re using Python for our app for predictive maintenance for wind turbines.
We could have chosen another language, but let’s face it, there’s no other solution that would work so well with data acquisition and processing (Numpy+Pandas+SqlAlchemy), statistical modeling (Scikit-Learn, StatsModels), neural networks (Keras) - or even absolutely custom neural networks (TensorFlow low level API) - allowing us to make the output easily accessible via REST API (Flask).
>>> Sounds interesting? Check out our case study
Why should you try Python?
Let’s wrap this article up. Python will become your good friend whenever you need data analysis, or when you want to try your hand at machine learning.
But because Python is so flexible and has a vast catalog of possible implementations, you can also use it for quick development of web applications which is pretty handy when you have to build a prototype or MVP for the customer. It will work perfectly fine when developing solutions for home automation or just for automation of rather dull stuff. Well, the list of Python uses is long, so just give it a try and check what Python can do for you.