Now that your environment is set up, it's time to write your first Python program. The traditional 'Hello, World!' program is a great starting point to understand basic Python syntax.
Writing 'Hello, World!'
-
Open your editor: Launch VS Code, PyCharm, or your chosen code editor.
-
Create a new file: Save it as
hello_world.py. The.pyextension is crucial as it tells the operating system and Python interpreter that this is a Python script. -
Write the code: Type the following single line into your file:
pythonprint("Hello, World!") -
Save the file.
Running Your Program
-
Using a Terminal: Open a terminal or command prompt, navigate to the directory where you saved
hello_world.py, and run it using:bashpython hello_world.py # or if 'python' refers to python2 on your system: python3 hello_world.py -
Using VS Code: If you have the Python extension installed, you can usually click the 'Run Python File' button in the top-right corner or right-click in the editor and select 'Run Python File in Terminal'.
-
Using PyCharm: Right-click on the
hello_world.pyfile in the project explorer and select 'Run 'hello_world''.
Understanding the Code
print(): This is a built-in Python function. Functions are blocks of organized, reusable code that perform a single, related action. Theprint()function's job is to display output to the console."Hello, World!": This is a string literal. A string is a sequence of characters, and in Python, strings are enclosed in either single quotes (') or double quotes ("). In this case, 'Hello, World!' is the argument passed to theprint()function.
Basic Python Syntax Rules
-
Indentation Matters: Python uses indentation (whitespace at the beginning of a line) to define code blocks, unlike many languages that use curly braces
{}. Consistent indentation (usually 4 spaces) is critical for Python code to run correctly.pythonif True: print("This line is indented by 4 spaces") else: print("This line is also indented by 4 spaces") # Error if not indented correctly -
No Semicolons: Unlike C++ or Java, Python statements do not require semicolons at the end. Each line is typically a new statement.
-
Comments: Comments are non-executable lines in your code, used to explain what the code does. They are vital for readability and maintainability.
- Single-line comments: Start with a hash symbol (
#).python# This is a single-line comment print("Hello") # This prints 'Hello' - Multi-line comments (docstrings): While Python doesn't have a dedicated multi-line comment syntax like
/* ... */, you can use triple quotes ("""or''') for multi-line strings that aren't assigned to a variable. These are often used for documentation strings (docstrings) for functions, classes, and modules.python"""This is a multi-line comment. It can span several lines. It's often used as a docstring. """ print("World")
- Single-line comments: Start with a hash symbol (
-
Case Sensitivity: Python is case-sensitive.
myVariableis different frommyvariable.
By following these basic syntax rules, you're well on your way to writing understandable and functional Python code. The print() function will be your best friend for debugging and understanding program flow as you learn.
Key Takeaways:
- Python files use the
.pyextension. - The
print()function outputs text to the console. - Python relies on indentation to define code blocks.
- Use
#for single-line comments and"""for docstrings/multi-line comments. - Python is case-sensitive.