-2

Code 1:

if 5 > 2:
 print ("Five is greater than two!")

Code 2:

if 5 > 2:
print ("Five is greater than two!")

What is the difference between these codes?

Code 1 has an extra space before print function and Code 2 doesn't have space before print, but when I am trying to run Code 1: it is showing "expected an indented block":

1

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Dora
  • 3
  • 4
  • 2
    indentation is important. – R4444 Jun 19 '19 at 05:38
  • 1
    Same as between "Let's eat, grandma" and "Let's eat grandma": different syntax. Read [indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) in documentation. – Amadan Jun 19 '19 at 05:39
  • Indentation Error: https://stackoverflow.com/questions/14979224/indentation-error-in-python – Amaresh S M Jun 19 '19 at 05:40
  • Indentation is necessary in Python – Omkar Jun 19 '19 at 05:41
  • If you have any problem with indentations at any line of your code that will be pop up in the execution time. So if you place both code1 and code2 in same script. The indentation problem will occur – Gihan Gamage Jun 19 '19 at 05:47

2 Answers2

2

Unlike languages like C that use special tokens to define blocks (i.e., { and }), Python defines blocks by indentation. In the second snippet, the print statement isn't properly indented under the if, resulting in an empty if block (which is illegal in Python), and then an unrelated print statement.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

You need indentation!

Even the one space indentation isn't so good, the best is to use four:

if 5 > 2:
    print ("Five is greater than two!")

Related: Python: using 4 spaces for indentation. Why?

U13-Forward
  • 69,221
  • 14
  • 89
  • 114