Python basics: your first scripts
Write and run your first Python: print, variables, if, loops, lists and functions, how indentation works, how to read a traceback, and how Python compares with JavaScript.
Why Python, and where to run it
Python reads almost like English, which is why many schools teach it first. It's widely used for data, automation, AI and science, and the ideas you learn carry over to every other language.
You can run it without installing anything in Google Colab, a free notebook in your browser that needs a Google account (check your school's rules first). Or install Python from python.org and run a script in the terminal with python3 hello.py. A notebook mixes notes and code cells: run a cell with Shift+Enter and the output appears underneath.
The basics
- print("Hello") shows text. name = "Sam" stores a value in a variable; no let or const needed.
- Indentation is part of the language: the lines inside an if, a loop or a function are indented, usually by 4 spaces, and that's how Python knows where the block ends.
- Decisions: if score >= 80: then an indented line, with elif and else for other cases.
- Lists and loops: scores = [72, 95, 88], then for score in scores: and an indented print(score). range(5) counts 0 to 4.
- Functions: def total(numbers): then an indented return sum(numbers).
- f-strings put values into text: print(f"Hi {name}, you scored {score}").
- Comments start with #. True, False and None are capitalised.
Reading a traceback
When Python hits an error it prints a traceback. Read it from the bottom: the last line names the error and the message, and the lines above point to the file and line number.
- NameError: name 'scroe' is not defined usually means a typo, or a variable used before it was set.
- IndentationError means the spaces don't line up; make every line in a block start at the same place.
- TypeError: can only concatenate str (not "int") to str means you tried to join text and a number with +. Use an f-string, or str(score).
- Paste the whole traceback into your AI helper and ask for the cause before the fix.
Python next to JavaScript
If you've read our JavaScript guides, you already know the ideas; only the spelling changes. print() is console.log(). def is function. Indentation does the job of curly braces. elif is else if. True and None are true and null. # starts a comment instead of //.
Python shines for scripts that tidy files and spreadsheets, cleaning CSV data, exploring data in notebooks and calling AI APIs with official libraries. Keep API keys out of any notebook you share: use a secret or an environment variable, never a code cell.
Key terms
- Python
- A popular programming language that reads almost like English, used for data, automation, AI and teaching.
- Indentation
- Spaces at the start of a line. In Python they show which lines belong inside an if, a loop or a function.
- Traceback
- Python's error report. Read it from the bottom: the last line names the error, the lines above show where.
- Notebook (Jupyter, Colab)
- A document that mixes notes and runnable code cells, with each cell's output shown underneath.
- Variable
- A named value in code, like const price = 20.
- Loop
- Code that repeats for each item in a list or until something changes.
- Function
- A named, reusable recipe in code that takes inputs and can return an output.
- Conditional (if/else)
- Code that runs only when something is true, otherwise something else.
- API key
- A secret code that identifies your app to a service and is often tied to billing.
Try it
Your first Python notebook
- Open Google Colab (or Python on your computer) and run print("Hello, world").
- Make a list of five scores and use a for loop to print each one.
- Write a function that returns the average of a list, and print the average of your scores with an f-string.
- Break it on purpose: remove the indent inside the loop, run it, and read the traceback from the bottom up.
- Paste your code into an AI helper and ask it to explain each line, then ask it to quiz you on one.
Ask an AI tutor about this guide
Copy a prompt into ChatGPT, Claude or Gemini to go deeper. Check what it tells you against the guide.
- Explain it like I'm 12
- Quiz me (5 questions, one at a time)
- Show me a real example for my situation
- What do beginners get wrong?
Knowledge check
Five questions. Get 4 right to earn the Python Starter badge. You can retake it as often as you like.
Question 1 of 5
Finished the How software works guides? Take the How software works track test for ten mixed questions.
My notes
Write what clicked, what confused you, and what you want to try. Saved in this browser only.
Learn it live
Digital Builders University runs small live workshops where you build something real with an instructor beside you, starting with Vibe Coding 101.
See workshopsPractise it
- Cheat sheet: Python
- Game: Code Scramble (put the lines of a working program back in order)
Official sites
Last reviewed November 25, 2026. AI tools change fast; check each tool’s own site for current features, plans and prices.
