Skip to content

Python Statements

In Python, a statement is a single line of code that performs an action. Python executes each statement sequentially as part of a program unless directed otherwise by control flow structures (e.g., loops, conditionals).


1. Types of Python Statements:

a. Simple Statements

  • A single-line instruction that performs an action.
  • Examples:

x = 5          # Assignment statement

print(x)       # Function call statement

b. Compound Statements

  • Consist of multiple clauses or blocks of code and span multiple lines.
  • Typically include control flow constructs such as:
    • Conditionals (if, elif, else)
    • Loops (for, while)
    • Exception Handling (try, except, finally)
    • Functions and Classes

if x > 0:

    print(“Positive”)

else:

    print(“Non-positive”)


2. Features of Python Statements:

a. Dynamic Typing and Assignment:

  • Variables do not need explicit type declaration.

a = 10          # Assigns an integer

b = “Hello”     # Assigns a string

b. Indentation-Based Syntax:

  • Python uses indentation to define code blocks instead of braces ({}) or begin/end.

for i in range(5):

    print(i)    # Indentation is required

c. Simplicity with Statement Keywords:

  • Python statements are concise and use keywords for specific purposes, like pass or break.

3. Types of Statements in Detail:

a. Expression Statements:

  • Evaluate and return a value.

y = 5 + 2   # Expression statement

print(y)    # Another example

b. Assignment Statements:

  • Assign values to variables.

name = “Alice”

x, y = 10, 20   # Multiple assignments

c. Control Flow Statements:

  • Alter the program’s execution flow.
    • Conditionals:

if x > 10:

    print(“Greater than 10”)

elif x == 10:

    print(“Equal to 10”)

else:

    print(“Less than 10”)

  • Loops:

for i in range(3):

    print(i)

d. Import Statements:

  • Bring in modules or specific elements from modules.

import math

from math import sqrt

e. Pass Statement:

  • A placeholder for future code, does nothing when executed.

def my_function():

    pass  # Placeholder for function logic

f. Return Statements:

  • Used in functions to return a value.

def add(a, b):

    return a + b

g. Break, Continue, and Loop Control:

  • Manage loop behavior.

for i in range(5):

    if i == 3:

        break   # Exit loop

    print(i)

h. Exception Handling Statements:

  • Handle runtime errors.

try:

    result = 10 / 0

except ZeroDivisionError:

    print(“Cannot divide by zero”)


4. Multi-Line Statements:

a. Implicit Line Continuation:

  • Parentheses, brackets, or braces allow statements to span multiple lines.

total = (1 + 2 +

         3 + 4)

b. Explicit Line Continuation:

  • Use a backslash (\) to extend a statement across lines.

total = 1 + 2 + \

        3 + 4


5. Best Practices for Writing Python Statements:

  1. Keep It Simple: Write clear and concise statements.

# Clear

x = y + z

# Avoid overly complex one-liners

  • Indent Consistently: Follow standard indentation rules (4 spaces per level).
  • Use Comments: Add comments for clarity, especially for complex statements.
  • Follow PEP 8: Adhere to Python’s style guide for better readability.

Conclusion: Python statements are the foundation of writing programs. They are versatile and range from simple assignments to complex constructs. With Python’s clear syntax, it becomes easier to manage and execute statements effectively, making the language both powerful and user-friendly.