0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97

2 Answers2

1

Using semicolon is the way to write several statement in one line. Second is empty in your code.

weisert
  • 185
  • 5
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Serial
  • 7,925
  • 13
  • 52
  • 71
  • Re *"don’t use semicolons to end a statement in Python"*: Semicolons are used for [suppressing output in IPython](https://stackoverflow.com/questions/56572156/semicolon-at-end-of-python-statement-suppresses-output#comment99722886_56572156). – Peter Mortensen Nov 22 '22 at 19:34