Your First Python ProgramGet Certified
What Is a Program?
A program is a set of instructions that tells a computer what to do and in what order. Each instruction is called a statement, and Python executes these statements to produce a result. Programs can be very small, such as displaying a greeting, or much larger, combining many tasks and decisions. Starting with a simple example makes it easier to understand how code is written and executed.
Almost every programmer starts with the same tiny program, called Hello, World!. It simply shows a message on the screen. Writing it proves that your setup works and teaches the most important tool for seeing results: the print() function.
Your First Program: Hello, World!
The classic first Python program displays the message Hello, World! on the screen. You create it by calling the built-in print() function and placing the message inside its parentheses. The quotation marks tell Python that the message is text rather than a variable or command. Run the example, check the output, and then change the message to see how a small edit changes the result.
Try It: Hello, World!
Click Test My Code to open the editor, then click Run Code. The output appears in the Console Output panel. Try changing the message and running it again.
print() twice. Each call displays its text on a separate line of the output.# My first Python program
print("Hello, World!")
print("I am writing my first Python program.")# symbol starts a comment. Python ignores everything after it on that line, so comments are only notes for people who read the code.print() is a built-in function that displays values on the screen. The text "Hello, World!" is a string, which is why it is surrounded by quotation marks. The quotation marks are not shown in the output.print(). Each call to print() starts a new line in the output.The Anatomy of a print() Statement
A print() statement is made up of a function name and parentheses containing the value or values to display. Text must be enclosed in matching quotation marks, while numbers and mathematical expressions can be written directly. Python evaluates expressions before displaying their results, which makes print() useful when checking calculations. Understanding these small parts helps you write statements accurately and recognize common syntax mistakes.
print: the name of the function. Names in Python are case-sensitive, soPrintorPRINTwill not work.- Parentheses
( ): they hold the values you give to the function. They are required in Python 3. - Quotation marks: single
'...'or double"..."quotes mark text. Use the same kind to open and close the text. - Values without quotes: numbers and calculations such as
7 + 3are written without quotes, and Python works out the result.
Try It: Print Text, Numbers and Calculations
The print() function can show text, numbers, and the results of calculations. It can also join several values with a separator and control what comes at the end of the line.
sep, and uses end so that two print() calls appear on the same line.# Printing text, numbers and calculations
print("Python is fun")
print(2026)
print(7 + 3)
print("7 + 3 =", 7 + 3)
print("Python", "is", "easy", sep="-")
print("Loading", end="...")
print("done")print() can display."Python is fun" is displayed exactly as written, without the quotation marks.print(2026) displays 2026.7 + 3 first and then prints the result, 10. It does not print the expression itself, because it is not inside quotes.print() several values separated by commas, it displays them with a space between each one. The output is 7 + 3 = 10: the first value is text, and the second is the calculated result.sep="-" replaces the default space between values with a hyphen. The output is Python-is-easy.print() ends with a new line. end="..." replaces that new line with three dots, so the next output continues on the same line.print() uses the default ending. Because the previous call did not end with a new line, the output reads Loading...done on one line.Running a Python Program on Your Computer
After trying Python in the browser, you can run the same program locally on your computer. A Python source file uses the .py extension, and it can be opened or edited with a basic text editor or a development environment. You can start the file from a terminal with the Python command or use the Run option in an IDE. Running a local file is useful for saving your work and building programs beyond the examples on this page.
- Create a file: open an editor or IDE and save a new file named
hello.py. The.pyending tells your system that it is a Python file. - Write the code: type
print("Hello, World!")in the file and save it. - Run it from a terminal: open a terminal in the folder that holds the file and run
python hello.py. On macOS and Linux usepython3 hello.py, and on Windows you can also usepy hello.py. - Or use your IDE: press the Run button. In IDLE, choose File > New File, write the code, and press F5. In Thonny, click the green Run button. In VS Code, use Run Python File.
- Read the output: the text
Hello, World!appears in the terminal or in the IDE’s output panel.
Getting Information from the User with input()
The input() function lets a program pause and receive information typed by the person using it. You can include a prompt to explain what information is expected, then assign the response to a variable for later use. The response is returned as a string, so it is treated as text even when the user enters digits. This makes input() a simple way to create interactive programs that respond to different answers.
Try It: Ask for a Name and a City
This program asks two questions. The Program Input box is already filled with the answers Alex and Boston, one answer per line, so you can click Run Code straight away. Then replace them with your own name and city and run it again.
input(), removes extra spaces with strip(), stores the answers in the variables name and city, and then prints a personal greeting that uses both.name = input("What is your name? ").strip()
city = input("Which city do you live in? ").strip()
print("Hello,", name + "!")
print("Welcome to Python from", city + ".")input() shows the prompt and reads the first line from the Program Input box as text. .strip() removes spaces at the start and end of the answer. The result is stored in the variable name using the assignment operator =.city. Each call to input() reads the next available line.+ operator joins two strings together, so name + "!" attaches an exclamation mark directly to the name. The comma in print() adds a space between Hello, and the name.Try these inputs: run the code with the defaults, then type your own name and city in Program Input and run it again. Finally, delete the second line and run it once more. The program asks for two answers but receives only one, so Python reports that the input ended too early.
Working with Numbers from input()
When a program needs to calculate with information entered by a user, the input text must be converted into a numeric type. Use int() when you expect a whole number and float() when decimal values may be entered. After conversion, Python can perform arithmetic such as addition, subtraction, multiplication, and division. If the entered text is not a valid number, conversion raises an error, so later programs can add validation to handle unexpected input.
Try It: A Simple Calculator
The Program Input box is already filled with the numbers 6 and 2.5, one per line, so you can click Run Code straight away. Then change the numbers and run it again.
float(), calculates their sum and product, and prints both results with labels.first = float(input("Enter the first number: "))
second = float(input("Enter the second number: "))
total = first + second
product = first * second
print("Sum:", total)
print("Product:", product)input() reads the typed text, and float() converts that text into a decimal number. The number is stored in first.second. It reads the next line of the Program Input box.+ operator now adds two numbers, because both values were converted. The result is stored in total.* operator multiplies the two numbers. The result is stored in product.print() shows the label Sum: followed by the value of total. For the defaults 6 and 2.5, the output is 8.5.Product: and the value of product. For the same inputs, the output is 15.0.Tip: If the Program Input box contains text that is not a number, such asabc,float()stops the program with aValueError. Later lessons on exceptions show how to handle that gracefully.
How Python Reads Your Program
Python normally executes a program from the first statement to the last, following the order in which the instructions appear. It evaluates each statement when it reaches it, so an earlier calculation or input can affect what happens next. Comments and blank lines help people understand the code but do not produce output or change the program’s flow. Knowing this execution order makes it easier to predict results and trace a program when something does not work as expected.
- Top to bottom: Python runs one statement at a time, in the order you wrote them.
- One statement per line: by default, each line holds one instruction.
- Blank lines and comments are skipped: they help people read the code and do not change the result.
- Output appears as it happens: each
print()displays its text when Python reaches it. - Errors stop the program: Python reports the problem and the line where it happened, and statements after that line do not run.
Common Beginner Errors and How to Fix Them
Small typing mistakes can prevent a Python program from running, especially when you are learning the language’s syntax. Missing quotation marks or parentheses, incorrect capitalization, unexpected indentation, and mixing incompatible data types are common examples. When an error appears, read its type and message, then inspect the indicated line and the code immediately around it. Fix one issue at a time and run the program again to confirm the change.
- SyntaxError (missing quote or parenthesis):
print("Hello)has an unclosed string. Make sure every opening quote and every opening parenthesis has a matching closing one. - NameError:
print(Hello)without quotes makes Python look for a variable calledHello. Put text inside quotation marks. - Wrong capitalization:
Print("Hi")fails because Python is case-sensitive. Built-in function names are written in lowercase. - IndentationError: an unexpected space at the start of a line causes this error. Start simple statements at the left margin.
- TypeError when mixing text and numbers:
"Age: " + 25fails because a string cannot be joined with a number. Use a comma insideprint(), or convert the number withstr().
Read the last line of an error message first. It names the error type and usually explains what went wrong, and the line number tells you where to look.
Practice Ideas
Practice helps turn the basic statements in this lesson into skills you can use independently. Start by changing the messages in the examples, then combine print() and input() to make a short interactive program. Try using numbers entered by the user in a simple calculation and display the result with a clear label. These small experiments encourage you to modify code, observe the output, and learn from mistakes.
- Change the greeting in the first example to show your own name.
- Print a three-line poem or a short shopping list using three
print()calls. - Change the calculator to also print the difference between the two numbers.
- Ask for the user’s favorite color and print a sentence that uses it.
Key Takeaways
This lesson introduced the basic steps involved in writing and running a small Python program. You have seen how to display information with print(), collect responses with input(), and convert text into numbers when calculations are needed. You also learned that Python follows the statements in order and reports errors when it cannot execute an instruction. Keep these ideas in mind as you move on to variables, data types, and more complex programs.
- A program is a sequence of statements that Python runs from top to bottom.
print()displays text, numbers, and calculation results. Thesepandendoptions control the layout.- Text must be written inside quotation marks, and Python is case-sensitive.
input()reads what the user types and always returns text. Useint()orfloat()to convert it to a number.- You can run programs in this website’s editor, from a terminal with
python hello.py, or with the Run button of an IDE. - Error messages are helpful. Read them carefully to find and fix mistakes.
Frequently Asked Questions
These frequently asked questions review the main ideas from your first Python program. Use the answers as a quick reference when you need to remember how to display text, run a file, or collect user input. If you are still unsure about a step, return to the matching example and run it with a small change. Practising each concept is the best way to become comfortable writing Python code.
What is the first program in Python?
The traditional first program is Hello, World!, written as print("Hello, World!"). It displays a message on the screen and confirms that Python is working. It also introduces the print() function, which you will use constantly.
How do I run a Python program?
Save your code in a file that ends in .py, such as hello.py, and run python hello.py in a terminal. On macOS and Linux use python3. You can also press the Run button in an IDE such as IDLE, Thonny, VS Code, or PyCharm, or use the editor on this website.
What does the print() function do?
The print() function displays values on the screen. It can show text, numbers, and the results of calculations. By default it separates several values with a space and ends with a new line, and you can change this with the sep and end options.
What does input() do in Python?
The input() function displays an optional prompt, waits for the user to type a line, and returns that line as text. You usually store the result in a variable, for example name = input("Your name: ").
Why does input() always return text?
Python cannot know whether the user means a number, a word, or something else, so it returns exactly what was typed as a string. To calculate with the value, convert it with int() for whole numbers or float() for decimals, for example age = int(input("Age: ")).
Why do I need quotation marks around text in print()?
Quotation marks tell Python that the characters are text and not a name or a command. Without them, Python treats the word as a variable name and raises a NameError if no such variable exists. You can use single or double quotes, as long as the opening and closing marks match.
Is Python case-sensitive?
Yes. print, Print, and PRINT are three different names, and only the lowercase print is the built-in function. The same rule applies to variable names, so name and Name refer to different variables.
What is a SyntaxError?
A SyntaxError means Python could not understand the code because it breaks the rules of the language, such as a missing closing quotation mark or parenthesis. Python stops before running the program and points to the line where it noticed the problem, so check that line and the one just before it.