Friday, December 9, 2022

[FIXED] Why is a semicolon working in Python and does not result in an error?

Issue

I am using Python 2.7.

I have been experimenting with the interpreter and found out the following unusual thing. I wrote the following code and it's working:

    def func():
        a = 5 ;
        print a

When I call this function, it gives the required output in the interpreter, not giving any syntax error, despite using the semicolon. Here is the screenshot:

Enter image description here

Why is it not giving a syntax error?


Solution

First of all, don’t use semicolons to end a statement in Python!

But look here to see why it’s allowed: Why is semicolon allowed in this Python snippet?

Or here: What does a semicolon do?

They are used to put multiple statements on a single line, so the interpreter is just ignoring it since there is not a second statement.



Answered By - Serial
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.