Your First Python Program
Step-by-Step Guide to Writing Your First Python Program
- Install Python:
- First, make sure Python is installed on your computer. You can download it from python.org.
- Open a Text Editor or IDE:
- You can use any text editor (like Notepad on Windows, TextEdit on macOS, or gedit on Linux) or an Integrated Development Environment (IDE) like PyCharm, VS Code, or even the built-in IDLE that comes with Python.
- Write Your Code:
- Let’s start with the traditional first program, the “Hello, World!” example. Open your text editor or IDE and type the following code:
0 1 2 3 4 5 6 |
print("Hello, World!") |
- Save Your File:
- Save the file with a
.py
extension. For example, you can save it ashello.py
.
- Save the file with a
- Run Your Program:
- To run your program, open a terminal or command prompt.
- Navigate to the directory where your
hello.py
file is saved. - Type the following command and press Enter:
0 1 2 3 4 5 6 |
python hello.py |
You should see the output:
Hello, World!
Explanation of the Code
- print(“Hello, World!”): This line is a function call in Python. The
print
function outputs the string provided to it within the parentheses. In this case, it printsHello, World!
to the screen.
Additional Tips
- Comments: You can add comments to your Python code using the
#
symbol. Comments are ignored by the Python interpreter and are used to explain the code.
0 1 2 3 4 5 6 7 |
# This is a comment print("Hello, World!") # This prints Hello, World! to the screen |
Variables: You can store values in variables.
0 1 2 3 4 5 6 7 |
message = "Hello, World!" print(message) |
Functions: You can define your own functions to reuse code.
0 1 2 3 4 5 6 7 8 9 |
def greet(name): print(f"Hello, {name}!") greet("Alice") |