Python-Explain the output for each of the following codes.

Why is it that line 1 prints "Hello World" while line 2,3 and 5 does not produce an error while line 4 of the code below produces an error? >>> print('Hello world') >>> 1/2 >>> type(1/2) >>> print (01) >>> 1/(2/3)

9/10/2019 10:34:40 PM

Chris Lutangu

1 Answer

New Answer


There is a fixed way of writing values in Python. As long as you write them correctly, you won't get an error. If you don't print them out, you will only see them in interpreter mode, but not if you wrote them in a program. Python would create your values - and forget them right away. 'Hello world' is a string (because of the quotation marks). It's a valid value, so print will output it. But 01 in line 4 is not a valid way to write an integer number in Python (can't start with a zero), so you get an error.