Your first program

Part of the free LogicWiz Python course, Module 1: Start coding. The examples and practice run in the app.

Shankar bhaiya wants the canteen screen to say Welcome to Canteen Counter. That takes one line of Python. Press Run on the first card and you have written your first program.

You'll learn to

  • Run a program and read its output
  • Show text and numbers with print()
  • Write comments that Python ignores
  • Read an error message and fix the line it points to

Learn

Press Run

print("Welcome to Canteen Counter!")
print("Samosa: Rs 15")
print("Chai: Rs 10")

That is a real program, and you just ran it. Now change 15 to 20 and press Run again. You can edit every code box in this course.

What just happened

print() shows whatever you put inside its brackets.

  • Text goes inside quotes: "Samosa". The quotes are not printed.
  • Python runs your lines top to bottom, one at a time. Line 1 prints first, then line 2, then line 3.
  • Each print() starts a new line.

Python is an interpreted language: it reads one line, runs it, then moves to the next. That is why the order of lines is the order of output.

Numbers, many things, and comments

# A comment starts with #. Python skips it.
print(15 + 10)          # maths: prints 25
print("15 + 10")        # text: printed exactly as written
print("Total:", 25)     # a comma puts a space between items
print("Chai", "Samosa", "Vada", sep=" | ")
print("Loading", end="...")
print("done")

sep changes what goes between items (a space by default). end changes what goes after the line (a new line by default).

Errors are messages, not punishments

Every programmer sees errors all day. Read them from the bottom up:

  1. The last line names the error and says what is wrong: NameError: name 'prnt' is not defined.
  2. Above it, line 2 tells you where.
  3. A caret ^ often points at the exact spot.

Fix that one line and run again. That is the whole skill.

A typo: earlier lines still run

print("Line 1 runs")
prnt("Line 2 has a typo")
print("Line 3 never runs")

Line 1 printed, then Python hit the unknown name prnt and stopped. Python even suggests print. Fix the typo and run it.

A broken line: nothing runs at all

print("Line 1")
print("Samosa: Rs 15)

This time even line 1 did not print. Python checks the whole file for grammar first. A SyntaxError (an unclosed quote, a missing bracket) stops it before any line runs. Add the closing quote and run again.

Python is case-sensitive: Print is not print. Quotes must come in pairs ("…" or '…'), and every ( needs a ). These three cause most first-day errors.

CBSE Class 11 asks: Python is an interpreted language. What does that mean? (It runs line by line.) Also interactive mode vs script mode: interactive mode (the >>> prompt) runs one statement at a time; script mode runs a saved .py file, like the boxes here.

In one line

print() shows output and Python runs top to bottom; read an error from its last line: the type, the message, then the line number.