Introduction to Python
Discover why Python is one of the most popular programming languages and set up your development environment to write your first program.
Why Python?
Python is a high-level, interpreted programming language known for its clean syntax and readability. It powers applications in web development, data science, machine learning, automation, and more. Python's extensive standard library and thriving ecosystem of third-party packages make it an excellent choice for beginners and professionals alike. Its philosophy emphasizes code readability, which means you can express concepts in fewer lines than many other languages.
Setting Up Your Environment
To get started with Python, download the latest version from python.org and run the installer for your operating system. Make sure to check the option to add Python to your system PATH during installation. Once installed, you can verify the installation by opening a terminal and typing the version command. Many developers also use virtual environments to isolate project dependencies.
# Check Python version
python --version
# Create a virtual environment
python -m venv myenv
# Activate the virtual environment (macOS/Linux)
source myenv/bin/activate
# Activate the virtual environment (Windows)
myenv\Scripts\activate
Hello World
The traditional first program in any language is Hello World. In Python, printing to the console is straightforward with the built-in print function. Unlike many other languages, Python does not require semicolons at the end of statements or curly braces to define code blocks. Indentation is used instead to define the structure and scope of your code.
# Your first Python program
print("Hello, World!")
# You can print multiple values separated by commas
print("Hello", "Python", "World")
# Use f-strings for formatted output
name = "Python"
version = 3.12
print(f"Welcome to {name} {version}!")
The Python REPL
Python comes with an interactive Read-Eval-Print Loop (REPL) that lets you execute code one line at a time. Simply type python in your terminal to start the interactive shell. The REPL is an excellent tool for experimenting with small code snippets, testing ideas, and learning new concepts. You can use it as a calculator, test functions, or explore modules interactively.
# Start the REPL by typing 'python' in your terminal
# Then try these commands interactively:
>>> 2 + 3
5
>>> "hello".upper()
'HELLO'
>>> import math
>>> math.pi
3.141592653589793
>>> help(print)