Your First Python Program
Starting your Python journey begins with writing and running the classic “Hello, World!” program. This simple exercise introduces the basic structure of Python and helps you get comfortable with running Python code.
Writing Your First Python Program
The “Hello, World!” program is a standard introduction to programming. In Python, it involves a single line of code.
Code:
print("Hello, World!")
Explanation:
print()
: This function displays the specified message or output on the screen."Hello, World!"
: The text to be displayed, enclosed in double quotes. Strings in Python can use either single ('
) or double ("
) quotes.
Running Your First Python Program
Step-by-Step Instructions:
Option 1: Using the Command Line/Terminal
- Open a text editor (like Notepad on Windows or TextEdit on macOS in plain text mode) and type the code:
print("Hello, World!")
- Save the file with the extension
.py
. For example:hello_world.py
. - Open the command line (Command Prompt on Windows, Terminal on macOS/Linux).
- Navigate to the directory where the file is saved using the
cd
command:
cd path/to/directory
Run the Python program:
python hello_world.py
Output
Hello, World!
Option 2: Using an IDE (VS Code, PyCharm, or Jupyter Notebook)
Using VS Code
- Open VS Code and create a new file with the extension
.py
(e.g.,hello_world.py
). - Type the code:
print("Hello, World!")
- Save the file and click the green play button in the top-right corner or press
Ctrl + F5
to run the code. - View the output in the terminal.
Using PyCharm
- Create a new Python project and add a new file (e.g.,
hello_world.py
). - Write the code:
print("Hello, World!")
- Right-click on the file and select Run.
- View the output in the PyCharm console.
Using Jupyter Notebook
- Launch Jupyter Notebook in your browser.
- Create a new Python notebook and type the code in a cell:
print("Hello, World!")
Press Shift + Enter
to run the cell and view the output.
Understanding the Basic Structure of Python Code
Python programs are designed to be simple and human-readable. Let’s break down the key features:
Key Features of Python Syntax:
Whitespace Sensitivity:
- Python uses indentation to define blocks of code (like loops or functions), instead of braces
{}
used in other languages. - Example:
- Python uses indentation to define blocks of code (like loops or functions), instead of braces
if True:
print("This is indented correctly")
Case Sensitivity:
- Python is case-sensitive.
Print()
is not the same asprint()
.
- Python is case-sensitive.
Comments:
- Use
#
to add comments. Comments are ignored by the Python interpreter and are useful for explaining code.
- Use
# This is a comment
print("Hello, World!") # This prints a message
Statements:
- Each line of Python code is typically a statement.
- Multiple statements can be written on one line using a semicolon
;
(not recommended for readability).
print("Hello"); print("World")
String Manipulation:
- Strings can use single or double quotes:
print('Hello') # Single quotes
print("World") # Double quotes
Common Errors to Avoid
SyntaxError:
- Example: Missing parentheses around
print
in Python 3:
print "Hello, World!" # SyntaxError
IndentationError:
- Example: Misaligned or missing indentation:
if True:
print("Hello, World!") # IndentationError
Case Sensitivity Issues:
- Example:
Print("Hello, World!") # NameError: 'Print' is not defined
Best Practices
Write Readable Code:
- Use consistent indentation and meaningful comments.
Use an IDE:
- IDEs like VS Code and PyCharm help identify syntax errors and improve productivity.
Run Code Frequently:
- Test your code often to ensure it works as expected.
Follow PEP 8:
- Adhere to Python’s official style guide for clean, maintainable code.