Let's look at the following code:
a = 5
if a == 4:
asdfjah;asdfr
asdf
This runs without producing an error (or any output). The reason is that the block inside the if statement never runs, and that it could potentially not produce any errors. The block would not produce any errors if all of asdfjah
, asdfr
, and asdfr
are defined.
The semicolon just separates lines of code (it is rare for people to use it in Python: usually, if you want two lines of code, you just write them as two lines.
Now, if we change a so that the block under the if statement does run, we will get an error: Python will encounter asdfjah
, which happens to not have been defined, and will say that it's not defined:
a = 4
if a == 4:
asdfjah;asdfr
asdf
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In [2], line 3 1 a = 4 2 if a == 4: ----> 3 asdfjah;asdfr 4 asdf NameError: name 'asdfjah' is not defined
We can make the code run without errors by definining all the variables:
a = 4
asdfjah = 1
asdfr = "hi"
asdf = "hello"
if a == 4:
asdfjah;asdfr
asdf
This runs, but does not produce any output. That is because in order to produce output, we need to actually tell Python to print something. For example:
a = 4
asdfjah = 1
asdfr = "hi"
asdf = "hello"
if a == 4:
print(asdfjah)
print(asdfr)
print(asdf)
1 hi hello