[Music]
hello everyone welcome back to mr.
random generator in this video I will be
showing you my five handy Python tips
starting with our list first we have
array subtraction so let's say we have array F
which consists of elements eight seven
[Music]
six five four and we have an array of
second which consists of elements six
five and four so if I want to get the
difference between F and s what I can do
is let's say it's just filtered out the
results so what I can do is I can use
the sets and feed it with our first
array and set
and feed it with our secondary array and the
result should give us to the front so as
you can see 8 and 7 is our different
elements this is really useful but
wanting to mention right here is that
this object is a type of type is list
and this has a type of set not
is a list so the generic things with the
general things that you do with array
will not work on sets so what I mean by
that let's say we just cut that and sign
that to a variable as you know in our
arrays I can reverse their order by
first writing the rape variable name of
the array or list if you prefer and I do
this and I feed minus 1 what that does
is it reverses my list so if I run that
we should see four five six seven eight
as you can see it is reversed however
the five feet by result into that and if
I try to run that we should get an error
as you can see we got an error because
the type is not list so what I can do is
convert my object to list and I can
easily do that by
you know following mesh now it should
work and I should be getting seven and
eight as you can see now the list
operations work on my result object so
since we covered that we can move on to
our second tip on in on our list which
is the combined greater than or less
than signs so imagine that we have
variable a value of ten B value of let's
say AIDS and seal its value of six in
many programming languages what you can
do if you want to make a comparison is
that you generally go with let's say if
a is greater than B and if I want to
combine with another statement I usually
do but we usually do this just right and
like that but in obviously and has
dedicated keyboard in Python so I can do
this but if I do that it will work we
just run it as you can see it is working
properly but as you can see it is
underlining so what that does is see it
wants me that the chain come it simply
it can be simplified into a chained
comparison what that means is that this
I can simplify statements in a
mathematical manner so it is they are
basically much more understandable so
but one key thing to notice that is here
is that if you introduce parentheses the
results will differ so what I mean by
that let's say we run that the outcome
is true but if I add parentheses right
there the outcome will be false and why
is that and this is caused because
let's do it and step by step the a
is equal to 10 and B is equal to 8 so
tan is obviously greater than X as you
can see that will return the result of
true so true greater than C so what our
program understands that is you cannot
compare true with 6 however it can
compare one with six so it's it treats
true as one and compare that with six
but one is obviously not greater than
six so it is returning false so again it
is important to be careful with the
parentheses so after we have you know
just cover the basics we can move on to
do ternary operator acting which is
My third tip that enables you to basically
write if statements in a single sentence
like sentence based manner so it has the
following structure first you provide
the true case let's say you have if the
condition is true and you do this so you
put that true case in front of your
statement then you plug your if keyword
after that you put your condition then
you feed your CD else keyword and
default condition for statement so what
I want to say is let's demonstrate this
right here let's assume that our my true
condition is basically true string so
after that I can add it keyboard now as
you can see this is our condition and
after that I could just say else and my
post keywords
and if I run that we should be getting
through and let's say a previous case we
just introduced our parentheses so if we
introduced them we should be adding
force and obviously you can do much more
complicated stuff with this thing but
again this comes really useful and also
in a clear a clear way clear away so
after we just cover that I want to move
our focus onto a bit more different
stuff so stuff which I don't think it
will work on the R + pi/2 file but but
it should work on fighting console so
what that does is that if you type
something or if you basically execute a
function or get some returned stuff it
will be cached on our console and we can
access that in next steps so as you can
see I just typed in 6 and now I can
access 6 using my cache so and I did do
that by using the underscore if I let's
say underscore and multiply with 8 I
should be getting 48 now 48 is cached I
can just underscore to access 48 and
let's say I want to add 2 to it there
you go
we get 50 now you get the idea
this is actually really useful stuff if
you have repeated input sequences or you
just want to make some experimental
things about your while you're debugging
this is actually really useful thing for
a pile of debugging so yeah keep that in
mind and now we move on to our final tip
Anna in our list which consists of don't
using janky scopes so what I mean by
janky scopes let's say we have variable
X is equal to 10 then we create a basic
for loop which is like this
and inside our for loop we just have a
basic if statement if X is equal to 15
then print X if you exit to loop again
int X so
just a quick question if I run that
program what is the expected output? it
looks really basic but it is kind of
confusing what do you normally expect is
that first we answer our loop and we hit
15 let me print out 15 in our console
then we get out of the loop X is equal
to 10 and we print 10 15 10 should be
the result right? Wrong. We got 15 and 90
so how that works is Python actually
looks up two variables in a way that
let's say we have X variable inside our
parts and it says that oh I do have X
variable predefined so why don't
I use it instead so it go ahead and use
that and also it's important to note
that our loop is not going to start from
time it starts from 0 and assigns 0 to X
and then checks it and then iterate
through the whole and as you can see
actually I just create a quick print
statement right there and they let say X
and if I run that as you can see it's
rate truly hold loop and once it reaches
the 19 it exits through and again you
get printed the 13 right there and the
final x value will be the last value in
our look which is 19 so hence we got 15
and 19 so that's basically it for this
video hope you liked it and if that is
the case please make sure to hit like
button and consider subscribing see you
next time take care
[Music]