Your First Python Script: "Hello, World!" (Part 2)
Following our exploration of Your First Python Script: "Hello, World!" (Part 1), this article delves deeper into the structure of a Python script and the versatile print() function.
📚 Prerequisites
Having written and run your first "Hello, World!" script.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ Basic Python script structure.
- ✅ The importance of comments in your code.
- ✅ The
print()function's parameters. - ✅ How to print multiple items and customize the output.
🧠 Section 1: Basic Python Script Structure
While a simple one-line script works, it's good practice to structure your scripts for readability and maintainability. A well-structured script typically includes:
- Comments: Explanations of what your code does.
- Import statements: To bring in code from other modules.
- Constants: Variables whose values don't change.
- Functions and classes: To organize your code into reusable blocks.
- Main execution block: The entry point of your script.
For now, we'll focus on comments and the main execution block.
💻 Section 2: The Importance of Comments
Comments are lines in your code that are ignored by the Python interpreter. They are for humans to read and understand the code. In Python, comments start with a hash symbol (#).
# This is a comment. It won't be executed.
print("Hello, World!") # This is an inline comment.
It's good practice to add comments to your code to explain complex parts or the overall purpose of the script.
🛠️ Section 3: A Deeper Dive into the print() Function
The print() function is more powerful than it first appears. It has several optional parameters that allow you to customize its behavior.
The full syntax is: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Let's break down the most important parameters:
*objects: You can pass multiple items toprint(), separated by commas.sep: The separator between the items. The default is a space.end: What to print at the end. The default is a newline character (\n).
🔬 Section 4: Customizing Your Output
Let's see how we can use these parameters to customize our output.
Printing Multiple Items:
print("Hello", "from", "Python!")
# Output: Hello from Python!
Using a Custom Separator:
print("apple", "banana", "cherry", sep=", ")
# Output: apple, banana, cherry
Printing Without a Newline:
print("Hello", end=" ")
print("World!")
# Output: Hello World!
💡 Conclusion & Key Takeaways
You've now learned how to structure a simple Python script and how to use the print() function to produce formatted output.
Let's summarize the key takeaways:
- Comments: Use
#to add comments to your code. print()parameters:sepandendare useful for customizing output.- Multiple arguments: You can print multiple items by separating them with commas.
Challenge Yourself: Write a script that prints your name, age, and favorite color on a single line, separated by hyphens.
➡️ Next Steps
In the next article, we'll start exploring the fundamental building blocks of Python: "Variables and Assignment".
Happy coding!
Glossary (Python Terms)
- Comment: A line of text in a program that is ignored by the interpreter.
- Parameter: A variable in a function definition.
- Argument: A value passed to a function when it is called.