Introduction to Python

What is Python?

Python is a high-level, interpreted programming language known for its simplicity, readability, and versatility. Created by Guido van Rossum in 1991, it is dynamically typed and uses indentation to define code blocks, making it beginner-friendly and easy to maintain. Python is used across various domains, including web development, data science, automation, machine learning, and artificial intelligence, thanks to its extensive standard library and powerful third-party frameworks like Django, Flask, pandas, and TensorFlow. Its cross-platform nature, large community support, and open-source status have made it one of the most popular and widely adopted programming languages today.

Python offers a vast ecosystem of libraries that extend its functionality across various domains. Popular libraries include NumPy and pandas for data manipulation, matplotlib and seaborn for data visualization, and scikit-learn, TensorFlow, and PyTorch for machine learning and AI. For web development, frameworks like Flask and Django are widely used, while BeautifulSoup and Scrapy help with web scraping. Other useful libraries include requests for HTTP requests, SQLAlchemy for database interaction, pygame for game development, and OpenCV for computer vision. Python's rich collection of libraries makes it a powerful tool for tasks ranging from automation to scientific computing.

You can download Python from the official website at python.org/downloads, where you'll find installers for different operating systems, including Windows, macOS, and Linux. On Windows, download the executable installer and ensure you check the box to "Add Python to PATH." macOS users can install Python using a .pkg file, while many Linux distributions come with Python pre-installed or allow you to install it via package managers like apt or yum. The website also offers older versions and pre-release versions for testing.

Why Python?

Python is a versatile language with a wide range of applications, making it a popular choice for developers, data scientists, researchers, and educators. Here are some reasons why Python is widely used:

  • Readability: Python's clean and readable syntax makes it easy to learn and understand, reducing the cost of program maintenance and development.
  • Extensive Libraries: Python's rich standard library and third-party packages provide tools for various tasks, from web development to data analysis and machine learning.
  • Community Support: Python has a large and active community that contributes to its growth, providing resources, tutorials, and open-source projects.
  • Interpreted Language: Python's interpreted nature allows for rapid development and testing, making it ideal for prototyping and scripting.
  • Cross-Platform: Python is available on all major operating systems, allowing developers to write code that can run on different platforms without modification.
  • Object-Oriented: Python supports object-oriented programming, enabling developers to create reusable and modular code.
  • Scalability: Python is suitable for projects of all sizes, from small scripts to large applications, thanks to its scalability and robustness.
  • Integration: Python can be easily integrated with other languages and technologies, making it a versatile choice for building complex systems.

How to install Python?

Python can be installed on Windows, macOS, and Linux operating systems. The official Python website provides installers for different platforms, making it easy to set up Python on your machine. Here's how you can install Python on different operating systems.

Windows

  1. Download the Python installer from the official website at python.org/downloads.
  2. Run the installer and ensure you check the box to "Add Python to PATH" during installation.
  3. Click "Install Now" to start the installation process.
  4. Once the installation is complete, you can open a command prompt and type python to verify that Python is installed correctly.

macOS

  1. Download the Python installer from the official website at python.org/downloads.
  2. Open the downloaded .pkg file and follow the installation instructions.
  3. Once the installation is complete, you can open a terminal and type python3 to verify that Python is installed correctly.

Linux

Many Linux distributions come with Python pre-installed. You can check the version of Python installed on your system by opening a terminal and typing python3 --version. If Python is not installed, you can install it using the package manager of your distribution:

  • For Debian/Ubuntu-based systems, use sudo apt install python3.
  • For Red Hat/Fedora-based systems, use sudo yum install python3.
  • For Arch Linux, use sudo pacman -S python.

Note that sometimes, you need to use the specific version of python, such as python3.12, to install Python.

Getting Started with Python

Once you have Python installed on your system, you can start writing and running Python code. Python provides an interactive shell where you can type and execute Python commands in real-time. You can also write Python scripts in text files with a .py extension and run them from the command line.

Interactive Shell

To open the Python interactive shell, open a terminal or command prompt and type python or python3 or python3.12 (depending on your Python version). This will start the Python interpreter, and you can start typing Python commands:

Python 3.12.7 (main, Oct  1 2024, 08:52:12) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!
>>>  

Python Scripts

To write and run Python scripts, you can use a text editor or an Integrated Development Environment (IDE) like Visual Studio Code, PyCharm, or Jupyter Notebook. Save your Python code in a file with a .py extension, and run it from the command line using the Python interpreter. For example, suppose the name of the python script file is hello.py. The content of the file is:

# hello.py
print("Hello, World!")
      

Save the above code in a file named hello.py, and run it from the command line using:

python hello.py

How to install a Python package?

Python packages are collections of modules that provide additional functionality to your Python programs. You can install Python packages using the package manager pip, which comes pre-installed with Python. Here's how you can install a Python package using pip:

python3.12 -m pip install numpy

The above command installs the numpy package, which is a popular library for numerical computing in Python. You can replace numpy with the name of any package you want to install.

If you don't have the package manager pip installed, you can install it using the following command:
wget https://bootstrap.pypa.io/get-pip.py
python3.12 get-pip.py
To use the installed package in your Python code, you need to import it at the beginning of your script:
import numpy as np
This imports the numpy package and gives it the alias np, which is a common convention when working with numpy. You can now use the functions and classes provided by the numpy package in your code. To use a specific function from the package, you can call it using the package name as a prefix, for example:
result = np.sqrt(16)

References

To cite the book, use: Zhang, Z. & Wang, L. (2017-2022). Advanced statistics using R. Granger, IN: ISDSA Press. https://doi.org/10.35566/advstats. ISBN: 978-1-946728-01-2.
To take the full advantage of the book such as running analysis within your web browser, please subscribe.