Your First Python Program
A teen-friendly Python tutorial: write your first program, use print, variables, input, and basic math, and understand how Python runs your code. With runnable examples and a quiz.
Key takeaways
- print() displays output; strings go in quotes
- Variables store values and are assigned with =
- input() reads text typed by the user, always as a string
- Use int() or float() to convert text into numbers for math
- Python runs your code top to bottom, one line at a time
Why Python?
Python is one of the most popular programming languages in the world. It's used for websites, games, data science, and the artificial intelligence behind tools you use every day. It's also a fantastic first language because the code reads almost like English.
To follow along, you can use any online Python editor in your browser, or install Python for free from python.org.
Hello, world!
By tradition, your first program in any language prints "Hello, world!". In Python it's a single line:
print("Hello, world!")
Run it, and you'll see:
Hello, world!
print() is a function. It displays whatever you put inside the parentheses. The text in quotes is called a string — the quotes tell Python "this is text," and they are not printed.
Variables store information
A variable is a named box that holds a value. You create one with =:
name = "Maya"
age = 14
print(name)
print(age)
Output:
Maya
14
Here name holds a string and age holds a number. The = means assign, not "equals" in the math sense — it puts the value on the right into the variable on the left.
You can combine variables with text:
name = "Maya"
print("Hi, my name is", name)
This prints Hi, my name is Maya. The comma in print() adds a space between the parts.
Doing math
Python is a great calculator. It uses these operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 3 + 4 | 7 |
- | subtract | 10 - 6 | 4 |
* | multiply | 5 * 2 | 10 |
/ | divide | 9 / 2 | 4.5 |
// | whole divide | 9 // 2 | 4 |
% | remainder | 9 % 2 | 1 |
Just like in math class, multiplication and division happen before addition and subtraction:
print(3 + 4 * 2)
This prints 11, because 4 2 is calculated first (8), then 3 + 8. Use parentheses to change the order: (3 + 4) 2 gives 14.
Getting input from the user
Programs get more fun when they respond to people. The input() function reads text the user types:
name = input("What is your name? ")
print("Nice to meet you,", name)
When you run this, the program waits, you type your name, press Enter, and it greets you.
Important: input() always gives you a string, even if the user types digits. So this code has a bug:
age = input("How old are you? ")
print(age + 1) # ERROR! Can't add a number to text
To do math, convert the string to a number with int() (for whole numbers) or float() (for decimals):
age = int(input("How old are you? "))
print("Next year you will be", age + 1)
Now age is a real number, and the math works. This is the same idea you'll use in Loops: Making the Computer Repeat when counting.
Putting it together
Here's a small complete program that uses everything above:
name = input("What is your name? ")
year = int(input("What year were you born? "))
age = 2026 - year
print("Hello,", name + "!")
print("You are about", age, "years old.")
Try running it. It asks two questions, does some math, and prints a friendly result.
How Python runs your code
Python reads your file top to bottom, running one line at a time. Each line finishes before the next begins. This order is exactly the "instructions in sequence" idea from coding's very beginning — clear steps, in the right order.
Practice challenges
- Write a program that asks for two numbers and prints their sum.
- Make a program that asks for a temperature in Celsius and prints it in Fahrenheit (
F = C * 9 / 5 + 32). - Greet the user, then tell them how many days old they are.
When you're comfortable, try building something visual in Build a Simple Web Page with HTML.
Quick quiz
Test yourself and earn XP
What does this print? ``` print("Hello, world!") ```
print() displays whatever is inside the parentheses; the quotes mark a string and are not printed.
What type of value does input() return?
input() always returns a string, even if the user types digits. Convert with int() or float() to do math.
After `age = 14`, what does `print(age)` show?
age is a variable holding the value 14, so print(age) displays 14.
What is the result of `print(3 + 4 * 2)`?
Multiplication happens before addition, so 4 * 2 = 8, then 3 + 8 = 11.
Why does `int(input())` work but `input()` alone fail for math?
input() returns a string. int() converts that string into an integer so you can do arithmetic.
FAQ
You can run Python online in a browser-based editor with no install, or download Python for free from python.org and run it on your own computer.
Python uses simple, readable words and clean syntax, so beginners can focus on ideas instead of fighting punctuation. It's also widely used in real jobs and in AI.
Keep exploring
More in Coding