CS Engineering Gyan

Python Syntax

In the previous chapter, we explored the different data types Python provides for storing values like numbers, text, and collections of items. Before writing any real programs that actually use those data types, it helps enormously to understand the basic rules Python expects every program to follow, since these rules determine exactly how the interpreter reads and understands the code you write.

Syntax refers to the specific set of rules that define how statements in a programming language must be structured in order to be considered valid and understood correctly by the language's interpreter or compiler. Python is well known throughout the programming world for having unusually clean, readable syntax, and one of its most distinctive features is that it uses indentation itself as part of its syntax, rather than relying on symbols like curly braces the way many other languages do.

In this tutorial, you will learn how to write and run your first Python program, understand exactly how indentation works and why it matters so much in Python, explore how comments are written, and cover several smaller but important syntax rules, including case sensitivity, statement termination, and line continuation.


Writing Your First Python Program

A Python program is simply a plain text file containing Python statements, typically saved with a .py file extension. Unlike some other languages, Python does not require any special starting structure, such as a main function, in order to run a simple program, which makes it especially approachable for beginners.

Example

print("Welcome to CS Engineering Gyan")

Output

Welcome to CS Engineering Gyan

This single line is a complete, valid Python program. The print() function displays whatever text is placed inside its parentheses onto the screen, and Python executes this statement immediately, from top to bottom, without needing any additional setup code around it.


Indentation in Python

Indentation refers to the whitespace, usually spaces, placed at the beginning of a line of code. In most programming languages, indentation is purely a matter of style, used to make code easier to read, but has no actual effect on how the program runs. In Python, indentation is different: it is part of the language's actual syntax, and it is what Python uses to determine which statements belong together as a group, called a block.

Example

subscribers = 60000

if subscribers > 50000:

    print("CS Engineering Gyan crossed 50K subscribers")

    print("Thank you to every viewer!")

print("This line always runs")

Output

CS Engineering Gyan crossed 50K subscribers

Thank you to every viewer!

This line always runs

Notice that the two print statements immediately following the if statement are indented with four spaces, showing Python that they belong to the if block and should only run when the condition is true. The final print statement is not indented, so it belongs outside the if block and runs regardless of whether the condition was true or false.

Python does not strictly require exactly four spaces, but it does require that every line within the same block be indented by the exact same consistent amount. Mixing different indentation levels within the same block, or mixing tabs and spaces inconsistently, will cause Python to raise an error rather than guessing what was intended.


Comments in Python

A comment is a piece of text within a program that Python's interpreter completely ignores when running the code. Comments exist purely to help human readers, including the original programmer returning to the code later, understand what a particular piece of code is doing or why a particular decision was made.

Single-Line Comments

A single-line comment in Python begins with a hash symbol, and everything from that hash symbol to the end of the line is treated as a comment.

Example

# This program displays the channel name

channel = "CS Engineering Gyan"

print(channel)  # prints the channel name to the screen

Output

CS Engineering Gyan

Multi-Line Comments

Python does not have a dedicated symbol for multi-line comments the way some other languages do, but triple-quoted strings are commonly used to achieve the same effect, since a string that is not assigned to a variable or used in any way is simply ignored by the interpreter during execution.

Example

"""

This program is part of the CS Engineering Gyan

Python tutorial series, covering basic syntax rules.

"""

print("Learning Python syntax")

Output

Learning Python syntax

Case Sensitivity in Python

Python is a case-sensitive language, which means it treats uppercase and lowercase letters as completely different characters. A variable named subscribers is treated as an entirely separate variable from one named Subscribers or SUBSCRIBERS, even though they might look similar to a human reader at a glance.

Example

channel = "CS Engineering Gyan"

Channel = "Different Variable"

print(channel)

print(Channel)

Output

CS Engineering Gyan

Different Variable

This example shows two separate variables, channel and Channel, existing independently at the same time, purely because of the difference in capitalization. This case sensitivity also applies to Python's keywords and built-in function names, so writing Print() instead of print() would result in an error, since Python would not recognize it as the built-in function it actually is.


Statement Termination in Python

Many programming languages require a semicolon at the end of every statement to mark where it ends. Python does not require this, since it uses the newline character, meaning simply pressing enter to start a new line, as the natural way of separating one statement from the next.

Example

title = "Python Syntax"

views = 5200

print(title)

print(views)

Output

Python Syntax

5200

While semicolons are not required, Python does allow them to be used optionally when a programmer wants to place multiple statements on a single line, which is covered in the next section.


Multiple Statements on a Single Line

