the full guide

Python from zero, part 1: your first program

Never coded before? Learn what a program is, print, variables, numbers, text and the errors everyone hits, with code you run right on the page.

20 min Absolute beginner

What you need: Nothing. No installing, no maths beyond adding and multiplying.

What is a program?

A program is a list of instructions for a computer, followed exactly, in order, from top to bottom. Python is a language for writing those instructions, and most AI work today is written in it because it reads almost like English.

print: make the computer speak

hello.py
print("Namaste!")
print("I am learning Python.")
print(2 + 3)

Output

Namaste!
I am learning Python.
5
  • print(...) shows whatever is inside the brackets.
  • Text goes inside quotes. Without quotes, Python thinks it's a name or a command.
  • Numbers need no quotes, and Python does the sum for you.

Comments: notes for humans

Anything after a # is ignored by Python. Use it to leave yourself a note.

comments.py
# This line does nothing. It's a note for you.
print("This line runs")  # A note can also go here

Output

This line runs

Variables: labelled boxes

A variable is a labelled box that holds a value. The = sign means “put this into the box”. It does not mean “equals” as in maths.

variables.py
name = "Riya"
age = 14

print(name)
print(age)

age = age + 1      # Take out 14, add 1, put 15 back
print(age)

Output

Riya
14
15

Rules for names

NameAllowed?Why
total_marksYesLetters and underscores: this is the usual style
marks2YesNumbers are fine after the first letter
2marksNoCan't start with a number
total marksNoNo spaces allowed
Age and ageDifferent boxesCapital letters matter

Numbers and text are different things

TypeName in PythonExample
Whole numberint42
Decimal numberfloat3.14
Textstr"hello"
True or falseboolTrue
types.py
print(type(42))
print(type(3.14))
print(type("hello"))

# The same symbol does different things
print(2 + 3)       # Numbers: add
print("2" + "3")   # Text: join together

Output

<class 'int'>
<class 'float'>
<class 'str'>
5
23

Python as a calculator

SymbolMeaningExampleAnswer
+Add7 + 29
-Subtract7 - 25
*Multiply7 * 214
/Divide7 / 23.5
//Divide, drop the decimal7 // 23
%Remainder7 % 21
**Power7 ** 249

Mixing text and values: f-strings

Put an f before the quotes, and anything in curly brackets is filled in.

fstrings.py
name = "Aman"
marks = 86

print(f"{name} scored {marks} marks.")
print(f"Next year {name} wants {marks + 10}.")

Output

Aman scored 86 marks.
Next year Aman wants 96.

Errors are normal: how to read them

Every programmer sees errors daily. An error isn't a judgement of you. It's Python telling you what confused it. Read the last line first.

You seeWhat it meansUsual fix
NameError: name 'nme' is not definedPython has no box with that nameCheck the spelling, and that you created it earlier
SyntaxErrorThe grammar is wrongLook for a missing quote or bracket
TypeError: can only concatenate strYou mixed text and a number with +Use an f-string
IndentationErrorThe spaces at the start of a line are wrongRemove the extra spaces

Here is a bug to fix. Run it, read the last line of the error, then correct it.

fix_me.py
name = "Riya"
print(nme)

Your first small project: a marks calculator

Everything so far, together. Change the marks to your own.

marks.py
# Marks out of 100 in each subject
maths = 78
science = 85
english = 90

total = maths + science + english
percentage = total / 300 * 100

print(f"Total: {total} out of 300")
print(f"Percentage: {percentage:.1f}%")

Output

Total: 253 out of 300
Percentage: 84.3%

Practice

  1. Print your name, and your age next year, using an f-string.
  2. A pen costs ₹12. Work out the price of 7 pens in a variable, then print it.
  3. Add a fourth subject to the marks calculator. What else must you change?
  4. What does 17 % 5 give? Guess first, then run it.

Finished reading?