Python is a versatile and beginner-friendly programming language that's widely used in various fields like web development, data science, artificial intelligence, and more. If you're new to programming or Python, writing your first program is an exciting step toward unlocking endless possibilities. This tutorial will guide you through creating your first Python program, explaining each step to ensure you understand the process thoroughly.
Python is known for its simplicity and readability, making it an ideal choice for beginners. Its clear syntax allows you to focus on learning programming concepts rather than getting bogged down by complex code. Additionally, Python has a vast ecosystem of libraries and frameworks that support a wide range of applications.
Before writing your first program, you need to set up your Python environment.
python --version
An editor or Integrated Development Environment (IDE) helps you write and manage your code efficiently.
Popular Choices:
For this tutorial, we'll use Visual Studio Code, but you can choose any editor you prefer.
Setting Up Visual Studio Code:
Now that your environment is set up, let's write your first Python program.
The classic first program in any language is "Hello, World!". It simply displays this message on the screen.
Steps:
Write the Code:
print("Hello, World!")
Let's break down the code to understand what it does.
print()
Function:print()
function outputs data to the console.Hello, World!
".'
or "
) is called a string.Hello, World!
" is a string literal.When you run this program, Python executes the print()
function, which outputs the string to the console. It's a simple way to confirm that your Python environment is working correctly.
After writing your code, you need to run it to see the output.
Win + R
, type cmd
, and press Enter.Navigate to Your File's Directory:
Use the cd
command (change directory) to navigate.
cd path_to_your_directory
For example:
cd Desktop
Run the Program:
python hello_world.py
Output:
Hello, World!
hello_world.py
.Ctrl+Shift+P
to open the command palette.The terminal within VS Code will display:
Hello, World!
Now that you've successfully run your first program, let's make it a bit more interactive.
Modify the Program to Greet the User:
Update the Code:
name = input("What is your name? ")
print(f"Hello, {name}! Welcome to Python programming.")
input()
Function:
f"Hello, {name}!"
, {name}
is replaced with the value of the name variableSample Output:
What is your name? Alice
Hello, Alice! Welcome to Python programming.
.py
extension for Python files.print()
Function: Outputs data to the console.input()
Function: Captures user input.Writing your first Python program is a significant milestone in your programming journey. In this tutorial, we walked through setting up your Python environment, choosing an appropriate editor, writing the classic "Hello, World!
" program, and understanding how it works. We then extended the program to make it interactive by introducing user input and variables.
By learning how to write and run a simple Python script, you've laid the foundation for exploring more complex programming concepts. Python's simplicity and readability make it an excellent language for beginners, and as you progress, you'll discover its powerful capabilities for various applications.
Remember, programming is a skill best learned by doing. Keep experimenting with code, try out new ideas, and don't be afraid to make mistakes. Each error is an opportunity to learn something new.