Detailed Notes
This episode looks into console input and output. We also touch on some type conversion functions (int, str, float) to help with these.
Helpful links: https://docs.python.org/3/tutorial/inputoutput.html https://docs.python.org/3/tutorial/floatingpoint.html
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] well hello and welcome back we are continuing our series where we're looking at all the things we need to know to get our certified associate in python certification and we've been moving our way slowly but getting there i worked our way through some operators and now today we're going to start looking at input and output before i do one of the things we're not really going to cover because it is just not very interesting to cover is and there's usually only one or two questions and most on it but i do want to point out take a look at uh basically just you can sort of google uh accuracy of floating point numbers and probably you know in python and you'll see some discussions of why particularly floating point numbers are not exact and like as they are stored in the computer because there are some things that just are not they it is impossible to have you know essentially because there's you know the short description is if you think about it there's essentially an infinite number of numbers between zero and one you could have point one point one one point one one one one point one one one one two one one one one blah blah blah blah so since there's an infinite number you don't have an exact representation of each of those in the computer it doesn't have an infinite amount of bit combinations basically to cover all of those that means there's gaps you find this in any programming language anything in a computer but it's good to know just to know about it to understand that the way that a computer lists something and the way it's going to display it is not necessarily identical and this can cause problems when you get into very complicated math if you've got very very large numbers particularly very small numbers if you get into a lot of decimal you know with a lot of decimal points and doing multiplication or adding a whole lot of numbers together you'll find things don't quite work out as you can get rounding errors basically and short of that is do your best to you know deal stick to integers but that's not always obviously possible so be aware and like i said they'll probably be a question on a on a test you know maybe not on all of them so take a look at that accuracy of floating point numbers in python now we have talked about some of the output things already we have already seen print a couple times although we're going to go back to it we have not yet talked about but we will talk about input and then with this um we have i believe we have looked at both string whoops if i spell that right and maybe int and maybe float i can't or if we've looked at each of those or not we're going to talk about those and then particularly with the print i want to talk about the end and the separator i'm going to look at those then i'm going to have some notes on these as well so you know some additional links for doing some uh some more research on them so the basic is first we have print and so this will be now this is a simple string output easy enough if we do this then we see that that string shows up now we can also do let's do this x equals now one that we haven't looked about at yet is this which is similar but i want to mention it oh sorry actually i think we have to do a print of that to get it out there we go and so you can see that you've got x is a value but if you want the uh more information about it this particularly as we get into like classes and stuff like that if you would force the representation the repr then you're going to get it with quotes uh not a major thing but just one of those neat little things in there input is uh so we're going to do y equals input enter a value print y and let's see what happens because this is the most likely thing to see so enter value qwerty boom qwerty so input's not terribly complex sort of what you would expect with input what you're end up doing is you give it a string that is going to be your prompt like you see here and then you could put like a i could put a if i do a line feed then do it there notice now i'm entering on the other and i oops depending on what i have i don't necessarily have all my key mappings so then it prints it back out by default it's going to be on the same line and you could also do it let me just do this real quick which is very complicated i can do it without and so now it's just waiting for something there you go so you really probably don't want to do that so if i do enter a value in here i'm going to do you entered and i can do this i believe that's the format let's find out in a second yeah that's not the exact format because and let me flip back over to that uh that's not what i want i think this is what i want so i always forget that little guy when we're talking about it oh it's dot format that's what it is okay so i can do this and it's gonna be dot format i think it has to be let's see so if i just do that i should be okay one what closes let's see there we go so that closes that oh let me get back to this guy there we go uh oh wait and here we go so you enter qwerty now i could also do it like this let's see enter a second value let's do another value and then you entered that and that so now i can do red and another one blue so i entered red and blue but i can also swap these i can put an order to this red blue and see now it says blue and red so i can actually based on this is zero indexed into the parameters that are part of the format so i can actually get i'm minority complicated pretty quick if i wanted to do that and so that's one of the ways that we can do our print another we can do is let's do a equals uh let me just see i'm going to still an example and this link will be there so let's do well let's do it like this let's do a equals that i know if i do print a let's just start with this and then let's actually do we'll play around this a little bit well oh a b there we go and so the representation is the same as it's going to get so you you're sending this array out but now one of the things i can add is uh let me get rid of this now i can do comma let's do a separator equals and let's do a star maybe i need to get rid of those inputs oh and let's go here so if i if i print them out at just a comma a list so this was before this is an array but now this is just an actual list of items so i can print any number of items if i want to and they can be all kinds of stuff so i can mix it up i can make this red i can make this a car and i can make this 3.4 so if i do that let me i'll leave those there for now so if i do a b then we're going to see that it has each of those in there and it separates them on by default with the space but what i can also do is i can come in and i can do a separator equals and then let's make the separator a pipe which these are kind of things you may actually find rather value when you're dealing with output and stuff like that so now i have a nice little pipe separator on each one of those if i also wanted to do something at the end equals let's just do eol and we'll just do it this way we'll do it like this now if i do it we can see here that i get the pipe separators and then i get the eol now notice that it does not i don't have at the end of this thing a uh online feed so i could take here uh let's see how i want it well i could do end of line let me do it this way because i think if i do it like this let's see what that does that may give me what i need yeah there we go so now that oil you don't see the printed end of line but note that that's only going to be there because i forced that in there so i can add separators to accommodate separate to a list of comma separated values within the print and it's not csv text it's actually just a list of values and i can add separators and i can do n's to that let's see what else we got so we've got here so let's do a equals b equals 2.5 and c equals let's do this because that's a string string and let's say c equals 2.5 so now if i try to get to the end and print a plus one we've run into this one before because if i go in and do that i can't that's uh that second part cannot can only concatenate string not integer to a string so here what i would need to do is convert that and watch if i do that it's going to say okay oh shoot i need to get my let me print let me just do this uh let me get a couple lines here just to get that so we can see a little better there we go okay so we come in and now it's printing 1 plus 1 equals 2 because i'm converting them now if i do the same thing and i try to do now if i do b if i do n b plus 1 then note now i'm getting it in valor literal because that's not an n it's not going to convert this 2.5 to an n so in this case i need to do a float and if i do that now we have it now note in this case it's going to just because it's a 1 it automatically is going to convert that over if i did that plus a then note that i'm back to the problem with a float and a string so i would have to do float a to get that so now there we go so 3.5 so i need i've got my ants and my floats and of course the other thing i can do if i want to be really weird is i can do let me convert both of those to a string and so now we're going to take 2.5 as a string plus 1.0 and let's see how this one blows up for us notice it's 2.5 and then it concatenates 1.0 because remember that the plus does a concatenation of strings so that gives us our prints our inputs and um casting as both our int's our strings and our floats so it's a basic input and output and there is some there's some other stuff we're going to deal with as far as writing the files but this should give us our first one and of course importantly um because we use this a lot actually is the format and so that's just remember that i can say um so i could do greeting equals hello blah blah blah i hope you realize it is blah out and i need to spell that right and then i can do print and let's see let me do name equals bob weather equals sunny and now i can take the greeting dot format and then i'm just going to give it a list of parameters now remember these could be i could make this 0 and 1 just to be specific or i can just assume it's all going to fall into place so if i'm going to do that then one is going to be name and the second one is going to be weather now i can also do um let's do parms equals name comma weather and i think this is going to work for me as well did i call it part i did so now do a little inputs and it is out of range so we did do hello bob but that second one it did not take that because it's a tuple i'm sorry i think it's this way maybe let me make sure i get this right no okay so it's taking it oh because unfortunately it is taking it as a it takes that as a single variable so the format it's going to try to it's trying to format this instead of converting it in there's another way to do that but i forget how so i'm not going to mess with it because this is cleaner anyways so in doing so we can add all kinds of formatting and have like template strings and things of that nature that will help us out quite a bit i think that's a good point to wrap it up for this one and um i guess next time we're probably gonna i think we're gonna move on from this so the next thing we're gonna actually probably tackle there's gonna be some conditional statements so we'll start some ifs and else's and stuff and start playing around a little bit with creating some little you know scriptlet type applications as we're starting to now have a couple of things in place that we can use to uh to make something interesting as always this is updated this will be updated in the github repository and take a look at the examples there play around with it don't forget take a look at the limitations and issues that exist in floating point numbers in python the accuracy of those numbers and as always go out there and have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
well hello and welcome back
we are continuing our series where we're
looking at all the things we need to
know
to get our certified associate in python
certification
and we've been moving our way slowly but
getting there
i worked our way through some operators
and now today we're going to start
looking at
input and output before
i do one of the things we're not really
going to cover
because it is just not very interesting
to cover
is and there's usually only one or two
questions and most on it
but i do want to point out take a look
at uh basically just you can sort of
uh accuracy of floating point numbers
and probably you know in python
and you'll see some discussions of
why particularly floating point numbers
are not exact
and like as they are stored in the
computer
because there are some things that just
are not they
it is impossible to have you know
essentially because there's
you know the short description is if you
think about it there's essentially an
infinite number of numbers between zero
and one
you could have point one point one one
point one one one one point one one one
one two one one one one blah blah blah
blah
so since there's an infinite number you
don't have an exact representation
of each of those in the computer it
doesn't have an
infinite amount of bit combinations
basically to cover all of those
that means there's gaps you find this in
any programming language anything
in a computer but it's good to know
just to know about it to understand that
the way that a computer
lists something and the way it's going
to display it is not necessarily
identical
and this can cause problems when you get
into very
complicated math if you've got very very
large numbers
particularly very small numbers if you
get into a lot of decimal you know with
a lot of decimal points
and doing multiplication or adding a
whole lot of numbers together you'll
find things don't
quite work out as you can get rounding
errors basically
and short of that is do your best to you
know deal stick to integers but
that's not always obviously possible so
be aware and like i said they'll
probably be a question
on a on a test you know maybe not on all
of them so take a look at that accuracy
of floating point numbers in python
now we have talked about some of the
output things already we have already
seen print a couple times although we're
going to go back to it
we have not yet talked about but we will
talk about input
and then with this um we have
i believe we have looked at both string
whoops if i spell that right
and maybe int and maybe float
i can't or if we've looked at each of
those or not we're going to talk about
those
and then particularly with the print i
want to talk about the end
and the separator i'm going to look at
those
then i'm going to have some notes on
these as well so you know some
additional links for doing some
uh some more research on them so the
basic is first we have print
and so this will be now this is a
simple string output
easy enough if we do this
then we see that that string shows up
now we can also do let's do this
x equals
now one that we haven't looked about at
yet
is this which is similar
but i want to mention it
oh sorry actually i think we have to do
a print
of that to get it out
there we go and so you can see that
you've got
x is a value but if you want the uh
more information about it this
particularly as we get into like classes
and stuff like that
if you would force the representation
the repr then you're going to get it
with
quotes uh not a major thing but just one
of those neat little things in there
input is uh so we're going to do
y equals input
enter a value
print y and let's see what happens
because this is the most
likely thing to see
so enter value qwerty boom qwerty
so input's not terribly complex sort of
what you would expect
with input what you're end up doing is
you give it a string
that is going to be your prompt like you
see here
and then you could put like a i could
put a if i do
a line feed then do it there notice now
i'm entering on the other and i oops
depending on what i have i don't
necessarily have all my key mappings
so then it prints it back out
by default it's going to be on the same
line and you could also do it
let me just do this real quick which is
very complicated i can do it without
and so now it's just waiting for
something there you go
so you really probably don't want to do
that so if i do enter a value
in here i'm going to do
you entered
and i can do this
i believe that's the format let's find
out in a second
yeah that's not the exact format because
and let me flip
back over to that
uh that's not what i want i think this
is what i want
so i always forget
that little guy when we're talking about
it
oh it's dot format that's what it is
okay
so i can do this and it's gonna be dot
format
i think it has to be
let's see so if i just do that
i should be okay one what closes let's
see there we go so that closes that
oh let me get back to this guy
there we go uh oh wait
and here we go so you enter qwerty now i
could also do it
like this
let's see enter a second value
let's do another value and then you
entered that
and that
so now i can do red and another one blue
so i entered red and blue but i can also
swap these
i can put an order to this
red blue and see now it says blue and
red so i can actually
based on this is zero indexed
into the parameters that are part of the
format so i can actually get
i'm minority complicated pretty quick if
i wanted to do that
and so that's one of the ways that we
can do our
print another we can do is let's do a
equals uh let me just see i'm going to
still an example and this link will be
there
so let's do well let's do it like this
let's do a equals
that i know if i do print
a let's just start with this
and then let's actually do we'll play
around this a little bit
well
oh a b there we go and so the
representation is the same as it's going
to get so you
you're sending this array out but now
one of the things i can add
is uh let me get rid of this now i can
do
comma let's do a separator
equals and let's do a star
maybe i need to get rid of those inputs
oh
and let's go here
so if i if i print them out at just a
comma
a list so this was before this is an
array
but now this is just an actual list of
items so i can print
any number of items if i want to and
they can be all kinds of stuff so i can
mix it up i can make this
red i can make this a car
and i can make this 3.4 so if i do that
let me i'll leave those there for now
so if i do a b then we're going to see
that it has
each of those in there and it separates
them on by default with the space
but what i can also do
is i can come in and i can do a
separator
equals and then let's make the separator
a pipe
which these are kind of things you may
actually find rather value
when you're dealing with output and
stuff like that so now i have a nice
little pipe separator on each one of
those
if i also wanted to do something at the
end
equals
let's just do eol and we'll just do it
this way we'll do it like this
now if i do it we can see here
that i get the pipe separators and then
i get the eol now notice
that it does not i don't have at the end
of this thing
a uh online feed so i could take
here uh let's see how i want it well i
could do end of line let me do it this
way because i think if i do it like this
let's see what that does that may give
me what i need
yeah there we go so now that oil you
don't see the printed
end of line but note that that's
only going to be there because i forced
that in there
so i can add separators to accommodate
separate to a
list of comma separated values within
the print and it's not csv
text it's actually just a list of values
and i can add separators and i can do
n's to that
let's see what else we got so we've got
here so let's do
a equals b equals
2.5
and c equals
let's do this because that's a string
string
and let's say c equals 2.5
so now if i try to get to the end and
a plus one
we've run into this one before because
if i go in and do that
i can't that's uh that second part
cannot can only concatenate string not
integer to a string
so here what i would need to do is
convert that
and watch if i do that it's going to say
okay oh shoot i need to get my let me
let me just do this uh let me get a
couple lines here just to get that so we
can see a little better
there we go okay so we come in and now
it's printing 1 plus 1 equals 2 because
i'm converting them
now if i do the same thing
and i try to do now if i do b
if i do n b plus 1
then note
now i'm getting it in valor literal
because that's not an n it's not going
to convert this 2.5 to an n so in this
case
i need to do a float and if i do that
now we have it now note in this case
it's going to just
because it's a 1 it automatically is
going to convert that
over if i did that plus a
then note that i'm back to the problem
with a float and a string so i would
have to do
float a
to get that so now there we go so 3.5
so i need i've got my ants and my floats
and of course the other thing i can do
if i want to be really weird
is i can do let me convert both of those
to a string
and so now we're going to take 2.5 as a
string
plus 1.0 and let's see how this one
blows up for us
notice it's 2.5 and then it concatenates
1.0 because remember that the
plus does a concatenation of strings
so that gives us our prints our inputs
and um casting as both our int's our
strings
and our floats so it's a basic input and
output
and there is some there's some other
stuff we're going to deal with as far as
writing the files but this should give
us our first one and of course
importantly um because we use this a lot
actually
is the format and so that's just
remember
that i can say um
so i could do greeting equals
hello blah blah blah
i hope you realize it is
blah out and i need to spell that right
and then i can do print and let's see
let me do name equals
bob weather equals
sunny and now i can take the greeting
dot format and then i'm just going to
give it a list of parameters now
remember these could be
i could make this 0 and 1 just to be
specific
or i can just assume it's all going to
fall into place
so if i'm going to do that then one is
going to be name and the second one is
going to be
weather now i can
also do
um let's do parms equals
name comma weather
and i think this is going to work for me
as well
did i call it part i did so now do a
little inputs
and it is out of range so we did do
hello bob but that second one it did not
take that because it's a tuple
i'm sorry i think it's this way maybe
let me make sure i get this right
no okay so it's taking it oh because
unfortunately it is taking it as a it
takes that as a single variable
so the format it's going to try to it's
trying to format this
instead of converting it in there's
another way to do that but i forget how
so i'm not going to mess with it because
this is cleaner anyways
so in doing so we can add all kinds of
formatting
and have like template strings and
things of that nature that will help us
out quite a bit
i think that's a good point to wrap it
up for this one
and um i guess next time we're probably
gonna
i think we're gonna move on from this so
the next thing we're gonna actually
probably tackle
there's gonna be some conditional
statements so we'll start some ifs and
else's and stuff and
start playing around a little bit with
creating some little you know scriptlet
type applications as we're starting to
now
have a couple of things in place that we
can use to uh to make something
interesting
as always this is updated this will be
updated in the github repository and
take a look at
the examples there play around with it
don't forget take a look at the
limitations and issues that exist in
floating point numbers
in python the accuracy of those numbers
and as always go out there and have
yourself a great day a great week
and we will talk to you next time
you