Semicolon in Python

Semicolon in Python | How & Why Python Semicolon is Used?

Let’s have a look at how the semicolon is used in Python. In different programming languages, the semicolon(;) signifies the end or termination of the current statement.

A semicolon is required to end a line of code in programming languages such as C, C++, and Java. Python, on the other hand, is not like that. So, does using semicolons in Python programming make a difference? Let’s have a look.

All About Python Semicolon

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Why Python semicolons are allowed?

To terminate the statements, Python does not require semicolons. If you want to write a number of statements on the same line, Semicolons may be used to delimit statements.

In Python, a semicolon denotes separation instead of completion. It enables many statements to be written on the same line. It also makes it legal at the end of a single sentence to put a semicolon. In fact, it’s two statements that empty the second.

When to Use a Semi-colon?

The most commonly asked logical question here would be: Why are Semi-colons allowed in Python?

I believe that it was done to make a transition from other programming languages slightly easier. Programmers with a background in Java, C++, and PHP habitually put a (useless) terminator at the end of every line.

But, there are certain conditions where semicolons are helpful.

  • Running Scripts from Shell
  • Evaluating Expressions for Side Effects

Role of Semicolons in Python

  1. Python is known as a simple coding language as there is no need of using Semicolon and if we even forget to place, it doesn’t throw an error.
  2. In Python, a Semicolon is not used to denote the end of the line.
  3. Python doesn’t use Semicolons but it is not restricted.
  4. Sometimes Python makes use of Semicolon as a line terminator where it is used as a divider to separate multiple lines.

How to Print a Semi-Colon in Python?

A semi-colon in Python means separation, rather than termination. It lets you compose various statements on the same line. Let’s examine what happens to us when we attempt to print semi-colons.

Below is the implementation:

#printing the semi colon
print(";")

Output:

;

It just treats the semicolon as a string (having one character) in python.

Split Statements Using Semicolons

Now, let’s look at how we can separate declarations using semicolons into Python. In this situation, we are going to try to employ a semicolon with more than two statements on the same course.

Syntax:

statement1; statement2 ; statement3

Semicolon in Python

Example:

Let us take four statements without semicolons

# printing four statements without using the semicolon
print("Hello")
print("This")
print("is")
print("BTechGeeks")

Output:

Hello
This
is
BTechGeeks

Now, let us take the same four statements using semicolons:

# printing four statements with using the semicolon
print("Hello");print("This");print("is");print("BTechGeeks")

Output:

Hello
This
is
BTechGeeks

As you can see, after splitting them with semicolons, Python performs the four commands individually. The interpreter would give us an error without this use.

Using semicolons with loops in Python

A semicolon may be used in loops like ‘For loop,’ if the entire statement begins with a loop and a semi-colon is used for a cohesive statement like a loop body.

Below is the implementation:

for i in range(10):print("Hello");print("this");print("is BTechGeeks python");print("Online Platform")

Output:

Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform
Hello
this
is BTechGeeks python
Online Platform

If you use a semicolon to separate a normal expression from a block statement, such as a loop, Python will throw an error.

print('hello') ; for i in range (10): print ('BTechGeeks')

Output:

  File "/home/36390fc9b8b053533f2165af206c5441.py", line 1
    print('hello') ; for i in range (10): print ('BTechGeeks')
                       ^
SyntaxError: invalid syntax

Conclusion on Python Program to Use Semicolon at the end of the Sentence

This takes us to the end of this little module where we learn how to use a semicolon at the end of multiple statements on the Python program. Let’s sum up the tutorial with two reminders:

  • In Python, a semicolon is commonly used to separate numerous statements written on a single line. So, you can use the python semicolon in a statement on the same line and attain the output that you wish.
  • Semicolons are used to write tiny statements and conserve some space, such as name = Vikram; age = 20; print (name, age)

The usage of semicolons is quite “non-pythonic” and should be avoided until absolutely necessary. But, Multiple statements on a single line should be avoided.

Though the language specification permits for the use of a semicolon to separate statements, doing so without cause makes one’s code more difficult to comprehend.

Related Programs: