Set Up Python and Your First Program
Set Up Python and Your First Program
Introduction to Python
Python is a versatile, beginner-friendly programming language known for its clear syntax and powerful capabilities. Whether you're interested in web development, data analysis, artificial intelligence, or automation, Python provides the foundation you need. Before writing your first program, you'll need to install Python and set up a development environment where you can write and run code.
Installing Python
Step 1: Download Python
Visit the official Python website at python.org. Navigate to the Downloads section and select the latest stable version (Python 3.x). Avoid Python 2.x, which is no longer actively maintained. Choose the installer appropriate for your operating system: Windows, macOS, or Linux.
Step 2: Run the Installer
On Windows, execute the downloaded .exe file. Importantly, check the box that says "Add Python to PATH" before clicking Install. This allows you to run Python from any command prompt. On macOS and Linux, follow the installation prompts in the terminal or use package managers like Homebrew.
Step 3: Verify Installation
Open your command prompt (Windows) or terminal (macOS/Linux) and type:
python --version
If installation was successful, you'll see the Python version number displayed.
Choosing a Code Editor
You have several options for writing Python code:
- IDLE: Python's built-in editor, included with your installation. Simple but limited—good for beginners.
- Visual Studio Code: A powerful, free editor with Python extensions that provide helpful features like syntax highlighting and debugging.
- PyCharm Community Edition: A dedicated Python IDE with excellent tools, though heavier than VS Code.
- Thonny: Specifically designed for Python beginners with an intuitive interface.
For this course, we recommend Visual Studio Code or Thonny as they balance simplicity with useful features.
Writing Your First Program
Create a new file and save it with a .py extension (for example, hello.py). Type the following code:
print("Hello, World!")
This single line demonstrates several key concepts. The print() function displays text on the screen. The text you want to display goes inside the parentheses and is enclosed in quotes.
Running Your Program
Using Command Prompt/Terminal: Navigate to the folder containing your file and type:
python hello.py
Using Your Editor: Most editors have a "Run" button or keyboard shortcut (often F5 or Ctrl+F5) that executes your code directly.
When you run the program, you'll see:
Hello, World!
Congratulations! You've successfully created and executed your first Python program.
Next Steps
Now that you're set up, you're ready to explore Python's fundamental concepts. In upcoming lessons, you'll learn about variables, data types, operators, and control flow. Each concept builds on your foundation, gradually expanding your ability to write increasingly complex programs.
Remember: programming is learned by doing. Don't just read about Python—write code, make mistakes, and experiment. Your development environment is now your laboratory for learning this powerful language.