DEV Community

Patrik Braborec
Patrik Braborec

Posted on

 

Why You Should Use Python For Your Next Project

There are tons of programming languages that we can use for development, and naturally we as programmers want to choose the ‘best’ one. However in reality every programming language has advantages and disadvantages. There is no universal language for everything. Instead of finding the best one, we can focus on finding the language that will solve our problems in the best way possible, and there, more often than not, Python might be playing an important role. Python might be the perfect fit for you if you are about to develop a back-end solution, data analysis, or machine learning tool. Also, according to the stack overflow trends, Python’s popularity has been rapidly growing in the last few years. That means the language can be a perfect fit for you or your company if you are starting a new project — you will have access to many libraries and a strong developer community.

Cover image

It is important to emphasize that Python is very popular in the data community. Therefore, Python may be the right choice if you are about to build BI tools, data analytics tools, or other tools heavily dependent on data.

Why Is Python So Popular?

Python is a high-level, dynamically typed programming language that focuses on rapid and robust development. It can be used in many areas, from web scraping to writing algorithms and data structures. Thanks to its versatility and simplicity, it has become a trendy language for data scientists in the last few years. Also, web developers are increasingly using Python thanks to frameworks such as Django, Flask, or FastAPI. World’s largest companies, such as Google, Facebook, Microsoft, and Spotify rely on Python every day, ensuring a lively community and a plethora of libraries for you to use for years to come.

5 Main Advantages of Python

I have mentioned a few positive words about Python but let’s dive into some details. If you are about to decide on something, it is a good to first list the advantages and disadvantages. This chapter attempts to cover the advantages of Python.

1. Easy To Use and Fast To Develop

Python has an English-like syntax, making it simpler to parse through and understand. In general Python requires fewer lines of code and omits a lot of the ‘syntactical noise’ (brackets, semicolons, …) that you are forced to deal with in other programming languages. Equally important fact is that Python is an interpreted language, which means you save on seconds or even minutes of compilation time every time you want to test out any change to the code. It’s just very human friendly.

2. Vast Libraries Support

Python package manager (pip) consists of over 300,000 packages. You can use libraries like ​​SciPy, which contains various modules for optimization, linear algebra, integration, and statistics, or Plotly, which you can use for scientific-quality graphing. The scope of problems that Python libraries address is vast. Rarely will you struggle to find the right library that will help you solve the problem.

3. Portability

If you are familiar with C or C++, you may know that sometimes you need to modify the code to run the program on different platforms. With Python being an interpreted language, you do not need to deal with these problems — you only need to write code once and you can run it anywhere. There is one crucial detail — you should not use system-dependent features if you want to run the code on different platforms.

4. Notebooks

Notebooks, such as Jupyter or Google Colab, allow you to create and share documents that contain code, equations, and for example, visualizations. What is super useful is that these documents are human-readable and allow you to execute code to see the results immediately. These documents are a powerful tool for scientists and data analysts.

5. Strong Community

Python has a huge community. You can consult the community for advice on any programming issue you may have, and chances are there will be someone with similar experience who can help you. Also because of the community’s size, Python projects and libraries are often more feature-rich and complete, than their C++ or Java counterparts.

3 Main Disadvantages of Python

I have mentioned advantages, but sometimes it is more important to know the disadvantages to make a good decision. This chapter attempts to cover the main disadvantages of Python.

1. Slow Speed

Python is an interpreted language, which means line-by-line code execution, often leading to slow speed. Also Python, as mentioned above, is a dynamically-typed language — it executes many common programming behaviors at runtime that static programming languages (C or C++) perform during compilation. This aspect of dynamically typed language also leads to slow speed. If you are looking for high-performance applications, consider if the slow speed of Python is not a blocker for you.

Still, it is worth mentioning that some libraries like NumPy, which is a library for scientific computing with Python, have some parts written in C, making it fast. It is always better to do the heavy computation in low-level languages such as C or C++, but their syntax and complexity might not be suitable for large projects. Therefore, some parts of Python libraries are written in C/C++ to make them faster and much more straightforward for data scientists, data analysts, and others. If you are looking for high performance in your code, consider Cython, which gives you C-like performance with code mostly written in Python.

2. Weak Mobile App Development

Python is great as a server-side language, but it is not very suitable for mobile application development. Android and iOS applications were simply not meant for Python developers. Libraries like Kivy and Beeware are helping make Python more mobile friendly in recent years, but they cannot compete with Kotlin, Java, or Swift in mobile development.

