Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

In this video you'll explore python

syntax, and learn how both white space and indentation can impact a program
when used incorrectly. In VS code, I create a new file called
python underscore syntax dot PY. I start by using a print statement
to generate a line of text. Don't worry if you're not
familiar with print or variable declaration at this point that
will be covered later in the course. I type print followed by the string,
Hello. When I click on the run button, the text,
Hello, appears in the terminal panel. Now let's say we want to use another print
statement on the same line which will output the text value world. So I add a space
and then type another
print statement with the string world. And we expect this to give
us the words hello world. But when I click on run we actually
get an error specifically. It says syntax error invalid syntax. This happens
because the interpreter
doesn't know when the new line or statement occurs. There are two ways to solve
this problem. One is to move the second print
statement to another line. I do this by placing the text cursor
before the print statement for world and then pressing the enter
key to move it down one. When I click on run this time there is
no error and I get the words hello and world on separate lines. Let me undo the
edits I made to my
code by pressing control and Z or command Z on a Mac. And then try the second
method which is
separating the two print statements with a semicolon in a space. When I click on
run again,
it also runs both statements as expected. Next you'll cover the impact of
whitespace in python syntax. I first clear my screen and
then declare a variable and assign it a value by typing x = 1 + 2. On the next
line,
I will add a print statement for x. Before I click on run, however,
I'll go back to my variable assignment and add a random number of spaces
around the plus symbol. Doing this will not cause
any problems with this line. However, issues will arise
if I add a new line or an end statement to demonstrate it. Let's input a new line
and type plus three writing this code
then returns a value of three. What has happened is that the interpreter
has executed our first line of one plus two correctly despite
the extra white space. But it did not account for
the plus three on the second line. There are a few ways to
work around this issue. The simplest approach is to
use a force line to do this. I type a backslash at
the end of the first line. Now, when I click on run
it returns a value of six, which means that both lines
have been accounted for to summarize any amount of whitespace or
indentations on a line is fine. But keep in mind that if you're
combining it with additional lines. Then you will need to give clear
indicators of where a new line has occurred next. You will explore indentation in
python. I start by clearing my screen and declaring the new variable name
with a string value of John. I want to write an if statement which will
return John only if the name variable has a new value of John. I do this by typing
if name == John. And then on a new line I
input the print statement for name to make this program work. I need to have an
indentation before
the print statement which V s code added automatically. When I click on run,
I get back John as expected. But what happens when
the indentation isn't there? If I delete the indentation from
my code and then run it again. I get the error. Indentation error expected
an indented block. This tells us that an indentation was
not found where it should have been. Fortunately the error message directs us
the specific line where the issue was detected. I could then edit my code and
fix it when writing programs in python. It's a good habit to read the output
whenever you encounter an error as you are often given the specifics
of what went wrong and where it happened as you noticed here.

You might also like