Python semicolon “denotes separation, rather than termination.” It allows you to separate multiple statements on a single line.
However, it’s important to note that this is not a common practice in Python and is generally discouraged in favor of writing each statement on its own line for better readability.
Visual Representation
print('Millie'); print('Bobby'); print('Brown')
Output
Millie
Bobby
Brown
Example 1: Print a semicolon
print(";")
Output
;
Example 2: Mixing Different Operations
x = 5; y = 10; z = x + y; print(z)
Output
15
Assigning a value, printing it, and then using it in another operation.
Example 3: Using with Loop
for i in range(3): print(i); print(i**2)
Output
0
0
1
1
2
4
A for loop where each iteration includes multiple operations.
Example 4: Using with List Comprehension
[print(x) for x in range(3)]; print("Loop finished")
Output
0
1
2
Loop finished
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.