Introduction to Python

Python is a powerful, versatile language that’s widely spread. In this post, we'll cover the essentials of Python. Step-by-step installation and a first look at Python syntax. Writing your first "Hello, World!" script and understanding core programming concepts in Python.

Introduction to Python
Photo by Hitesh Choudhary / Unsplash

1. Overview of Python and Its Uses

Python is a versatile and powerful programming language renowned for its simplicity and readability. Created by Guido van Rossum and first released in 1991, Python emphasizes clear syntax and code readability, making it an excellent choice for beginners and experts alike.

Python is widely used in various fields, including:

  • Web Development: With frameworks like Django and Flask, Python is a popular choice for creating dynamic websites and web applications.
  • Data Science and Machine Learning: Libraries like Pandas, NumPy, and scikit-learn make Python indispensable for data analysis and predictive modeling.
  • Scripting and Automation: Python can automate repetitive tasks, from file handling to system operations.
  • Game Development: Libraries like Pygame provide tools for creating 2D games.
  • Artificial Intelligence (AI): Python is widely used in AI research, with TensorFlow and PyTorch libraries supporting machine learning and neural networks.
  • Internet of Things (IoT): Python works well with microcontrollers like Raspberry Pi, enabling creative projects in IoT.

Python's versatility and large ecosystem of libraries make it a go-to language across industries and applications.

2. Installation and Setup

Let’s go through setting up Python on your machine.

a. Check for Pre-installed Python

On many systems, Python is pre-installed. You can check if Python is already available by opening your terminal (Linux/Mac) or command prompt (Windows) and typing:

python --version

or

python3 --version

If you see a version number (e.g., Python 3.x.x), Python is already installed.

b. Installing Python

If Python isn’t installed, follow these steps:

  1. Download Python: Visit the official Python website at python.org and click on the "Downloads" section. Select the version compatible with your OS.
  2. Install Python:
    • Windows: Run the installer and make sure to check the box that says "Add Python to PATH" before you proceed with the installation. This allows you to run Python from the command prompt.
    • macOS: Run the downloaded installer. You may need to use brew install python if you prefer using Homebrew.
    • Linux: Install Python via your package manager, such as sudo apt install python3 for Ubuntu-based systems.
  3. Verify Installation: Once installed, verify by typing python3 --version in your terminal or command prompt. You should see the Python version number, confirming that Python is installed.
  4. Install an Editor: For coding, you’ll need a text editor. Good options are:
    • VS Code: Lightweight and feature-rich, with support for Python extensions.
    • PyCharm: A full-featured IDE specifically for Python.
  5. Run Python in Interactive Mode:
    • Open your terminal or command prompt and type python3 or python. This will open an interactive Python shell where you can type Python commands and see immediate results. Type exit() to quit.

c. Setting Up Virtual Environments (Optional)

Virtual environments allow you to create isolated Python environments for different projects, which is especially helpful for managing dependencies.

python3 -m venv myenv
source myenv/bin/activate  # On Windows use `myenv\Scripts\activate`

Once activated, you can install packages specific to your project.

3. Writing and Running Python Scripts

A Python script is simply a file with Python code saved with a .py extension. Here’s a step-by-step guide on creating and running a simple Python script.

a. Writing Your First Script

  1. Save the file as hello.py.

Open your text editor and type the following line:

print("Hello, World!")

b. Running Your Script

  1. Open a terminal or command prompt.
  2. Navigate to the directory where your script is saved using cd <path_to_script>.

Run the script by typing:

python3 hello.py

You should see the output: Hello, World!

Congratulations! You've written and executed your first Python script.

4. Basic Syntax and Structure

Let’s dive into some foundational Python syntax.

a. Variables

In Python, variables don’t require explicit type declarations. You can assign values directly:

message = "Hello, Python!"
number = 42
pi = 3.14159

b. Data Types

Python supports various data types:

  • String: A sequence of characters, e.g., name = "Alice".
  • Integer: Whole numbers, e.g., age = 25.
  • Float: Decimal numbers, e.g., height = 5.9.
  • Boolean: Represents True or False.

c. Comments

Use # to add comments to your code. Comments are ignored by Python and are useful for adding notes.

# This is a comment
print("Comments are helpful for code readability!")

d. Indentation

Indentation is crucial in Python as it defines blocks of code, especially for loops and functions.

if age > 18:
    print("You are an adult.")

e. Basic Operators

Python includes a variety of operators for performing operations:

  • Arithmetic Operators: +, -, *, /, %
  • Comparison Operators: ==, !=, >, <, >=, <=
  • Logical Operators: and, or, not

Example:

x = 10
y = 20
print(x + y)  # Output: 30
print(x > y)  # Output: False

f. Input and Output

Python makes it easy to get user input and display output.

name = input("Enter your name: ")
print("Hello, " + name + "!")

g. Basic Control Flow

Python has several control flow tools, like if statements and loops.

If Statements:

temperature = 25
if temperature > 20:
    print("It's warm!")
else:
    print("It's cold.")

For Loops:

for i in range(5):
    print(i)  # Output: 0, 1, 2, 3, 4

While Loops:

count = 0
while count < 5:
    print(count)
    count += 1

Conclusion

In this introductory lesson, you’ve gained an understanding of Python’s background and why it’s such a popular programming language today. We’ve also covered the setup process, how to write and run a basic Python script, and explored some of Python’s foundational syntax. With this knowledge, you’re well-equipped to start coding in Python!

Stay tuned for the next lesson, where we’ll dive deeper into Python basics, including variables, data types, and operators. Happy coding!

Read next

NodeJS and TypeScript

The key differences between Node.js and TypeScript. Learn how these technologies serve different purposes and complement each other for building scalable and reliable applications.