Learn Python: Basic Python Syntax and Hello World
Python is recognized for its clean and intuitive syntax. Instead of curly braces to group code, Python relies on indentation (spaces or tabs) to show which lines belong together in a block (like for loops or function definitions). At this stage, you only know how to display text with the print function and handle newline characters for multi-line output. This lesson focuses on how Python structures code and how to craft a well-formatted “Hello, World!” program within Visual Studio Code (VS Code).
What will be covered
• The role of indentation in Python and why it’s mandatory.
• How to create multi-line output using the print function.
• Basic case sensitivity and why Python treats differently cased identifiers as separate.
• Writing an expanded “Hello, World!” program that demonstrates clean formatting.
Indentation and Code Blocks
Many languages use braces (like { }) to indicate the start and end of a code block. Python doesn’t. Instead, it enforces indentation. If a set of lines is meant to be treated as one block, they are indented by the same amount. If any line in that block is aligned differently, Python flags an IndentationError. While you’ve not yet learned loops or functions, this rule still matters any time Python expects lines to be grouped. Even now, if you accidentally indent a print statement, Python assumes it belongs to a block you haven’t defined, causing an error.
Case Sensitivity
Python is case sensitive. That means print is not the same as Print. If you capitalize it incorrectly, you’ll get an error because Python thinks you’re calling something else. This concept applies to everything in Python, from functions to potential names you’ll learn in future lessons. For now, just remember to type print in lowercase, exactly as shown.
Multi-Line Output
You already know how to write a single print statement with a string. But if you want to show multiple lines, you have a few options:
- Use separate print statements for each line.
- Insert \n (newline character) in your string to break lines.
- Use triple-quoted strings (""") to span multiple lines in one go (optional if you prefer a single approach for now).
Expanded Hello World
Below is a short script showing how to print a few lines. Open VS Code, create a file named hello_world.py, and run it in your terminal using python hello_world.py.
print("Hello, World!")
print("Python uses indentation to group code.")
print("Case sensitivity means 'print' != 'Print'.")
print("You can print multiple lines:")
print("First line\nSecond line\nThird line")
The script first prints a couple of single-line messages. Then it demonstrates multi-line output by embedding \n into the string. Run the file and confirm the lines appear exactly as intended. If you introduce an accidental indentation on any line, Python complains because it expects a context like a loop or function definition (which you haven’t learned yet).
Visual Overview
This diagram shows the simple flow for your script:
Python line-by-line executes each print, producing console output. Any indentation issue immediately triggers an error, reinforcing how integral spacing is to Python’s syntax.
Practice Exercises
- Create a file named greetings.py. Write three print statements. The first says "Hello from my new Python script," the second says "This is line two," and the third says "End of lines." Run it to confirm you see all three lines in your terminal.
- Introduce an indentation on the second print (like four spaces at the start). Run the script and note Python’s error message. Remove the indentation and rerun successfully.
- Make a file named multiline_example.py. Print a short paragraph (like two or three lines) using the embedded newline character \n or multiple print statements. Confirm the spacing meets your expectation.
- Optional experiment: use triple quotes (""") to create a multi-line string in one print. Compare the output with using several separate print calls. Decide which style you find clearer.
- Check case sensitivity by misspelling print in at least one line, for example Print("Testing"). See the error, then correct it. This demonstrates why exact spelling matters.
Summary
By exploring indentation, multi-line printing, and the importance of case sensitivity, you have reinforced Python’s fundamental syntax rules. This is the core for writing clear, readable code. Even though you have not learned about advanced structures yet, mastering these essentials will help you avoid common pitfalls and errors. You now have the ability to create and run small Python scripts in VS Code, printing text with complete control over spacing and line breaks. As you progress, you’ll apply these syntax principles to more sophisticated tasks like loops, conditionals, and beyond.