Programming tutorial #Python #programming #coding for beginners

Your First Python Program: A Gentle Start

Install nothing: run Python in your browser, then learn variables, input, if statements, loops and lists by building a grade calculator.

1

Reads

0

Saves

3

Min read

Python is one of the easiest programming languages to start with, and it is widely used in data analysis, automation, web development and artificial intelligence. This tutorial takes you from zero to a small working program.

Where to run Python

You can start without installing anything by using a free online Python editor in your browser, or a free mobile app that runs Python code. When you are ready, you can install Python from python.org on a laptop.

1. Printing

print("Hello, AcadSyntra!")

print() displays whatever is inside the brackets. Text must be inside quotation marks.

2. Variables

A variable stores a value so you can use it later.

name = "Tunde"
score = 67
print(name, "scored", score)

Text values are called strings; whole numbers are integers; decimals are floats.

3. Getting input

name = input("What is your name? ")
print("Welcome,", name)

input() always returns text. To do maths with it, convert it to a number:

score = int(input("Enter your score: "))

4. Making decisions with if

score = int(input("Enter your score: "))

if score >= 70:
    print("Grade A")
elif score >= 60:
    print("Grade B")
elif score >= 50:
    print("Grade C")
elif score >= 45:
    print("Grade D")
else:
    print("Below D")

Notice the colon at the end of each condition and the indentation (four spaces) before each print. In Python, indentation shows which lines belong to which block. Getting it wrong is the most common beginner error.

5. Lists and loops

A list stores many values. A for loop repeats code for each item.

scores = [72, 58, 64, 45, 81]

total = 0
for s in scores:
    total = total + s

average = total / len(scores)
print("Average:", average)

len(scores) gives the number of items in the list. Python also has a shortcut: sum(scores).

6. Putting it together: a grade calculator

def grade(score):
    if score >= 70:
        return "A"
    elif score >= 60:
        return "B"
    elif score >= 50:
        return "C"
    elif score >= 45:
        return "D"
    else:
        return "F"

students = {"Ada": 72, "Bayo": 58, "Chioma": 64}

for name, score in students.items():
    print(name, score, grade(score))

Here, def creates a function, a reusable block of code. The curly brackets create a dictionary, which stores pairs of keys and values (name and score). The loop prints each student's grade.

Common errors and what they mean

  • IndentationError: your spaces are inconsistent. Use four spaces for each level.
  • SyntaxError: often a missing colon, bracket or quotation mark.
  • NameError: you used a variable before creating it, or misspelled it.
  • TypeError: often mixing text and numbers, for example forgetting int() on input.

What next?

Practise a little every day. Try changing the program to read scores from the user, count how many students passed, or find the highest score. Small daily practice beats long, rare sessions.

Discussion

Academic conversation

Evidence-first
No comments yet. Start the academic conversation.