All articles
Programming
10 min read

Python for Beginners: Write Your First Five Programs

By Next Gen Remote Learning editorial team · · For: Students aged 12+ and adults with no programming experience

Python is a popular first programming language because its code reads almost like English and you can see results immediately. This guide assumes no experience. By the end you will have written five short programs, and each one introduces a single new idea.

Before you start: getting Python

Download Python 3 from python.org and install it. On Windows, tick the option to add Python to PATH during installation. Python installs with a simple editor called IDLE, which is enough for this guide. If you can't install software, an online Python editor works too.

Open IDLE, choose File → New File, type a program, save it with a name ending in .py, then press F5 to run it. The output appears in the shell window.

Program 1: Hello, world (output)

print("Hello, world!")
print("I am learning Python.")

print() displays whatever is inside the brackets. Text must be inside quotation marks; this kind of value is called a string. Each print() starts a new line.

Program 2: A personal greeting (variables and input)

name = input("What is your name? ")
print("Nice to meet you, " + name + "!")

A variable is a name that stores a value. Here, input() shows a question, waits for the user to type, and stores the answer in the variable name. The + sign joins strings together.

Program 3: Age next year (numbers)

age = int(input("How old are you? "))
next_year = age + 1
print("Next year you will be", next_year)

input() always gives back a string, even if the user types digits. int() converts it into a whole number so that arithmetic works. Passing several values to print(), separated by commas, prints them with spaces between them.

Program 4: Pass or fail (decisions with if)

score = int(input("Enter your test score (0-100): "))

if score >= 50:
    print("Pass")
else:
    print("Not yet a pass. Keep practising!")

if runs the indented lines only when its condition is true; else runs when it is false. The indentation (four spaces) is not decoration: in Python, it shows which lines belong to the if. Comparison operators include >=, <=, ==, !=, > and <.

Program 5: Times tables (loops and functions)

def times_table(number):
    for i in range(1, 11):
        print(number, "x", i, "=", number * i)

choice = int(input("Which times table? "))
times_table(choice)

def creates a function: a named, reusable block of code. number is a parameter, a value the function receives when it is called. The for loop repeats once for each value of i; range(1, 11) produces the numbers 1 to 10, because the end value is not included.

What to learn next

  • Lists, which store several values in one variable.
  • while loops, which repeat until a condition changes.
  • Handling errors with try and except, so bad input doesn't crash your program.
  • Small projects, such as a quiz, a number-guessing game or a simple calculator.

Short, regular practice works better than occasional long sessions. Change each example, predict what will happen before you run it, and then check whether you were right.

References and further reading