Writing Your First Script (Hello World)
🏷️ Python Basics: Syntax, Variables, and Types / Basic Syntax Rules
Welcome to your first Python script! This is the traditional starting point for learning any programming language. The Hello World script is a simple program that prints a message to the screen. It helps you verify that your Python environment is set up correctly and introduces you to the basic structure of a Python program.
⚙️ What You Need Before Starting
- Python installed on your machine (version 3.x recommended)
- A text editor or IDE (like VS Code, PyCharm, or even Notepad)
- A terminal or command prompt to run your script
🛠️ Creating Your First Script
Follow these steps to create and run your first Python script:
- Create a new file and name it hello_world.py (the .py extension tells your system this is a Python file)
- Open the file in your text editor
- Write the following line into the file: print("Hello, World!")
- Save the file
🕵️ Understanding the Code
Let's break down what you just wrote:
- print() is a built-in Python function that displays output to the screen
- The text "Hello, World!" is a string (a sequence of characters) enclosed in double quotes
- The parentheses () hold the information you want to print
- When Python runs this line, it executes the print function and shows the message
📊 Running Your Script
To execute your script, follow these steps:
- Open your terminal or command prompt
- Navigate to the folder where you saved hello_world.py
- Type the following command and press Enter: python hello_world.py
Your expected output should be: Hello, World!
🔄 Common Variations You Can Try
Experiment with these simple modifications to see how Python handles different inputs:
- print("Hello, Engineers!") — changes the message
- print(42) — prints a number without quotes
- print("Hello" + " " + "World") — combines multiple strings using the + operator
- print("Hello", "World") — prints multiple items separated by a comma (adds a space automatically)
⚠️ Common Mistakes to Avoid
| Mistake | What Happens | How to Fix |
|---|---|---|
| Missing parentheses | SyntaxError: invalid syntax | Add () around your text |
| Missing quotes | NameError: name is not defined | Enclose text in " " or ' ' |
| Misspelling print | NameError: name 'prnt' is not defined | Check spelling to match print |
| Wrong file extension | Script won't run as Python | Save with .py extension |
✅ Key Takeaways
- A Python script is simply a text file with a .py extension
- The print() function is your primary tool for displaying output
- Strings must be enclosed in quotes (single or double both work)
- Python executes your code line by line from top to bottom
- Always save your file before running it
🚀 Next Steps
Now that you've written and run your first script, try these challenges:
- Print your name and favorite programming language
- Print multiple lines using separate print() statements
- Print a simple calculation like print(2 + 3)
You've just taken your first step into Python programming. Every complex program starts with a simple print() statement — keep experimenting!
A Hello World script is the simplest Python program that prints text to the screen, confirming your environment works.
🖥️ Example 1: The simplest Hello World
This example prints the classic greeting using Python's built-in print() function.
print("Hello World")
📤 Output: Hello World
📝 Example 2: Hello World with a variable
This example stores the greeting text in a variable before printing it.
greeting = "Hello World"
print(greeting)
📤 Output: Hello World
👤 Example 3: Hello World with a name
This example prints a personalized greeting by combining text with a variable.
name = "Engineer"
print("Hello " + name)
📤 Output: Hello Engineer
🔤 Example 4: Hello World with formatted output
This example uses an f-string to insert a variable directly into the greeting text.
name = "Python"
print(f"Hello {name}")
📤 Output: Hello Python
🌍 Example 5: Hello World with user input
This example asks the user for their name and prints a personalized greeting.
user_name = input("Enter your name: ")
print(f"Hello {user_name}, welcome to Python!")
📤 Output: Hello [user input], welcome to Python!
Comparison Table
| Example | Technique Used | Output Type |
|---|---|---|
| 1 | Direct string in print() | Static text |
| 2 | Variable with print() | Static text |
| 3 | String concatenation | Dynamic text |
| 4 | f-string formatting | Dynamic text |
| 5 | User input + f-string | Interactive text |
Welcome to your first Python script! This is the traditional starting point for learning any programming language. The Hello World script is a simple program that prints a message to the screen. It helps you verify that your Python environment is set up correctly and introduces you to the basic structure of a Python program.
⚙️ What You Need Before Starting
- Python installed on your machine (version 3.x recommended)
- A text editor or IDE (like VS Code, PyCharm, or even Notepad)
- A terminal or command prompt to run your script
🛠️ Creating Your First Script
Follow these steps to create and run your first Python script:
- Create a new file and name it hello_world.py (the .py extension tells your system this is a Python file)
- Open the file in your text editor
- Write the following line into the file: print("Hello, World!")
- Save the file
🕵️ Understanding the Code
Let's break down what you just wrote:
- print() is a built-in Python function that displays output to the screen
- The text "Hello, World!" is a string (a sequence of characters) enclosed in double quotes
- The parentheses () hold the information you want to print
- When Python runs this line, it executes the print function and shows the message
📊 Running Your Script
To execute your script, follow these steps:
- Open your terminal or command prompt
- Navigate to the folder where you saved hello_world.py
- Type the following command and press Enter: python hello_world.py
Your expected output should be: Hello, World!
🔄 Common Variations You Can Try
Experiment with these simple modifications to see how Python handles different inputs:
- print("Hello, Engineers!") — changes the message
- print(42) — prints a number without quotes
- print("Hello" + " " + "World") — combines multiple strings using the + operator
- print("Hello", "World") — prints multiple items separated by a comma (adds a space automatically)
⚠️ Common Mistakes to Avoid
| Mistake | What Happens | How to Fix |
|---|---|---|
| Missing parentheses | SyntaxError: invalid syntax | Add () around your text |
| Missing quotes | NameError: name is not defined | Enclose text in " " or ' ' |
| Misspelling print | NameError: name 'prnt' is not defined | Check spelling to match print |
| Wrong file extension | Script won't run as Python | Save with .py extension |
✅ Key Takeaways
- A Python script is simply a text file with a .py extension
- The print() function is your primary tool for displaying output
- Strings must be enclosed in quotes (single or double both work)
- Python executes your code line by line from top to bottom
- Always save your file before running it
🚀 Next Steps
Now that you've written and run your first script, try these challenges:
- Print your name and favorite programming language
- Print multiple lines using separate print() statements
- Print a simple calculation like print(2 + 3)
You've just taken your first step into Python programming. Every complex program starts with a simple print() statement — keep experimenting!
Interactive Views
You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.
A Hello World script is the simplest Python program that prints text to the screen, confirming your environment works.
🖥️ Example 1: The simplest Hello World
This example prints the classic greeting using Python's built-in print() function.
print("Hello World")
📤 Output: Hello World
📝 Example 2: Hello World with a variable
This example stores the greeting text in a variable before printing it.
greeting = "Hello World"
print(greeting)
📤 Output: Hello World
👤 Example 3: Hello World with a name
This example prints a personalized greeting by combining text with a variable.
name = "Engineer"
print("Hello " + name)
📤 Output: Hello Engineer
🔤 Example 4: Hello World with formatted output
This example uses an f-string to insert a variable directly into the greeting text.
name = "Python"
print(f"Hello {name}")
📤 Output: Hello Python
🌍 Example 5: Hello World with user input
This example asks the user for their name and prints a personalized greeting.
user_name = input("Enter your name: ")
print(f"Hello {user_name}, welcome to Python!")
📤 Output: Hello [user input], welcome to Python!
Comparison Table
| Example | Technique Used | Output Type |
|---|---|---|
| 1 | Direct string in print() | Static text |
| 2 | Variable with print() | Static text |
| 3 | String concatenation | Dynamic text |
| 4 | f-string formatting | Dynamic text |
| 5 | User input + f-string | Interactive text |