Write Your First Python Program
Write, run and fix real Python code in your browser, no installation needed
Writing your first program is the moment programming stops being abstract. In 30 minutes you will write, run and fix real Python programs directly on this page, then test yourself with an auto-graded quiz.

Learning objectives
- Explain what a program is and the input, processing, output pattern
- Display information with print()
- Store values in variables and read user input with input()
- Convert text to numbers with int() and float()
- Build expressions with Python operators
- Read an error message and fix the code
Writing your first program is the moment programming stops being abstract. In 30 minutes you will write, run and fix real Python programs right on this page, with nothing to install. At the end, an auto-graded quiz will tell you whether you are ready for the next step.
- Time: 30 minutes
- Level: complete beginner
- Prerequisites: none
- You need: a web browser
1. What is a Python program?
A program is a list of instructions that the computer runs in order, line by line. Python is known for its readable syntax: it looks a lot like plain English, which makes it one of the best languages for beginners. It is used to automate tasks, analyze data, build websites and power artificial intelligence.
Almost every program follows the same three steps:
- Input: the program receives some information (a name, a number).
- Processing: it calculates or transforms that information.
- Output: it displays a result.
Keep this pattern in mind: you will apply it in every example of this lesson.
2. Where do you write Python?
You have three options. For this lesson, the first one is enough.
- The editors on this page: they run real Python in your browser, nothing to install.
- Python on your computer: download it for free from python.org, then write your programs in IDLE (included with Python) or in Thonny, which is very beginner-friendly.
- A code editor such as Visual Studio Code, when you are ready for bigger projects.
A Python program is simply a text file ending in .py, for example hello.py.
3. Your first program: print()
The print() function displays information on the screen. The text to display goes between quotation marks, inside the parentheses:
print("Hello, world!")
Let's break this line down:
printis the name of the function: it is the order you give to Python.- The parentheses hold what the function must handle.
- The quotation marks tell Python this is text (called a "string").
Open the interactive activities below
Try this: add a second line print("I am learning Python"). Python runs it right after the first one: the order of the lines matters.
4. Variables and input(): talking to the user
A variable is a named box that keeps a value in memory. You create one with the = sign. The input() function asks the user a question and collects the answer:
name = input("Your first name: ")
print("Hello", name)
This program follows the input → processing → output pattern exactly: it asks for a first name, stores it in the variable name, then displays it. When you click Run, a pop-up window will ask you to type your name.
Open the interactive activities below
Run it once with "Lina", then with another name. The message changes: the variable is really being used. This concrete test confirms that the program works for several cases, not just one.
Choosing good variable names
- Use letters, digits and the underscore:
total_price. - Never start with a digit:
2timesis not allowed. - Pick a meaningful name:
ageis better thana. - Python is case-sensitive:
Nameandnameare two different variables.
5. The classic trap: input() returns text
Here is the mistake almost every beginner makes. input() always returns text, even when the user types a number. To calculate, you must convert that text into an integer with int().
Open the interactive activities below
Without conversion, Python glues two texts together: "5" and "3" give "53". With int(), they are real numbers and the addition gives 8. If the user may type a decimal number, use float() instead.
6. Calculating with expressions
An expression combines values and operators. Python follows the usual order of operations from mathematics: parentheses first, then powers, then multiplication and division, and finally addition and subtraction.
| Operator | Role | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
// | Integer division | 7 // 2 | 3 |
% | Remainder | 7 % 2 | 1 |
** | Power | 7 ** 2 | 49 |
A concrete example: calculating the amount of an order.
price = float(input("Unit price: "))
quantity = int(input("Quantity: "))
total = price * quantity
print("Total:", total)
Open the interactive activities below
Testing several cases, including edge cases like 0, is the best way to catch a mistake before someone else finds it.
7. The most common errors
An error is not a failure: it is a message telling you what is wrong and, above all, on which line. Learn to read them.
| Message | Typical cause | Fix |
|---|---|---|
SyntaxError | Missing quote or parenthesis: print("Hi) | Close the quote and the parenthesis |
NameError | Unknown variable or typo: pritn("a") | Check the spelling and that the variable exists |
TypeError | Mixing text and number: "Age: " + 12 | Convert with str() or use a comma in print |
ValueError | Impossible conversion: int("abc") | Check what the user typed |
IndentationError | Unwanted spaces at the start of a line | Align the lines to the left |
Open the interactive activities below
8. Practice exercises
Complete each line, then check your answer. The feedback explains the solution.
Open the interactive activities below
9. Auto-graded challenges
Write a real program. The grader runs it with several sets of data and compares the expected result with your program's output.
Challenge 1: the greeting
Ask for a first name with input(), then display exactly Hello, Lina! if the name is Lina (same format for any other name).
Open the interactive activities below
Challenge 2: the sum
Read two whole numbers with input() and display only their sum. Remember the trap from section 5.
Open the interactive activities below
10. Final quiz: test what you learned
Ten questions, one correct answer each. You get instant grading and every answer is explained.
Open the interactive activities below
Key takeaways
print()displays information on the screen.input()collects what the user types, always as text.int()converts numeric text into an integer,float()into a decimal number.- An expression combines values and operators, following the order of operations from mathematics.
- Testing several cases, including 0, helps you catch mistakes.
- An error message tells you the type of problem and the line concerned.
Frequently asked questions
Is Python free?
Yes. Python is free, open-source software available on Windows, macOS and Linux.
Do I need to install Python to get started?
No. The editors on this page run real Python in your browser. You can install Python later, when you want to save your programs on your own computer.
Why does input() return text instead of a number?
Because Python cannot guess what the user will type: a name, a number, a sentence. Everything is received as text, and it is up to the programmer to convert it with int() or float() when needed.
How do I run a .py file on my computer?
Open the file in IDLE or Thonny and click "Run", or type python file_name.py in a terminal.
How long does it take to learn Python?
The basics (variables, input, output, calculations) take a few hours. Practicing a little every day works better than long, infrequent sessions.
You can now display, read and calculate. Keep the momentum: redo the challenges tomorrow without looking at the lesson, then continue with the other programming lessons. You can also retake the related quiz to consolidate.
Interactive activities
Definitions
Program
A list of instructions that the computer runs in order, line by line.
Variable
A named box that stores a value in memory, created with the = sign.
String
A piece of text written between quotation marks.
Function
A named instruction, such as print() or input(), that performs a task.
Expression
A combination of values and operators that Python evaluates to a result.
Syntax error
An error raised when the code is not written according to Python's rules.
Key formulas
Division with remainder
In Python, q = a // b and r = a % b. Example: 17 = 5 × 3 + 2.
Order of operations
Python multiplies before adding, like in mathematics.
Common mistakes to avoid
- Forgetting to close a quotation mark or a parenthesis (SyntaxError)
- Misspelling a function or variable name (NameError)
- Adding text and a number without converting (TypeError)
- Forgetting that input() always returns text
- Using int() on text that is not a number (ValueError)
- Adding spaces at the start of a line (IndentationError)
Key takeaways
- print() displays information on the screen
- input() always returns text
- int() and float() convert text into numbers
- Test several cases, including 0
- Error messages tell you the type of problem and the line