The statements under 'if' are considered a part of one 'block.' The other statements are not part of the if block and are not considered when statements of 'if' are evaluated. Let's take an example where we intentionally differ from the proper indentation: Python will automatically change the text color if you deviate from this indentation and display an error when you run your code. We can see here that Python delivers an error message as: "Expected an indented block. "Īlso, note the color of 'print' in line 3. The color variation happens because of the abrupt indentation of 'print.' All the other text is green, while 'print' has the color red. When we have maintained the indentation of Python, we get the output hassle-free. Suppose your ‘ if’ condition is false and you have an alternative statement ready for execution. Now, suppose you have multiple if conditions and an alternative for each one. Now let us take an example for each case : Use of else clause: Then, you can use the elif clause and specify any number of situations.
0 Comments
Leave a Reply. |