{"id":7800,"date":"2021-08-26T12:00:25","date_gmt":"2021-08-26T06:30:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=7800"},"modified":"2021-11-22T18:39:31","modified_gmt":"2021-11-22T13:09:31","slug":"semicolon-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/semicolon-in-python\/","title":{"rendered":"Semicolon in Python | How & Why Python Semicolon is Used?"},"content":{"rendered":"
Let\u2019s 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.<\/p>\n
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\u2019s have a look.<\/p>\n
Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0<\/strong>Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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.<\/p>\n 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\u2019s two statements that empty the second.<\/p>\n The most commonly asked logical question here would be:\u00a0Why are Semi-colons allowed in Python?<\/strong><\/p>\n 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\u00a0a (useless) terminator\u00a0at the end of every line.<\/p>\n But, there are certain conditions where semicolons are helpful.<\/p>\n A semi-colon in Python means separation<\/strong>, rather than termination. It lets you compose various statements on the same line. Let\u2019s examine what happens to us when we attempt to print semi-colons.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n It just treats the semicolon as a string (having one character) in python.<\/p>\n 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.<\/p>\n Syntax:<\/strong><\/p>\n <\/p>\n Example:<\/strong><\/p>\n Let us take four statements without semicolons<\/p>\n Output:<\/strong><\/p>\n Now, let us take the same four statements using semicolons:<\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you use a semicolon to separate a normal expression from a block statement, such as a loop, Python will throw an error.<\/p>\n Output:<\/strong><\/p>\n 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\u2019s sum up the tutorial with two reminders:<\/p>\n The usage of semicolons is quite \u201cnon-pythonic\u201d and should be avoided until absolutely necessary. But, Multiple statements on a single line should be avoided.<\/p>\n Though the language specification permits for the use of a semicolon to separate statements, doing so without cause makes one\u2019s code more difficult to comprehend.<\/p>\n Related Programs<\/strong>:<\/p>\n Let\u2019s 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. …<\/p>\n<\/a>Why Python semicolons are allowed?<\/h3>\n
<\/a>When to Use a Semi-colon?<\/h3>\n
\n
<\/a>Role of Semicolons in Python<\/h3>\n
\n
<\/a>How to Print a Semi-Colon in Python?<\/h3>\n
#printing the semi colon\r\nprint(\";\")<\/pre>\n
;<\/pre>\n
<\/a>Split Statements Using Semicolons<\/h3>\n
statement1; statement2 ; statement3<\/pre>\n
# printing four statements without using the semicolon\r\nprint(\"Hello\")\r\nprint(\"This\")\r\nprint(\"is\")\r\nprint(\"BTechGeeks\")\r\n<\/pre>\n
Hello\r\nThis\r\nis\r\nBTechGeeks<\/pre>\n
# printing four statements with using the semicolon\r\nprint(\"Hello\");print(\"This\");print(\"is\");print(\"BTechGeeks\")\r\n<\/pre>\n
Hello\r\nThis\r\nis\r\nBTechGeeks<\/pre>\n
<\/a>Using semicolons with loops in Python<\/h3>\n
for i in range(10):print(\"Hello\");print(\"this\");print(\"is BTechGeeks python\");print(\"Online Platform\")\r\n<\/pre>\n
Hello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform\r\nHello\r\nthis\r\nis BTechGeeks python\r\nOnline Platform<\/pre>\n
print('hello') ; for i in range (10): print ('BTechGeeks')<\/pre>\n
File \"\/home\/36390fc9b8b053533f2165af206c5441.py\", line 1\r\n print('hello') ; for i in range (10): print ('BTechGeeks')\r\n ^\r\nSyntaxError: invalid syntax<\/pre>\n
<\/a>Conclusion on Python Program to Use Semicolon at the end of the Sentence<\/h3>\n
\n
\n