Introduction to Python Programming
Python Basics
What is Python?
Python is a programming language, which is just a way to give instructions to a computer. Think of it like a recipe. A recipe gives a chef a series of steps to create a dish; a program gives a computer a series of steps to complete a task.
Created in the late 1980s by Guido van Rossum, Python was designed to be easy to read and write. Its syntax is clean and looks a lot like plain English, which makes it a great choice for beginners. But don't let its simplicity fool you. Python is incredibly powerful and is used for everything from building websites and analyzing data to creating artificial intelligence.
The main goal of Python is to make programming straightforward, allowing developers to focus on solving problems rather than getting stuck on complicated language rules.
Setting Up Your Workspace
Before you can start writing Python code, you need to install it on your computer. This installation includes the Python interpreter, the program that actually reads your code and carries out your instructions.
You can download the official installer from the Python website, python.org. While macOS and Linux often come with a version of Python already installed, it's a good habit to install the latest version yourself.
During installation on Windows, you'll see a checkbox that says something like "Add Python to PATH." Make sure you check this box. It makes running your programs much easier later on.
Once installed, you can access Python's interactive shell. This is a tool that lets you write and run Python code one line at a time. It’s perfect for quick tests and experiments. To open it, just open your computer's terminal or command prompt and type python (or python3 on some systems) and press Enter. You'll see a prompt that looks like >>>.
Try typing a simple command:
2 + 2
Press Enter, and Python will immediately respond with the answer:
4
Your First Python Script
The interactive shell is great for small tasks, but for more complex programs, you'll want to save your code in a file. These files are called scripts. A Python script is simply a text file that ends with the .py extension.
Let's create the classic first program: "Hello, World!" Open any basic text editor, like Notepad on Windows or TextEdit on Mac, and type the following line:
print("Hello, World!")
This code uses the built-in print() function. As the name suggests, this function tells the computer to display whatever you put inside the parentheses on the screen.
Save the file as hello.py on your desktop or another easy-to-find location. Now, open your terminal or command prompt again.
First, navigate to the directory where you saved your file. For example, if it's on your desktop, you might type cd Desktop. Once you're in the right folder, run the script by typing:
python hello.py
Press Enter. The computer will execute the code in your file, and you'll see the message Hello, World! appear in the terminal. You've just written and run your very first Python program.
