Open In App

Provide Multiple Statements on a Single Line in Python

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python is known for its readability and simplicity, allowing developers to express concepts concisely. While it generally encourages clear and straightforward code, there are scenarios where you might want to execute multiple statements on a single line. In this article, we’ll explore the logic, and syntax, and provide different examples of how to achieve this in Python.

What are Multiple Statements on a Single Line?

The key to placing multiple statements on a single line in Python is to use a semicolon (;) to separate each statement. This allows you to execute multiple commands within the same line, enhancing code compactness. However, it’s important to use this feature judiciously, as overly complex code can lead to reduced readability.

Syntax: The basic syntax for placing multiple statements on a single line is as follows:

statement1 ; statement2 ; statement3

How To Provide Multiple Statements On A Single Line In Python?

Below, are the methods of How To Provide Multiple Statements On A Single Line In Python.

  • Variable Assignment & Print Statement
  • Conditional Statements
  • Loop with Break Statement

Variable Assignment and Print Statement

In this example, three statements are executed on a single line. First, we assign the value 5 to the variable x, then we assign 10 to the variable y, and finally, we print the sum of x and y.

Python3




x = 5 ; y = 10 ; print(x + y)


Output

15


Multiple Statements On A Single Line Using Conditional Statements

Here, a conditional statement is used to determine eligibility based on the value of the variable age. The result is assigned to the variable message, and it is printed in a single line.

Python3




age = 25 ; message = "You are eligible" if age >= 18 else "You are not eligible" ;
print(message)


Output

You are eligible


Multiple Statements On A Single Line Using Loop with Break Statement

In this example, a loop iterates through the numbers list, prints each number, and checks if it equals the target. If the target is found, the found variable is set to True, and the loop is terminated with the break statement.

Python3




numbers = [1, 2, 3, 4, 5] ; target = 3 ; found = False ; for num in numbers: print(num) ; found = True if num == target else found ; break if found else None


Multiple Statements On A Single Line Using List Comprehension

In this example, a list comprehension is used to generate a list of squares for even numbers in the range from 1 to 5. The result is assigned to the squares variable, and the list is printed on a single line.

Python3




squares = [x**2 for x in range(1, 6) if x % 2 == 0] ;
print(squares)


Output

[4, 16]


Conclusion

While Python emphasizes readability, there are situations where placing multiple statements on a single line can be useful. The semicolon (;) is the key syntax element for achieving this. However, it’s crucial to strike a balance between conciseness and readability to ensure maintainability and understanding of the code



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads