This guide assumes you have already installed Python on your computer. If you haven't, please follow one of our installation guides first:
Let's create a simple "Hello, World!" program - the traditional first program in any programming language.
You can use any text editor to write Python code:
nano hello.py
Type the following code in your text editor:
# This is my first Python program print("Hello, World!") print("Welcome to Python programming!") # Let's make it interactive name = input("What's your name? ") print(f"Nice to meet you, {name}!")
What this code does:
#
are comments - notes for humans that Python ignoresprint()
displays text on the screeninput()
gets text input from the userf"..."
is a format string that lets you include variables in your textSave your file with a .py
extension, which tells your computer it's a Python file. For example: hello.py
Now it's time to run your program! You'll need to use the command line for this.
cd
command
cd path\to\your\folder
For example, if your file is on the Desktop:
cd C:\Users\YourUsername\Desktop
python hello.py
cd path/to/your/folder
For example, if your file is on the Desktop:
cd ~/Desktop
python3 hello.py
When you run your program, you should see:
Hello, World! Welcome to Python programming! What's your name?
Type your name and press Enter. You should then see something like:
Nice to meet you, YourName!
Congratulations! You've just written and run your first Python program!
# This is my first Python program
- This is a comment. Python ignores comments, but they help you understand your codeprint("Hello, World!")
- This prints the text "Hello, World!" to the screenprint("Welcome to Python programming!")
- This prints another message# Let's make it interactive
- Another commentname = input("What's your name? ")
- This asks the user for their name and stores it in a variable called 'name'print(f"Nice to meet you, {name}!")
- This prints a message that includes the name the user enteredprint()
and input()
are built-in commands that perform specific tasksname
store information that you can use later{}
Now try making some changes to see how your program works:
Modify your program to ask the user for their age too. Add these lines after the name input:
age = input("How old are you? ") print(f"Wow, {age} is a great age to learn programming!")
Create a new file called calculator.py
with this code:
print("Simple Calculator") num1 = input("Enter the first number: ") num2 = input("Enter the second number: ") # Convert input strings to numbers num1 = float(num1) num2 = float(num2) sum_result = num1 + num2 print(f"{num1} + {num2} = {sum_result}")
Run this program the same way you ran your first one!
Now that you've run your first Python program, you can: