Skip to the workspace
Pebellslearn by building
Fundamentals  ›  Lesson

Basic Syntax & Output

Basic Syntax 16 minSyntax is the set of rules that makes Python code a valid program. Output is information the program sends to the console.

Set up Python and run your first file

Install the Python interpreter, save a small program, and see the difference between a saved file and the interactive Python prompt. The browser exercises remain available while you set up your own computer.

Already set up? Continue to the lesson

New to coding? Meet the tools and find the terminal
Python interpreter
Reads and executes Python source files.
Text editor
Creates and saves .py source files.
Virtual environment
A project-specific Python environment for packages. You can create one after the first file runs.

A terminal is a window for commands. Its shell interprets those commands. PATH is the list of folders the shell searches for programs. An editor extension adds editing help; it usually does not install the language itself.

In VS Code, open your project folder, then choose View → Terminal. Type at the cursor in the Terminal panel, not in Output, the source file, or the browser address bar. Our command blocks omit prompt symbols such as $ and >; do not add them.

Official VS Code screenshot: the Terminal tab is selected below the editor, with the PowerShell shell name at the right and a command prompt at the left.
Find the Terminal tab and shell name. Open full-size screenshot (new tab).

The dropdown next to the plus button selects a shell. Choose the shell named in your operating-system instructions below. Available choices depend on what you installed.

Official VS Code screenshot: the arrow beside the terminal plus button opens a list containing PowerShell, Command Prompt, Git Bash and Ubuntu WSL.
Official documentation screenshots by Microsoft, VS Code terminal guide, CC BY 3.0 US. Unmodified; your version may look different.

Choose a system to see its installation steps. A computer is needed for these local setup instructions.

Official documentation and illustrated guides

Instructions reviewed 2026-10-27. Use the linked installer page for current versions and supported operating systems.

Continue to the lesson

Syntax is the set of rules that makes Python code a valid program. Output is information the program sends to the console.

Read simple statements, indentation and print calls, and distinguish a parsing failure from a failure during execution.

A receipt line is a useful first program: you can predict its text, change one piece, and compare the result. Start with quotes, expressions and print; then use indentation to group instructions into a function. No previous Python knowledge is needed. By the end, distinguish a program Python cannot parse from a valid program that fails while running. Python reading a short script from the top: the comment line is greyed out and skipped, the print line sends text to the screen, the indented lines are bracketed together as the body of def, and a note distinguishes invalid syntax from errors during execution

Read code in two stages

Python first checks the syntax of the source being run: whether the quotes, brackets and statement structure form valid Python. It then executes that source. For a script, a syntax error anywhere in that file prevents that file's first statement from running. A misspelled name is different: Print("hi") is valid syntax, but raises NameError when Python tries to look up Print.

An expression produces a value. 20 * 3 produces 60; "Token" produces text. A statement tells Python to do something, such as assign a value:

python

Read assignment from the right: find price, multiply it by 3, then bind the result to total. The first two lines produce no console output. Only the third displays anything. Seeing no output does not mean no work ran.

Read a failure's last line for its error type, then inspect the indicated source line. A syntax caret marks where parsing became impossible; a missing quote or bracket may be earlier. Change one suspected cause and run again so the next observation can test your diagnosis.

Preparing this topic’s interactive investigation…

Print a canteen token

The college canteen prints a token for every order. print() takes any number of values, separated by commas, and puts one space between them:

python

sep goes between the values, end goes after the last one (normally a new line). Now try building the same line with +:

broken — fix itTypeError

Run it, read the error, then make it print the token line.

Python refuses to guess whether "4" + 2 means "42" or 6, so it stops. The everyday fix is an f-string: put f before the quote and drop any value into {braces}. You can even do sums inside them:

python

Receipts also need layout. Inside a string, \n starts a new line and \t jumps to the next tab stop:

python

And if the text contains an apostrophe, wrap it in the other kind of quote:

broken — fix itSyntaxError

This will not even start. Read the error, then make it print the special.

That error occurs while parsing this snippet, before its print can run. Later lessons distinguish this from exceptions raised by already-running code.

Greet a passenger

A railway app greets whoever logs in. In a terminal script the name would come from input(), which pauses, waits for the user, and hands back what they typed, always as text:

name = input("Your name? ")   # the user types Asha
print("Namaste,", name)       # Namaste, Asha

(The browser boxes here can't pause for typing, so from now on we'll write the typed value straight into a variable.)

The app does not want the greeting printed on the console. It wants the greeting as a value, so it can put it on the screen. So you write a function that builds the line. First attempt:

What does this print?
def greet(name):
    line = f"Namaste, {name}!"

banner = greet("Asha")
print(banner)

That None is the most common bug of a beginner's first week. The function did the work and then threw the result away. Hand it back with return, and step through to watch banner get its value at the moment the call finishes:

step through it
1def greet(name):
2 line = f"Namaste, {name}!"
3 return line
4 
5banner = greet("Asha")
6print(banner.upper())

Rule of thumb: print is for people, return is for code. A function that builds something should return it and let the caller decide whether to show it.

Sketch a ticket app

You are planning a train-booking app and want to write the outline first, before filling in any logic. Every function needs a body, and an empty body is a SyntaxError, so Python gives you pass: a statement that does nothing.

python

The body is whatever is indented under the def line (four spaces by convention). Indentation is the structure, so getting it wrong is an error, not a style issue. The same goes for names: some words are keywords that Python has reserved for itself, and you cannot use them as variable names.

broken — fix itSyntaxError

Run it and read the error. Rename the variable so the line prints.

You can see the full list any time. There are about 35, and you'll meet most of them in this course: if, for, def, return, True, None …

python

Traps you'll hit this week

  • Case matters. seat, Seat and SEAT are three different names, and Print("hi") is a NameError.
  • Comments start with # outside a string. Use several # lines for a long note. Triple quotes create a string, not a comment; a string placed first in a function can be its documentation, called a docstring.
  • Long lines can continue inside brackets. Python keeps reading until the bracket closes, so you rarely need a \ at the end of a line:
python

Before the choices below, make a two-line label for a parcel using a name and a numeric count. Predict which lines will be printed. Then deliberately remove a quote, restore it, and misspell the variable name instead. Explain why those two failures happen at different stages.

Quick practice

What does this print?
print("Pune", "Mumbai", sep=" to ", end="!")
What does this print?
print("3" + "4", 3 + 4)
broken — fix itIndentationError

Run it and read the error, then fix the layout.

What does this print?
name = "Meera"
print(f"Hi {name}", "Hi {name}")

🎯 Your turn

Write greet_customer(name). It returns the welcome line the app shows each guest:

  • greet_customer("Ada") → "Welcome, Ada! Let's split the bill."
  • greet_customer("Sam") → "Welcome, Sam! Let's split the bill."

Hint: use an f-string, wrap it in double quotes so the apostrophe in Let's is safe, and return it. Don't print it.

Press ▶ Run, try the Live App chips, then ✓ Check.

Your task
Build greet_customer(name). It returns the welcome line the app shows each guest. Example: greet_customer("Ada") → "Welcome, Ada! Let's split the bill.".
Project context and further resources
You're building a piece ofBill Splitter & Tip Calculator
This piece — greet_customer(): Greets each guest at the very top of the app.
Scenario A party of 4 sits down at Table 12. The app greets them by name before they start ordering.
greet_customer.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
greet_customer(name) → strReturn a friendly welcome line for the guest.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.