1 Python Syntax and Program Structure

A progressive guide to Python’s core syntax, program structure, values, operators, input and output, and common beginner errors.

Program building blocks

Python programs are sequences of instructions that Python evaluates from top to bottom. The two central building blocks are and : a tells Python to perform an action, while an produces a value.

A small program can calculate a rectangle’s area by assigning values to width and height, evaluating the width * height\texttt{width * height}, assigning the result to area, and passing that value to print(). Names must be assigned before they are used; otherwise, Python raises NameError.

A source file contains physical lines of text, but Python can treat several physical lines as one logical line when an is enclosed in parentheses, brackets, or braces. Parentheses are generally clearer than a backslash used for explicit line continuation.

Takeaway: Read a Python program as an ordered sequence of statements, and identify the expressions that provide the values those statements use.

Lines, indentation, and comments

Python uses indentation to show which statements belong together. A colon after a compound- header introduces its body, and the following statements are indented to form a .

For example, after if temperature > 80:, each indented print() belongs to the if . A later aligned with if is outside the . Every at the same level should use the same indentation. Mixing tabs and spaces can raise TabError, so consistent spaces are recommended.

An indented cannot be empty. If a is required syntactically but no action is needed yet, use pass as a placeholder.

A comment begins with # and continues to the end of the physical line. Python ignores comments while interpreting the program. However, a # inside a string is ordinary string content. Good comments explain intent, assumptions, or non-obvious decisions rather than repeating exactly what simple code already says.

Takeaway: Colons begin compound- bodies, indentation defines their extent, and comments document code without changing its execution.

Names, literals, and strings

Names identify variables, functions, modules, and other objects. A name may contain letters, underscores, digits, and many non-ASCII characters, but it cannot begin with a digit. Python is case-sensitive, so total, Total, and TOTAL are different names. Keywords such as if, for, and def have special meaning and cannot be used as ordinary variable names.

A directly represents a value. Python supports integer and floating-point numeric literals, string and bytes literals, the Boolean literals True and False, the special value None, and collection literals such as lists.

Strings may use single or double quotes. Triple-quoted strings can span multiple lines, and escape sequences such as \\n represent characters that are difficult to type directly. An can insert the value of an into a string by placing the inside braces.

Takeaway: Choose descriptive names, remember that capitalization matters, and distinguish names from literals that directly provide values.

Assignment and operators

An binds the value produced by the right-hand side to the name on the left. For example, area = 3.14159 * radius ** 2 evaluates the before storing its result in area. Assignment with == is not mathematical equality; comparison with ==== asks whether two values are equal.

Python supports multiple assignment, such as width, height = 8, 5, and simultaneous unpacking of corresponding values.

Arithmetic operators include addition ++, subtraction −-, multiplication ×\times, division //, floor division ////, remainder %, and exponentiation **\texttt{**}. Regular division produces a floating-point result; for example, 72=3.5\frac{7}{2}=3.5. Floor division discards the fractional part toward negative infinity, while the remainder operator produces the remainder.

Comparison operators produce Boolean values: <<, >>, ≤\leq, ≥\geq, ====, and !=!=. Boolean operators and, or, and not combine or negate conditions. The membership operators in and not in test whether a value occurs in a collection. The identity operators is and is not test whether two references identify the same object and are commonly used with None; use ====, not is, for ordinary value comparison.

determines evaluation order. For example, 2+3×4=142+3\times4=14, whereas parentheses make (2+3)×4=20(2+3)\times4=20. Use parentheses when they clarify intent or require a different order.

Takeaway: Understand the distinction between binding and comparison, and use parentheses when precedence could make an difficult to read.

Input, output, and reliable style

The built-in print() function writes text to standard output. It accepts multiple objects, separates them with a space by default, and ends with a newline. The sep argument changes the separator, while end changes what follows the final object. F-strings are often a clear way to combine labels with calculated values.

The built-in function displays an optional prompt and returns the user’s response as a string. Convert numeric input explicitly before arithmetic: use int() for whole-number input and float() for decimal input. If the entered text cannot be converted, Python raises an exception.

A typical input-and-output flow is:

  1. Read a length and a width with .

  2. Convert both responses with float().

  3. Evaluate length * width\texttt{length * width}.

  4. Display the result with print() and an .

A prevents Python from parsing the program. Common causes include a missing colon after an if header, unmatched parentheses, misspelled keywords, and incorrect indentation. Clear lowercase names with underscores, consistent spaces, spaces around most binary operators, short statements, and logically grouped operations make programs easier to inspect and maintain.

Takeaway: Treat input as text until it is explicitly converted, use output formatting to communicate results, and check syntax before diagnosing program behavior.