Welcome to your first Python lesson! Python is a high-level, interpreted programming language known for its simplicity and readability. It is used in web development, data science, automation, AI, and more.
Every programmer starts with a "Hello, World!" program. In Python, this is incredibly simple compared to languages like Java, C++, or Go.
Create a file named hello.py and write the following line:
print("Hello, World!")To run it, open your terminal and type:
python hello.pyprint()is a built-in function in Python that outputs text to the screen."Hello, World!"is a string (text data). In Python, strings must be surrounded by either double quotes (") or single quotes (').
Comments are notes in your code written for humans. Python completely ignores them when running the program.
Use the hash symbol (#) to start a comment:
# This is a comment. Python will ignore this.
print("Hello, Python!") # This comment is inlineFor multi-line comments, you can repeat the # symbol or use a multi-line string (docstring) that isn't assigned to any variable:
"""
This is a multi-line comment.
It uses triple double-quotes.
"""You don't always need to write files to run Python. You can use the REPL (Read-Eval-Print Loop).
Open your terminal and type python (or python3 on Mac/Linux) and press Enter. You should see a prompt like:
>>> Now you can run Python code line by line:
>>> 3 + 5
8
>>> print("Interactive mode!")
Interactive mode!
>>> exit()Python can act as a simple calculator. Try these in the REPL:
| Operation | Operator | Example | Result |
|---|---|---|---|
| Addition | + |
5 + 3 |
8 |
| Subtraction | - |
10 - 4 |
6 |
| Multiplication | * |
4 * 3 |
12 |
| Division | / |
7 / 2 |
3.5 (returns a float) |
| Exponentiation | ** |
2 ** 3 |
8 (2 cubed) |
- Open the interactive Python shell (REPL) and calculate
12345 * 67890. - Write a Python script that prints three lines of text: your name, your favorite programming language, and your goal for this study path.