Introduction & Print Function
The 'Print' function is like sending a text message to your future self on the screen. Just like texting 'Hello' to a friend, print() shows messages on your computer.
Python is a high‑level, interpreted programming language known for its readability and simplicity. When you write Python code, you are giving step‑by‑step instructions to a computer – a very literal machine that does exactly what you tell it, nothing more, nothing less. The very first command most programmers learn is the `print()` function.
**What does `print()` do?**
It outputs text (or numbers, or results of calculations) to the console – the black‑and‑white window where your program runs. This is your primary way of communicating with the user, debugging your code, or simply checking what your program is doing at a certain point.
**Syntax basics**
- `print("Hello")` – displays `Hello`
- Anything inside the parentheses is evaluated first, then displayed.
- Text must be enclosed in quotes (single `'` or double `"`). This is called a *string*.
- Numbers and variables do not need quotes.
**Printing multiple items**
You can pass several items separated by commas, and `print()` will automatically add a space between them:
```python
print("The answer is", 42)
```
Output: `The answer is 42`
**Controlling the end character**
By default, `print()` adds a newline (`\n`) at the end, so each `print()` call starts on a new line. You can change this using the `end` parameter:
```python
print("Hello", end=" ")
print("World")
```
Output: `Hello World` (both on the same line).
**Printing to a file**
The `print()` function can also write directly to a file using the `file` parameter:
```python
with open("output.txt", "w") as f:
print("Hello file", file=f)
```
**Escape sequences**
Certain characters need to be “escaped” using a backslash:
- `\n` – new line
- `\t` – tab
- `\\` – backslash
- `\"` – double quote inside a double‑quoted string
Example:
```python
print("Line1\nLine2\tIndented")
```
**Common beginner mistakes**
1. Forgetting quotes – `print(Hello)` ❌ (NameError)
2. Mixing quotes – `print("Hello')` ❌ (SyntaxError)
3. Missing parentheses – `print "Hello"` ❌ (Python 2 style, not allowed in Python 3)
4. Trying to print a variable that hasn't been defined.
**Real‑world use cases**
- Displaying welcome messages in command‑line tools.
- Debugging: print variable values at different stages.
- Showing calculation results to the user.
- Creating simple text‑based menus or reports.
- Logging program progress to the console.
**Practice exercises (try on your own)**
1. Print your name, age, and favorite color on three separate lines.
2. Print the same three items on one line, separated by spaces.
3. Print a small ASCII art (like a smiley face or a house) using multiple `print()` statements.
4. Experiment with `print(10 + 5)` – what happens? (It prints the result, not the expression).
5. Use the `end` parameter to create a countdown: `print(3, end=" ")`, `print(2, end=" ")`, `print(1, end=" ")`, `print("Go!")`
**Why mastering `print()` matters**
Even though `print()` seems trivial, it is the foundation of understanding how Python evaluates expressions, handles data types, and interacts with the outside world. Every experienced Python developer uses `print()` hundreds of times during development – it’s the simplest and most effective debugging tool. By truly understanding its parameters and behaviour, you are building good habits for future debugging, logging, and user interaction.
**Summary**
- `print()` outputs text to the console.
- Strings need quotes, numbers and variables do not.
- Use commas to separate multiple items.
- `end` controls what comes after the output (default newline).
- Practice small variations to build confidence.
# ========== EXAMPLE 1: Basic Printing ==========
print("=== Example 1: Basic Printing ===")
print("Hello, World!")
print("I am learning Python.")
print(2026)
print("The result is", 10 + 20)
print()
print() # empty line for spacing
# ========== EXAMPLE 2: The 'end' Parameter ==========
print("=== Example 2: Using 'end' Parameter ===")
print("One", end=" ")
print("Two", end=" ")
print("Three")
print() # empty line
# ========== EXAMPLE 3: Escape Sequences ==========
print("=== Example 3: Escape Sequences ===")
print("Line1\nLine2\tTabbed")
print("He said, \"Python is fun!\"")
→ Run this code interactively