Â
1.1 Installing Python on Your Computer
Before you can start coding in Python, you need to install the Python interpreter on your computer. Follow these steps to install Python:
Windows:
Visit the official Python website at https://www.python.org/.
Download the latest version of Python for Windows.
Run the installer and follow the installation instructions.
Make sure to check the box that says “Add Python to PATH” during installation.
macOS:
macOS typically comes with Python preinstalled. However, it’s recommended to install the latest version using Homebrew or the official Python installer.
Open a terminal and type `brew install python` if you have Homebrew installed.
Alternatively, download the macOS installer from the Python website and follow the installation instructions.
Linux:
Most Linux distributions come with Python preinstalled. However, you can install it using your package manager if needed.
Open a terminal and type the appropriate command based on your distribution. For example, on Ubuntu, you can type `sudo aptget install python3`.
1.2 Understanding the Python Interpreter and Interactive Mode
Python comes with an interactive interpreter that allows you to execute Python code interactively. To start the Python interpreter, open your terminal or command prompt and type `python` (or `python3` for Python 3).
Once the interpreter starts, you’ll see the Python prompt (`>>>`), indicating that Python is ready to accept commands. You can type Python expressions and statements directly into the interpreter, and it will execute them immediately.
For example:
“`python
>>> print(“Hello, World!”)
Hello, World!
“`
1.3 Writing Your First Python Program: Hello, World!
Let’s write a simple Python program that prints “Hello, World!” to the console.
“`python
This is a simple Python program
It prints “Hello, World!” to the console
print(“Hello, World!”)
“`
To write a Python program, open a text editor (such as Notepad, Sublime Text, or VS Code) and type the code above.
Save the file with a `.py` extension, for example, `hello_world.py`.
Open your terminal or command prompt, navigate to the directory where you saved the file, and run the program by typing `python hello_world.py`.
You should see the output `Hello, World!` printed to the console.
Â