Ever wondered how websites, desktop applications, games, or even AI-powered software are created?”
Imagine being able to build your own programs and bring your ideas to life! Sounds exciting, right? With Python, anyone—even beginners—can start coding with ease. Let’s explore how Python makes programming fun, simple, and powerful!
If you’re new to Python and wondering where to start, don’t worry! This guide will walk you through the basics, from installation to writing your first Python program. Ready? Let’s dive in! 🎯
How to Use Python 🛠️
Before writing your first Python code, you have two options:
Install Python on Your Computer
- Download Python from the official site: https://www.python.org/downloads/
- Install an IDE like PyCharm, VS Code, or IDLE for better coding experience.
Use an Online Editor
Websites like Replit (https://replit.com/), Google Colab (https://colab.research.google.com/), or Jupyter Notebook allow you to write and run Python code without installation.
1.A. Install Python 🛠️
Installing Python allows you to run scripts and build projects locally.
Windows Installation
- Go to the official Python website: Download Python
- Download the latest Python version for Windows.
- Run the installer and check the box “Add Python to PATH” before installing.
- Click Install Now and complete the setup.
💡Check Installation: Open a terminal or command prompt and type:
python --version
Or
python3 –version
If installed correctly, it will show the Python version! 🎉
macOS Installation
- Download Python from https://www.python.org/downloads/mac-osx/
- Open the .pkg file and follow the installation steps.
💡Check Installation: Open a terminal or command prompt and type:
python3 --version
Linux Installation (Ubuntu/Debian-based distros)
- Open a terminal and type:
- sudo apt update
- sudo apt install python3
💡Check Installation: Open a terminal or command prompt and type:
python3 --version
.
1.B. Choosing a Python IDE 🏗️
You can write Python code in different environments:
📝 IDLE – Comes with Python (Basic Editor)
⚡ VS Code – Lightweight & powerful (Recommended)
🐍 PyCharm – Great for large projects
🌐 Jupyter Notebook – Best for data science
💡 You can also run Python scripts in a simple text editor and execute them in the terminal:
Write Your First Python Program 💻
Now, let’s write a simple “Hello, World!” program!
1. Using Python Interpreter (Command Line Mode)
1. Open a terminal and type:
print("Hello, World!")
2. Press Enter and see Python in action! 🎊
3. Exit the interpreter using exit() or Ctrl+D.
2. Writing a Python Script:
- Open a text editor (Notepad, VS Code, PyCharm, etc.).
- Create a new file: hello.py.
3. Write the following code:
print("Hello, World!")
4. Save the file and run it in the terminal:
python hello.py
5. Output:
Hello, World!
3. Using Python Without Installation (Online Editors)
If you don’t want to install Python, you can use online editors:
🔹 Google Colab – https://colab.research.google.com/
🔹 Replit – https://replit.com/~
🔹 Jupyter Notebook (via Anaconda or Google Colab)
🔹 OnlineGDB – https://www.onlinegdb.com/
Example: Run this code in an online editor:
print("Hello, Python Online!")
Printing in Python
- The print() function is used to display text or other information on the screen.
- It helps us check program output or print messages for the user.
Example:
print("Hello, Python Learners!")
Explanation:
- The print() function takes a string (text enclosed in quotes) and displays it on the screen.
- Here, “Hello, Python Learners!” is printed as output.
Parameters of the print() Function in Python
The print() function in Python has multiple parameters that allow customization of the output. The most commonly used parameters are:
- objects → The values to print.
- sep (separator) → Defines how multiple values are separated.
- end → Specifies what to print at the end of the output.
- file → Redirects output to a file instead of the console.
- flush → Controls buffering when printing.
1. objects (Values to Print)
The print() function can print strings, numbers, variables, lists, and more.
Example:
print("Hello, World!")
print(42)
print(3.14)
print(True)
Output:
Hello, World!
42
3.14
True
You can print multiple values by separating them with commas:
print("Hello", "Python", "Learners!")
Output:
Hello Python Learners!
2. sep (Separator)
By default, print() separates multiple values with a space (” “). The sep parameter allows you to change the separator.
Example:
print("Apple", "Banana", "Cherry", sep=", ")
Output:
Apple, Banana, Cherry
Example with Special Characters:
print("Python", "Java", "C++", sep=" | ")
Output:
Python | Java | C++
3. end (Ending Character)
By default, print() ends output with a newline (\n). The end parameter allows you to change this behavior.
Example:
print("Hello", end=" ")
print("World!")
Output:
Hello World!
Example Using a Special Character:
print("Python", end="...")
print("Rocks!")
Output:
Python…Rocks!
4. file (Redirect Output to a File)
By default, print() outputs to the console. The file parameter lets you write output to a file.
Example:
file = open("output.txt", "w") # Open file in write mode
print("Hello, File!", file=file)
file.close() # Close the file
Explanation:
- This writes “Hello, File!” into output.txt instead of printing it on the screen.
5. flush (Forcing Output to Appear Immediately)
By default, print() waits before sending output to the screen (buffering). The flush parameter can force immediate output.
Example:
import time
print("Loading", end="", flush=True) # Print immediately
for _ in range(3):
print(".", end="", flush=True)
time.sleep(1) # Wait for 1 second
Output (appears gradually on the screen):
Loading…
Complete Example Using All Parameters
print("Python", "Java", "C++", sep=" | ", end="!!!\n", flush=True)
Output:
Python | Java | C++!!!
Summary Table of print() Parameters
Parameter | Description | Default |
objects | Values to print | Required |
sep | Separator between values | ” “ (space) |
end | What to print at the end | “\n” (new line) |
file | Output destination | sys.stdout (console) |
flush | Forces immediate printing | False |
Escape Characters in Python
- Escape characters are special symbols in Python that allow you to format text inside strings.
- They start with a backslash (\) followed by a character.
Escape Sequence | Meaning | Example | Output |
\n | New line | “Hello\nWorld” | Hello World |
\t | Tab space | “Python\tRocks” | Python Rocks |
\\ | Backslash | “This is a backslash: \\” | This is a backslash: \ |
\’ | Single quote | “It\’s Python” | It’s Python |
\” | Double quote | “She said, \”Hi!\”” | She said, “Hi!” |
Example:
print("line1 \n line2") # Moves line2 to a new line
print("Python\tProgramming") # Adds a tab space
print("This is a backslash: \\") # Prints a backslash
print("She said, \"Python is fun!\"") # Prints double quotes inside a string
Explanation:
- \n creates a new line, so “World” moves to the next line.
- \t adds a tab space between “Python” and “Programming”.
- \\ prints a backslash (\).
- \” allows double quotes inside a string without ending it early.
Output:
line1
line2
Python Programming
This is a backslash: \
She said, “Python is fun!”
Comments in Python
- Comments are notes added to a program to explain what the code does.
- They are ignored by Python when the program runs.
Single-line Comment (#)
# This is a comment
print("Hello, Python!") # This prints a message
Explanation:
- A comment starts with # and is ignored by Python.
- You can write a comment before or after a line of code to describe its purpose.
Output:
Hello, Python!
Multi-line Comment (“”” “”” or ”’ ”’)
- If you need a longer explanation, you can use triple quotes (“”” “”” or ”’ ”’) to create multi-line comments.
- “””
- This is a multi-line comment.
- It can span multiple lines.
- Python ignores this when running the program
print("Python is amazing!")
Explanation:
- Everything inside “”” “”” is treated as a comment.
- Multi-line comments are useful for writing detailed explanations or documentation.
Output:
Python is amazing!
Practice Task
Try running the following code and observe the output:
# This program prints a message
print("Welcome to Python!")
# Printing with escape characters
print("Learning\nPython\tis fun!")
# Using a comment
print("End of program!") # This is the last line
Explanation:
- The first print() function displays “Welcome to Python!”.
- The second print() uses \n to create a new line and \t to add a tab space.
- The last print() function runs normally, while the comment after it is ignored by Python.