Starting Python from Scratch
Setup and Basics
Setting Up Your Workspace
Before writing any Python, you need two things: the Python interpreter and a place to write your code. The interpreter is the program that reads your Python code and translates it into instructions the computer can execute. You can download it directly from the official Python website.
Once Python is installed, you need a code editor. While you could use a basic text editor, most developers use an Integrated Development Environment, or IDE. An IDE provides helpful features like syntax highlighting, debugging tools, and code completion. Python comes with a simple, built-in IDE called IDLE, but many programmers prefer more powerful editors like Visual Studio Code or PyCharm.
Running Your First Code
There are two primary ways to run Python code: interactively in a or by executing a script file.
The REPL is a command-line interface where you can type Python code one line at a time and see the results immediately. To start it, just type python (or python3 on some systems) into your terminal and press Enter. You'll see a >>> prompt, indicating it's ready for your command.
# In your terminal
python
>>> print("Hello, interactive world!")
Hello, interactive world!
>>> 2 + 2
4
For more complex programs, you'll write your code in a script file. These are plain text files that end with a .py extension. After saving your code, you run the entire file from the terminal using the python command followed by the filename.
# File: my_first_script.py
print("Hello from a script!")
To run this, you would navigate to the file's directory in your terminal and type:
python my_first_script.py
The output, Hello from a script!, will appear in your terminal.
The Rules of the Road
Python has a few syntax rules you need to know. Unlike many languages that use braces {} to define blocks of code, Python uses indentation. The amount of whitespace at the beginning of a line is significant. Code that is part of the same block must be indented at the same level.
This strict indentation rule is a core feature of Python's design. It enforces a clean, readable code style across all Python projects.
For example, notice the indented print statement below. This tells Python that the print command is part of the if block.
mood = "happy"
if mood == "happy":
# This line is indented, so it's inside the 'if' block.
print("That's great to hear!")
# This line is not indented, so it runs after the 'if' block.
print("Have a good day.")
Another key rule is that Python is case-sensitive. This means the variables name and Name are treated as two completely different variables.
Finally, you can leave notes for yourself and other developers in your code using comments. Any text on a line following a hash symbol (#) is ignored by the interpreter. Comments are crucial for explaining the 'why' behind your code, not just the 'what'.
# This is a comment. It's here to explain the next line of code.
# It calculates the area of a circle with a radius of 5.
pi = 3.14159
radius = 5
area = pi * (radius ** 2) # The formula is πr²
What is the primary function of the Python interpreter?
In Python, how are blocks of code, such as the body of an if statement, defined?
