Your First Program
Explore the essentials of writing your first Python program by using the print() function to display text. Understand how Python reads and executes code line by line, the importance of correct indentation, and how to identify and fix syntax errors. This lesson helps you start programming confidently by engaging with Python's fundamental execution and error feedback.
Programming is essentially a conversation in which we give instructions, and the computer carries them out. In this lesson, we will write our first lines of code, see how Python translates our English-like commands into action, and learn what happens when we make a mistake.
The first statement
A Python program is composed of statement(s). A statement is a single instruction that Python executes. The most fundamental action we can perform is displaying information to the console using the print() function.
In Python, functions are like a named set of steps that you can run whenever you need. It's like a recipe. You give it ingredients, it follows the steps, and then gives you a result. The print() function is a commonly used built-in Python function to display text. To display text, we enclose the text in quotation marks (either single ' or double ").
Line 1: Calls the
print()function with the text"Hello, World!". Python executes this by displaying the text as is on the console.
The interpreter: Reading line by line
Python is an interpreted language. This means it does not translate an entire program into machine code all at once before running it (a process known as compiling). Instead, the Python interpreter reads the file from top to bottom, executing each line in order. This behavior allows for rapid feedback. If Python encounters an error, it stops execution at that line and reports the issue, which makes it easier to identify and fix problems early.
Indentation and structure
In many programming languages, we use semicolons ; to end lines or curly braces {} to group code. Python is different: it uses whitespace to define structure. At this stage, the most important rule is simple: "start at the beginning of the line."
If we add random spaces (indentation) at the start of a line without a specific reason, Python gets confused. It assumes indentation implies a sub-block of code (like inside a specific rule or loop), which we haven't defined yet. For standard, top-level commands, we must keep our code aligned to the left margin.
Line 1: This is correctly aligned to the left. It runs successfully.
Line 2: This has leading spaces (indentation) but is not part of a larger structure. Python raises an
IndentationErrorand stops.
When things break: Syntax errors
During execution, Python also catches grammar mistakes, called syntax errors. If we misspell a keyword or forget a piece of punctuation, the interpreter stops and complains. This error message is not a failure; it is a helpful report telling us exactly where Python got lost.
Line 1: We open the text with a double quote
"but forgot to close it before the parentheses.
Python scans this line, realises the text never ends, and crashes with a SyntaxError: EOL while scanning string literal. This tells us the "End Of Line" (EOL) was reached before the string (text) was finished. Try fixing this by simply adding the missing quote before ).
This cycle, "write, run, see error, and fix," is the core loop of programming.
We have successfully initiated a conversation with the computer. We learned that Python follows our instructions sequentially, requires consistent indentation, and provides immediate feedback when we break the syntax rules. These errors are simply guideposts helping us write valid code.