3. Not Memory Efficient

Python has to do a tradeoff to offer simplicity to developers — it uses a large amount of memory. This can be a significant disadvantage for building applications that need memory optimization to run smoothly.

If it happens that the Python code uses a lot of memory, one method is to use Heapy to find out which objects are holding the most memory. It’s good to think twice before developing an application in Python that could be memory intensive. Again, as mentioned above in the section about Python performance, the communication solves memory efficiency issues with C++ libraries (Apache Arrow) and the no-copy approach.

Data Science Relies on Python

If you are interested in tech, you probably noticed that people in the field like to call data the new oil. That would make data scientists, data engineers, and other data personas the oil magnates of the future, and they in turn rely on Python. These people spend most of their time preparing and working with data with the help of tools like Pandas, SciPy, TensorFlow, etc.

Pandas is especially popular in working with data using their data series and data frames data structures. You can imagine Pandas as a 2D table (similar to a spreadsheet) where you can filter, clean empty cells, remove duplicates, etc. An example is worth a thousand words, imagine that you have the following structure in the database:

Database structure

You can see that the second row contains age 240, which is wrong (probably typo during registration). With Pandas, you can easily fix that with just one line of code (df means data frame):

df.loc[1, 'Age'] = 24
Enter fullscreen mode Exit fullscreen mode

This means that with Python and Pandas, you can automatize the flow of how you clean data from a database, which is very helpful. But before you can start working with Pandas, you usually have to load data from a database. If you have many tables, you have to deal with many joins, which might not be easy. The following section discusses how we deal with that in GoodData.

GoodData Python SDK

Our product contains a Headless BI engine where you can connect any application, data platform, or visualization tool to the engine’s semantic layer and consume the same consistent analytics anywhere. Connecting to a semantic layer with the help of GoodData Python libraries is very straightforward! GoodData Pandas allows creating pandas series and data frames from the computations done against a semantic model in GoodData.CN. As mentioned above, imagine you have a database with many tables, and you want to get a data frame consisting of columns from various joined tables. Usually, you have to do many joins manually in SQL before getting the desired data frame in Pandas. But if you connect the database to GoodData.CN, you can forget joins — with the help of a semantic model and GoodData Pandas, you will get your desired data frame with much less hassle. Just for demonstration, compare the two following code snippets. The first one is just pure pandas:

def pure_pandas():
    engine = create_engine(f'postgresql+psycopg2://{USERNAME}:{PASSWORD}@localhost/demo')
    query = 'select * from demo.demo.campaigns c join demo.demo.order_lines ol on ol.campaign_id  = c.campaign_id;'
    df = pd.read_sql_query(query, con=engine)
    grouped_df = df.groupby(["campaign_name"]).sum()
    price_sum = grouped_df[["price"]]
    filtered_df = df.loc[df.order_status == "Delivered"].copy()
    filtered_df["order_amount"] = filtered_df["price"] * filtered_df["quantity"]
    filtered_df_grouped = filtered_df.groupby(['campaign_name']).sum()
    filtered_df_grouped = filtered_df_grouped[["order_amount"]]
    wanted_df = price_sum.join(filtered_df_grouped, on='campaign_name', how='left')
    wanted_df.reset_index(level=0, inplace=True)
    wanted_df = wanted_df.rename(columns={"price": "price_sum", "order_amount": "revenue"})
    return wanted_df
Enter fullscreen mode Exit fullscreen mode

The second one uses GoodData in conjunction with Pandas:

def good_pandas():
    gp = GoodPandas(host=HOST, token=TOKEN)
    frames = gp.data_frames(WORKSPACE_ID)
    df = frames.not_indexed(columns=dict(
        campaign_name='label/campaigns.campaign_name',
        price_sum='fact/order_lines.price',
        revenue='metric/revenue'
    ))
    return df
Enter fullscreen mode Exit fullscreen mode

If you are interested in details, check out Python SDK for Composable and Reusable Analytics. We have also built other Python libraries like GoodData Python SDK to interact with GoodData.CN, or GoodData Foreign Data Wrapper which presents a way to map the GoodData.CN semantic layer and insights into a PostgreSQL as foreign tables. We intend to continue making the GoodData platform increasingly Python friendly.

Conclusion

We went through the advantages and disadvantages of Python, and we demonstrated how it is possible to use Python as a language for the company libraries. We hope you found it valuable — If you have a question or want to discuss something, do not hesitate to drop a comment below, and if you are interested in more articles, please follow us!

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git