hey thanks does and welcome to this
coding tutorial so
in this recording tutorial i'm going to
address the topic how to break one line
into multiple
lines so if you have a long line like
this one how can you break it into
um into longer into multiple lines and
so the first method is also the
one that is recommended by the pep 8
standard which is to use the parenthesis
and brackets like even
each time you use a bracket and like say
an opening bracket
python expects that there will be the
closing brackets so it will look in the
same line but if it
if the closing bracket does not appear
in the same line it will look in the
next
line okay so this is like you need to
know about a few python internals to
figure this out but
so for example if i'm if i'm opening
this list here addis bob
list and n so maybe let's first run the
code so that you can see what the code
does so it zips together those two lists
and we can print a ah yeah so we get the
the sipped list i mean it doesn't really
matter what the code does it just
just need to see the output and now you
can um
for example here we have opened opened
up the list so we can
basically write those statements
in following lines and we have opened up
this
argument lists of the zip functions so
we have an opening parenthesis
this means that we can also write all
arguments in
a single line of python code and if you
use a standard python editor then it
will automatically align
the different hierarchical structures so
for example this whole list this is one
argument for the zip function
and this list is one argument to the zip
function so therefore those two lists
will have the same alignment okay
so because they are hierarchically
equally structured
so um yeah this is the first way of
writing
of writing python code so the same
python code into a single line of python
code it has the same it produces the
same
output so it is
basically equal to the to the to the one
used before
and but it's it's maybe nicer to to
understand and to see it
uh maybe not in this case but in many
other cases it's just nicer to break
things up
and to write it in multiple lines
instead of having a single line of
python code
um good so what's this the second method
the second method say we have
um a longer computation like this one
so one plus two plus three plus four
minus five times two
now we can um we cannot we cannot
basically do the following so like
write this into a single line of python
chord because then python will
complain it will draw an invalid syntax
error so how can we fix it
we can use the line
this backslash single backslash just in
front the new line break
and what it does internally this
backslash is called the escape
character so it escapes the character
that
just follows afterward and the character
that follows after what is a new line
character you cannot see it but it is
here
um the new line character and so
basically
now we can have a few empty spaces that
this doesn't actually matter
um or you can also have an arbitrary
number of empty spaces in a single line
so therefore we can also
have a number of empty spaces in a new
line uh so
those are just ignored and now with the
escape um
character um with escape
backslash we also escaped the new line
character so therefore it
seems to to the python interpreter that
everything is in one line so we can also
execute this code and it doesn't throw a
syntax error
and we if we print a we get the result
zero which is the result of this
computation right
um good so so this is possible and this
way you can also structure the
um computation and like one one quick
hint i want to give you that this would
be the wrong way of uh
actually breaking the line according to
the pep standard so the pep standard
um expects you or um
recommends that you break the line
before the
operator before a binary operator like
the plus and minus and
and multiplication asterisk operator all
of them are binary operators so they
combine
two operands and here
we break before the plus so that the
plus appears in the new line so this is
just easier to read
if the plus appears in the new line
because you see that the expression is
not over yet
and you see um yeah the the type of
expression that is executed okay so good
this is the second way of
breaking a long line into multiple lines
there's also a third la
third way if you have uh say
a long string with some new line
characters
this third way only works for strings
with new line characters
so here we have a very long string uh
now a citation of
in shakespeare we have some new line
characters in the string now we can
simply use a mighty line
string and instead of the new line
character we have a line break in our
mighty line string and the mighty line
string it starts with a triple quote
and it also ends with a triple quote now
let me quickly replace all of the
newline characters
and we end with a triple quote and now
we have simply used the new line
the like the real new line in the editor
instead of the new line character and we
have used the triplet quote
uh to indicate that the string spans
multiple lines this is not not
possible for the single quote for a
normal string a normal string can
can only span about span one line so a
normal string
only is a like one line string but now
if you have a multi-line string
and we print the result to the share now
you see that it's basically the same
string we have uh looked at before with
a new line character
but we have used really the line break
in our editor instead of
the new line character so this also
works beautifully so these are the three
ways of breaking a long line into
multiple lines and the combination of
them is usually enough
to for you to actually break up
some complicated one-liners that you
cannot write into a single line of
python code
good so if you are interested in python
one-liners then check out my book python
one-liners it will teach you the power
of a single line of python code so
everything there is to know
about the single line of python code it
will make you a better coder it will
ensure that you can understand each and
every single line of python code you
will ever encounter
in practice so this is the promise of
the book so check it out python
one-liners i give a link in the
description below
thanks for watching and see you in the
next video bye