Module 1 · Beginner · Getting started

Your first Python program, line by line

Write your first Python program step by step: print, variables, input, maths and f-strings, explained line by line, then improve it with if and loops.

By theprogrammers team · · 6 min read

Everyone's first program is print("Hello, world!"). It proves Python works, but it doesn't teach much. In this lesson you'll build something a little more useful: an age calculator that asks for your name and birth year and tells you how old you'll be at the end of the year.

It's only seven lines, but they use the ideas that every Python program is built from: output, variables, input, type conversion, maths and formatted text. We'll go through it line by line, then improve it.

You'll need Python and an editor set up. If you haven't done that, follow how to install Python and set up VS Code.

The finished program

Create a file called age.py and type this in. Type it, don't copy it: typing is how your fingers and brain learn the patterns.

# Age calculator: my first Python program
print("Welcome to the age calculator!")

name = input("What is your name? ")
birth_year = int(input("What year were you born? "))

age = 2026 - birth_year
print(f"Hi {name}, you will be {age} at the end of 2026.")

Run it (the ▶ button in VS Code, or python age.py in a terminal) and answer the questions:

Welcome to the age calculator!
What is your name? Ahana
What year were you born? 2006
Hi Ahana, you will be 20 at the end of 2026.

Now let's see what each line does.

Line 1: a comment

# Age calculator: my first Python program

Anything after a # is a comment. Python ignores it completely; it's a note for humans. Use comments to explain what a program is for or why a tricky line exists.

Line 2: printing text

print("Welcome to the age calculator!")

print() is a function: a ready-made command that does a job. Its job is to show whatever you put inside the brackets. The text is in quotes, which makes it a string. Without quotes, Python would think Welcome is the name of something and give an error.

Line 4: asking a question

name = input("What is your name? ")

Two things happen here, right to left:

  1. input() shows the question, waits for the user to type something and press Enter, and gives back what they typed.
  2. name = stores that answer in a variable called name, so we can use it later.

Think of a variable as a label stuck on a value. Notice the space before the closing quote: it stops the answer from being squashed against the question.

Line 5: converting text to a number

birth_year = int(input("What year were you born? "))

input() always gives back text, even if the user types digits. "2006" in quotes is a string, and you can't subtract a string from a number. int() converts it to the whole number 2006.

Python runs the innermost brackets first: input() asks the question, then int() converts the answer, then = stores the result.

Line 7: doing maths

age = 2026 - birth_year

Python calculates 2026 - 2006, gets 20, and stores it in age. You can use +, -, * (multiply) and / (divide) like a calculator.

Line 8: putting values into text

print(f"Hi {name}, you will be {age} at the end of 2026.")

The f before the quotes makes this an f-string. Anything inside curly braces is replaced by its value, so {name} becomes Ahana and {age} becomes 20. It's the easiest way to mix text and variables.

What Python actually does

Python runs a program from top to bottom, one line at a time. When it reaches input(), it pauses until the user presses Enter. When it reaches the last line, the program ends. Blank lines are ignored; they're there to make the code easier to read.

Making it better

Real programs grow step by step. Let's improve ours.

Avoid a hard-coded year

Writing 2026 means the program will be wrong next year. Python's datetime module knows today's date:

from datetime import date

this_year = date.today().year
age = this_year - birth_year

React to the answer

An if statement lets the program make a decision:

if age >= 18:
    print("You are old enough to vote.")
else:
    print(f"You can vote in {18 - age} years.")

The indented lines belong to the if or the else. Python if, elif and else covers decisions in depth.

The improved program

# Age calculator, version 2
from datetime import date

print("Welcome to the age calculator!")
name = input("What is your name? ")
birth_year = int(input("What year were you born? "))

this_year = date.today().year
age = this_year - birth_year
print(f"Hi {name}, you will be {age} at the end of {this_year}.")

if age >= 18:
    print("You are old enough to vote.")
else:
    print(f"You can vote in {18 - age} years.")

What if something goes wrong?

Type twenty instead of a year and the program crashes with:

ValueError: invalid literal for int() with base 10: 'twenty'

That's Python telling you int() couldn't turn "twenty" into a number. Errors aren't failures; they're messages. Our next lesson, Python error messages explained, shows how to read them.

Key takeaways

  • print() shows output; input() asks the user and always returns text.
  • A variable stores a value under a name with =.
  • int() converts text to a whole number so you can do maths with it.
  • f-strings such as f"Hi {name}" put values into text.
  • Python runs a program from top to bottom, one line at a time.

Practice

  1. Change the program so it also prints the age in months.
Show the answer
print(f"That's about {age * 12} months.")
  1. Write bill.py: ask for the price of an item and the quantity, then print the total.
Show the answer
price = float(input("Price: "))
quantity = int(input("Quantity: "))
print(f"Total: ₹{price * quantity}")

float() converts to a decimal number, so prices like 49.5 work.

  1. Write a program that asks for a temperature in Celsius and prints it in Fahrenheit, using F = C × 9 / 5 + 32.
Show the answer
celsius = float(input("Temperature in °C: "))
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius}°C is {fahrenheit}°F")

For 37, it prints 37.0°C is 98.6°F.

  1. What's wrong with this line, and how do you fix it?
year = input("Year: ")
print(year + 1)
Show the answer

input() returns a string, and you can't add a number to a string, so Python raises a TypeError. Convert first: year = int(input("Year: ")).

Interview questions

What does the input() function return?

Always a string (str), whatever the user types. To use the answer as a number, convert it with int() or float().

What is the difference between print() and return?

print() shows a value on the screen for a human to read. return sends a value back from a function to the code that called it, so the program can keep using it. A function that prints but doesn't return gives back None.

How does Python execute a script?

From top to bottom, one statement at a time. Python first compiles the file to bytecode, then runs it. Code inside a function only runs when the function is called, and an unhandled error stops the program at that line.

What to learn next

  1. Learn to read errors without panicking: Python error messages and tracebacks explained.
  2. Go deeper into storing values with Python variables and data types.