Stop Using Semicolons in Python (2024)

Stop Using Semicolons in Python (3)

Coming from a C/C++ background, I am used to seeing a lot of semi-colons ; in code. They are used to represent statement termination.

But, Python does not mandate the use of semi-colons for delimiting statements. Yet, I often come across Python code littered with semi-colons.

Most recently, I was going through a Data Science with Python Course. The instructor was introducing conditional statements and wrote the following piece of code:

temp = 10;if temp <12: print('It is cold'); cold = True;

My reaction: Pure Horror!

Semi-colons are used only in atypical situations in Python. So, I prepared a small guide explaining why you shouldn’t use semi-colons in Python and point out the handful exceptions.

In many popular programming languages, you need to add a semi-colon at the end of every statement. For example, in C++:

int c = 10;
int a = 5;
printf('In C++, semicolon at the end is must');

But this is not true for Python.

Python is a no-bullsh*t programming language. It stays away from unnecessary characters and syntax.

In Python, a statement ends at the end of a line (exception for open brackets, quotes or parentheses). For example:

c = 10
a = 5
print('No semicolons in Python')

A semi-colon in Python denotes separation, rather than termination. It allows you to write multiple statements on the same line.

print('Statement 1'); print('Statement 2'); print('Statement 3')

This syntax also makes it legal to put a semicolon at the end of a single statement:

Stop Using Semicolons in Python (2024)
Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 5525

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.