Although each Python statement is normally written on its own line, it is possible to place more than one statement on a single line by separating them with a semicolon. This is generally discouraged for everyday code, since it tends to reduce readability, but it remains valid, supported syntax.

Example

channel = "CS Engineering Gyan"; views = 4800; print(channel, views)

Output

CS Engineering Gyan 4800

Line Continuation in Python

Since Python treats a newline as the end of a statement, a single statement that is too long to comfortably fit on one line needs a way to be split across multiple lines without Python interpreting each line as a separate, incomplete statement. This can be done explicitly using a backslash at the end of a line, or implicitly by wrapping the statement inside parentheses, square brackets, or curly braces.

Example Using a Backslash

total_views = 1500 + 1800 + 2100 + \

              1950 + 2200

print(total_views)

Output

9550

Example Using Parentheses

total_views = (1500 + 1800 + 2100 +

               1950 + 2200)

print(total_views)

Output

9550

The parentheses-based approach is generally preferred by Python programmers over the backslash, since it is slightly less error-prone, given that a backslash must be the very last character on its line, with no trailing spaces afterward, or Python will fail to recognize it as a continuation.


Summary Table of Key Python Syntax Rules

Rule Description
Indentation Defines code blocks; must be consistent within the same block
Single-Line Comments Begin with a hash symbol and run to the end of the line
Multi-Line Comments Commonly written using triple-quoted strings
Case Sensitivity Uppercase and lowercase letters are treated as different characters
Statement Termination A newline ends a statement; semicolons are optional
Line Continuation Achieved using a backslash or by wrapping code in brackets

Advantages and Limitations of Python's Syntax

Advantages Limitations
Indentation-based blocks naturally encourage clean, readable code formatting. Inconsistent indentation causes errors rather than being silently ignored.
Minimal punctuation, such as no required semicolons, keeps code visually simple. Programmers coming from brace-based languages often need time to adjust.
Comment support makes it easy to document code directly alongside the logic it explains. Python lacks a single dedicated symbol for true multi-line comments.

Best Practices While Learning Python Syntax


Common Mistakes Beginners Make

Mistake Correct Practice
Mixing different indentation levels within the same block. Keep every line within a block indented by the exact same consistent amount.
Assuming Python ignores capitalization differences in variable names. Remember that Python is case-sensitive, so channel and Channel are different variables.
Leaving trailing spaces after a backslash used for line continuation. Make sure the backslash is the very last character on the line, with nothing after it.
Forgetting the colon at the end of a line that introduces a new indented block. Always end statements like if, for, and while with a colon before the indented block begins.

Frequently Asked Interview Questions

  1. What is syntax in a programming language?
    Syntax refers to the specific set of rules that define how statements must be structured to be considered valid by the language's interpreter or compiler.
  2. Why is indentation important in Python?
    Indentation in Python is part of the language's actual syntax, used to define which statements belong together as a block, rather than being purely stylistic.
  3. How do you write a single-line comment in Python?
    A single-line comment begins with a hash symbol, and everything after it on that line is ignored by the interpreter.
  4. How are multi-line comments typically written in Python?
    Multi-line comments are commonly written using triple-quoted strings that are not assigned to any variable.
  5. Is Python case-sensitive?
    Yes, Python treats uppercase and lowercase letters as different characters, so variable names differing only in case are treated as separate variables.
  6. Does Python require semicolons at the end of statements?
    No, Python uses a newline to mark the end of a statement, and semicolons are only needed when placing multiple statements on a single line.
  7. How can a long statement be continued across multiple lines in Python?
    A long statement can be continued using a backslash at the end of a line, or by wrapping the statement inside parentheses, brackets, or braces.
  8. What happens if indentation is inconsistent within the same block in Python?
    Python raises an error, since consistent indentation is required to correctly identify which statements belong to the same block.

Summary

Python's syntax is built around a small set of clear, consistent rules that make code easier to read and write compared to many other programming languages. Indentation defines code blocks directly, comments allow programmers to document their code using either single-line or triple-quoted multi-line style, and Python's case sensitivity means capitalization always matters when naming variables or calling functions.

We also covered how Python handles statement termination using newlines rather than semicolons, how multiple statements can optionally share a single line, and how long statements can be split across several lines using either a backslash or surrounding brackets. Together, these rules form the foundation every Python program is built on, regardless of how large or complex that program eventually becomes.

With Python syntax covered, you are now ready to explore variables and data types in more depth, learning how Python stores and handles different kinds of data and how values can be converted between types as a program runs.


← Previous: Python Data Types Next: Variables & Data Types →

Home Visit Our YouTube Channel