Introduction
Python is a general-purpose programming language that is easy to learn and use. It is a high-level language, which means that it abstracts away most of the complex details of the machine (computer) and is more human-readable. Python is an interpreted language, which means that it is not compiled into machine code. Instead, it is executed line-by-line by the Python interpreter.
Python was created by Guido van Rossum and originally released in 1991. It was designed with a focus on code readability and simplicity. Python has since become one of the most popular programming languages in the world, used by millions of developers for a wide range of applications.
Python is a versatile language that can be used for a wide range of applications, including web development, data analysis, machine learning, artificial intelligence, scientific computing, and more. It has a large and active community that contributes to its development and provides a wealth of libraries and frameworks that extend its functionality. Python is known for its simplicity and readability, making it a popular choice for beginners and experienced programmers alike. It is also a dynamically typed language, which means that you do not need to declare the data type of variable before using it. This makes Python code more flexible and easier to write.
Why Python for Data Analytics?
Python is one of the two leading programming languages in the field of data analytics. The other is R. R was invented in 1993 as a statistics language. You will learn how to use R in other classes in the program. Its greatest strengths are in high-level statistical analysis. It is still used commonly within the academic community for this reason, but it has fallen behind Python in data analytics since 2015 and 2016. Python was invented in 1991 as a general-purpose language, which means it can do almost anything. It contains many libraries (pre-written code that we can use without reinventing the wheel) for statistical analysis and visualizations. In addition, it can create web servers, video games, command line interfaces, and general scripting tasks. Finally, Python dominates the machine learning and AI programming space and is the leading choice for data scientists. In short, with Python, you can learn once and do most things.
Python is a popular choice for data analytics for several reasons:
- Ease of Learning: Python has a simple and clean syntax, making it accessible to beginners.
- Rich Ecosystem: Python has a vast ecosystem of libraries and frameworks for data analysis, such as Pandas, NumPy, and Matplotlib.
- Community Support: Python has a large and active community, which means that there are plenty of resources, tutorials, and documentation available.
- Integration: Python can easily integrate with other languages and technologies, making it a versatile choice for data analytics.
Moreover, Python has taken over the Machine Learning and AI space, which is a natural extension of data analytics. It has libraries like TensorFlow, PyTorch, and Scikit-learn that make it easy to build and deploy machine learning models. This makes Python a great choice for data scientists who want to work with both data analysis and machine learning.
The Basics of Python
As well as being a high-level language, Python is an interpreted language. Interpretation means that Python does not directly translate source code into machine-readable binary, as compiled languages like C do. Instead, when you run a Python program, the source code is first translated into an intermediate form called bytecode, which is a lower-level, platform-independent representation of the code. This bytecode is then executed by the Python Virtual Machine (PVM), which interprets the instructions and performs the necessary system-level operations. This process happens dynamically at runtime, making Python highly flexible and easy to use. However, the additional interpretation step makes Python slower compared to compiled languages such as C or Rust. In addition, the lack of a compilation step (when you build the code into binary before running it) makes it easier to push errors to production. This happens because compilation (or the build step) will fail if an error exists whereas Python will only detect errors at runtime when the problematic code is executed. On the plus side, interpreted languages are easier to develop because they allow for rapid prototyping and immediate feedback. You can write and execute code without needing a separate compilation step, which speeds up the development process. Additionally, Python’s dynamic typing (more on this later) and high-level abstractions make it simpler to write and test code, especially for beginners or when working on projects that require quick iteration.
Python uses indentation to define code blocks, which makes the structure of the code clear and easy to understand. Unlike many other programming languages that use braces or keywords to delimit blocks of code, Python enforces consistent indentation as part of its syntax. This approach not only improves code readability but also encourages developers to write clean and well-organized code. By making indentation a requirement, Python reduces the likelihood of errors caused by mismatched braces or poorly formatted code, making it especially beginner-friendly. This design choice reflects Python’s emphasis on simplicity and readability, which are core principles of the language. On the flip side, however, this means that you need to be particularly careful on the indentation level of each block of code. See below for an example.
if 5 > 2: # an if statement that we will get to in chapter 3
print("Five is greater than two!")
# indentation means the above line belongs to the if statement
# This will work well and print the message since 5 is greater than 2While the above code will work because the print() function (which prints text to the console) is indented, the following code will not work because the print() function is not indented correctly:
if 5 > 2:
print("Five is greater than two!") # this will throw an errorNotice the lack of indentation for example 2. Python expects a code block below the if statement. A code block is a series of statements that are grouped together and executed as a unit. In Python, a code block is defined by its indentation level, so the lack of proper indentation causes a syntax error, as Python cannot determine where the block begins or ends.
Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming. We will focus on procedural programming in this class. Procedural programming is a straightforward approach that organizes code into a series of steps or instructions, executed in a sequential order. This makes it easy to follow and debug, as the flow of the program mirrors the logical progression of tasks. It is particularly well-suited for data analytics, where data is often processed step by step linearly. Object-oriented programming, on the other hand, is centered around the concept of “objects,” which bundle data and the methods that operate on that data. This paradigm is ideal for building complex systems, as it promotes code reuse and modularity by organizing functionality into reusable components. We will discuss object-oriented programming briefly in chapter 6 on classes. Classes and objects are fundamental to programming and particularly useful in machine learning workflows, but those are beyond the extent of this book’s purview. Functional programming, by contrast, focuses on writing programs using pure functions, avoiding shared state and side effects. It emphasizes immutability and a declarative style, which can make code more predictable and easier to test. While object-oriented and functional paradigms have their strengths, procedural programming is simpler and more intuitive for beginners. Since we will be working in Jupyter Notebooks, which are designed for interactive and linear workflows, the sequential nature of procedural programming aligns perfectly with the notebook’s structure, making it a natural choice.