In this video we cover some of the top tips and tricks for maintaining clean and proper syntax python code! This is often referred to as Pythonic programming by python developers and it covers some of the most common tips and tricks for writing good object oriented programming.
First is covering the need to write code in clear single-line statements. Writing one statement per line of code is a MUST for maintaining easy to read and easy to troubleshoot code.
Second is using only one return statement inside functions when possible. Having a single input and output location again allows for easy debugging and troubleshooting of one's code!
Third is examples and discussions of using explicit definitions whenever possible and shows the potential drawbacks of using implicit definitions within functions or porgramming!
What other tips or tricks do you think make a program 'pythonic'? Let us know in the comments below and be sure to subscribe to the channel for tons more tutorials and sample projects in the future!
Thanks for watching and goodluck with your code!
what's going on you guys welcome back to
the channel and today's video is going
to be tips and tricks for writing more
pythonic python code
[Music]
so when i say pythonic this maybe is a
term that you've heard referred to if
you've ever been asking for help on the
internet or read it and people will tell
you well your code works but it's not
completely pythonic and what they really
mean by that is that there's a style of
writing your code that object-oriented
programming and especially python where
it's maybe not actually improving the
functionality of it but it improves the
readability the simplicity of your code
and it makes it kind of used in the
format that the python programming
language was intended to be used so
it's kind of like your code may work but
there could be a much simpler and more
efficient way of writing it and these
are a few of the tips for how to make
sure you're following those standards so
the first thing is
one statement of line per code
and this one is probably the
most
common across all programming languages
so the next two that we'll look at in
this video are maybe a little more
python specific but really however you
program whatever language you write it
in
putting more than one statement or
function on one line of code is just
overly complicating your programming
needlessly
so one statement per line of code and
let's look at a few examples because you
could
so
don't do this
but you could print
the word
one
and then on that same line of code you
could print two
print
two and just separate them by a
semicolon so you could do this and it'll
run
extra punctuation and it'll run let me
go ahead and run that and you can see
you get one and then you get two on the
next line
but
do
this
the right way to do this and obviously
i'm using simplified examples just so i
can show them in a quick video but this
becomes more of an issue with more
complex programming so you're going to
see if i run this and i'm going to
separate out the good and the bad by
just a blank line so it's a little more
clear
i'm going to run this we're going to see
the exact same output twice one two one
two
but if you were debugging or working
with this code
and you had to go through and you had to
look and see and say this wasn't just
printing one and printing two this was a
complicated math function
and then this was a complicated math
function put them both on their own line
and that really cleans up your code it
makes it easier for troubleshooting and
debugging because a lot of the times
when you get error messages in python it
tells you what line the error is
occurring on and so if you have two
complicated things going on in the same
line you now don't have as specific of
troubleshooting either so that's uh one
tip and maybe a better example of why
you would want to do that
let's say you had a multi-variable
condition like you want an alarm that
automatically turns on if it's a weekday
and if it is uh if you're not on
vacation so let's say you have two
variables you have day
and it's sunday and then you have
vacation which is just a true or false
so you're not on vacation well what you
want is this variable alarm to be true
if it's a weekday or if you're on
vacation so if you're doing all of that
on one line of code you're going to be
doing
a check if day is equal to
monday
or and i'm going to copy this to speed
up a little bit
but here you'd be checking or tuesday or
wednesday
thursday
friday
and you're checking to see if
not
vacation so you're checking to see if
you're not on vacation and if the day
is monday
tuesday
wed
nest day
thursday
and here we go let's finish it off
friday and we don't need that last or
so strictly speaking this value is going
to tell you whether or not to run your
alarm
so i'm going to go ahead and run that
and we'll see that when it is sunday but
you're not on vacation your alarm should
still not be on because it's not a
weekday
well the pythonic way to do this and
what we're going to do now i'm going to
copy these three down into the good
section and we're just going to modify
them
don't don't just stick day in here five
times for this check do an intermediate
variable call it weekday and make it
equal to these conditions in here okay
so weekday is a true or false variable
now which is equal to any of these
conditions where the day is equal to
monday tuesday wednesday or thursday
that simplifies this statement to check
if it's a weekday and you're not on
vacation this is pythonic code
where you can say okay we have a
variable that's just telling us what day
it is we need to create conditions
stating that when it's a weekday and use
an intermediate variable call it weekday
and create the complicated parameters
for it in this and then you'll see when
i run this now it's going to give us the
exact same output as that previous line
of code and your engineer software
computer science brain may say well less
lines of code is a good thing so like in
this case we reduce the number of lines
of code and intermediate variables
actually less lines of code is not
always a good thing if you're
complicating your program that's kind of
the essence of pythonic code so one
statement one complicated check one
operation per line is tip number one in
python
tip number two is going to be one return
statement per
function
so
you maybe have seen python programming
depending on your level of knowledge in
python where you have a complicated
function and i'm going to bring one in
just for this example
where you have let's say three variables
okay i'm just this is a
three variable function that i called
complicated function and what it does is
it takes three values it checks for
equivalency between them so first we
check if a is equal to b and a is equal
to c
and then we check to see if just one set
is equal to each other and then at the
end we also want an else that would
return
nothing is equal so
if we just go ahead and run this like if
i were to print
complicated function
oops
complicated function with our variables
a b and c then you'll see with values of
10 12 and 10 we would expect to see a is
equal to c
okay a equals c but not b so
that's fine but what you'll see is
inside this function we have five spots
that stuff could be returned to us and
as you create really elaborate functions
where you're doing a lot of intermediate
work this is actually pretty
straightforward to trace but the idea
remains the same you don't want multiple
exit points from your programming just
like you wouldn't want multiple input
points because you don't realize at what
point you're gathering the data a good
function has one point of entry and one
point of exit and so what i mean by that
let's go ahead and take a look at the
good one now so again this is a don't do
this
let's go down to the
do this
okay so if i take this same function
and i'm going to start from the same
rubric
and i'm going to call this one
complicated function 2 so i can call
them separately
i'm going to create a variable i'm going
to say equal to result and now you'll
see you have to initialize it inside
your function but this kind of goes back
to what we were saying in the previous
tip about simplifying your code
and creating an additional variable
adding a line of code for the result and
then having to return the result at the
end of the program might actually seem
like we just added
a couple lines of code
and you need equal signs
it may seem like we just added two lines
of code in another variable but what
we've actually done is we've taken a
function that had five exit doors four
exit doors however many exit doors
where it could leave our function at any
point and now we can trace really
clearly the input and the output of the
whole thing and so you'll see again i'm
going to print out complicated function
2 with our same variables a b and c
and we're gonna run it and you're gonna
see they're we're getting the identical
output again
because these tips again are not ways of
modifying code that wasn't working
they're ways of
improving and simplifying your code the
way python and object-oriented
programming was intended
so the idea behind this is use an
intermediate variable if you want to
exit the code at several different times
just figure out
what it is they have in common and stick
that in a variable and then just return
that variable at the very end once
you're done doing all of your processing
it's easier for tracking it's better
practice and that's tip number two
so tip number three is going to be be
explicit in your code
and uh what i don't mean by that is
explicit as in swearing explicit as in
clearly define what you're doing
um and so the idea behind this is
uh you can at times simp
do what you think is a shortcut like
return inside of a variable excuse me
inside of a function return all of the
local variables inside of that function
in a dictionary
because python has a built-in tool for
that the problem is you don't really
have clear eyes on what you're sending
back to the user then
so let's do an actual example of this
more coding less talking
if i have variable a and variable b and
we won't get in trouble because we're
redefining them
but if i have those two variables and i
define my function as something that
receives an a and a b
and then inside of this function
we are going to do some operation that
we'll just say is equal to a plus b and
then we decide to return
uh because now we want to be clever we
know a little python and we want to use
a shortcut
and you return this dictionary of all of
the
locals and if you don't if you're not
familiar with this nomenclature
don't worry about it
but
if you've ever seen this locals this is
a way of getting all of the variables
used inside of a dictionary as a
used inside of a function as a
dictionary
where you get as the key for each
dictionary entry you get the variable
name and then the value is whatever
value they have inside of them
and i'll just uh to show that i'll kind
of print
what happens if my function
my funk
and then we pass in a and b
okay
and let's go ahead and run that
that should do it
that should do it
okay so we're getting this dictionary
that says a is equal to one b is equal
to z is equal to three and that should
make sense because uh we're just adding
a and b together but this is actually
not the properly explicit way to do
pythonic code so um again don't
do this
and let's show what you could do instead
um
now let's say we're gonna have all of
this
we're going to do my funk 2
and we're going to do my funk 2. if you
think that what you want to return
is a dictionary of all of your values
like that you think that that is what
the user wants the actual proper way to
do it is going to be this way you you
say the name of the variable and then
you punch in what the value should be
the name of the variable
punch in what the value should be
and the name of the variable punch and
what the value should be so uh again
let's go ahead and run this
it may seem like i'm repeating myself
but that's because pythonic code really
boils down to the same couple of
concepts and doing them
right every time
you can see here the output is the exact
same as the first function and so you
may say to yourself like well i've got
12 local variables and you know i'm
clever and i know this built-in honestly
if you're at the point where you're
writing really complicated functions and
you're doing really advanced python you
can make the judgment call of whether or
not your program is going to work this
way for a beginner starting out in a lot
of your troubleshooting you're going to
find it's tricky to trace a lot of
non-explicit code so if you have things
that are implicitly defined using the
built-in functions of like locals well
in the scenario where you want to um
just pull one variable out and return
that because all you really need from
your function is the sum
if you wrote your program like this then
you're going to have to change it to
just return the sum whereas if you wrote
it like this then you can pretty quickly
see like oh okay i'm just going to grab
from index 2 of my dictionary
so these are not maybe things that you
run into constantly i imagine these tips
ranged from you know useful in every
single line of code to useful in some of
the functions you write to kind of a
special case but having all three of
these in mind when you're writing code
is a really good starting point to start
building out more extensive python files
and keep in mind a good way of keeping
simplicity um in mind when you're doing
it so again a lot of these things would
be considered ways of improving your
code writing more pythonic code but at
the end of the day the whole idea with
programming is that you create a
function that works for you once you're
on a team it needs to work for the team
so these are ways to create more easily
readable and deep debuggable uh code but
um there are a million different ways to
do it python's an extremely versatile
tool so you may find that there's a
scenario where it's not exactly pythonic
but it's what you have to do to get
working
so these are just general guidelines
also if you are aware of some other
python tips or you've heard of other
things that you don't completely
understand and you'd like to see me
discuss in a future video just let me
know about in the comments below i'll
try to get to it as soon as i can and um
be sure to check out the channel for
tons of tips and tutorials on python and
other platforms and if you did find this
or anything else on the channel useful i
really appreciate a like on the video
subscribe to the channel it helps me out
a ton and let me know what you'd like to
see in future videos in the comments and
maybe you'll see it on the channel
pretty soon so as always good luck with
your code and thanks for watching thanks
bye