Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Multiple Statements in Python
Python allows you to write multiple statements on a single line or group them into code blocks called suites. This flexibility helps organize your code efficiently.
Multiple Statements on a Single Line
The semicolon (;) allows multiple statements on the same line, provided that neither statement starts a new code block ?
import sys; x = 'foo'; sys.stdout.write(x + '\n')
foo
Example with Variables
You can assign multiple variables and perform operations on a single line ?
a = 5; b = 10; result = a + b; print(result)
15
Multiple Statement Groups as Suites
A group of individual statements that make a single code block are called suites in Python. Compound statements like if, while, def, and class require a header line and a suite.
Header lines begin with a keyword and terminate with a colon (:), followed by one or more indented lines that make up the suite ?
if expression:
suite
elif expression:
suite
else:
suite
Example with if Statement
age = 18
if age >= 18:
print("You are an adult")
print("You can vote")
else:
print("You are a minor")
print("You cannot vote yet")
You are an adult You can vote
Example with Function Definition
def calculate_area(length, width):
area = length * width
return area
result = calculate_area(5, 3)
print(f"Area: {result}")
Area: 15
Key Points
- Use semicolons to separate multiple statements on one line
- Suites are indented code blocks following a colon
- Python uses indentation to define code blocks, not braces
- Compound statements require proper indentation for their suites
Conclusion
Python's semicolon syntax allows compact single-line statements, while suites provide structured code blocks. Proper indentation is essential for defining suites in compound statements.
