Multiple lines

When we write multiple lines of code in the Python interpreter (for example, the If statement and for and while loop functions), then the interpreter uses three dots (...) as a secondary prompt for line continuation. To come out of these lines, you have to press the Enter key twice. Now we will look at the following example:

>>> val1 = 2500
>>> val2 = 2400
>>> if val1 > val2:
... print("val1 is greater than val2")
... else:
... print("val2 is greater than val1")
...
val1 is greater than val2
>>>

In this example, we've assigned integer values to two variables, val1 and val2, and we're checking whether val1 is greater than val2 or not. In this case, val1 is greater than val2, so the statement in the if block gets printed. Remember, statements in if and else blocks are indented. If you don't use indentation, you will get the following error:

>>> if val1 > val2:
... print("val1 is greater than val2")
File "<stdin>", line 2
print("val1 is greater than val2")
^
IndentationError: expected an indented block
>>>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset