📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Input/Output To Console

2021-06-24 •Youtube

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
0.63

[Music]

26.16

well hello and welcome back

27.68

we are continuing our series where we're

29.599

looking at all the things we need to

31.599

know

32.32

to get our certified associate in python

35.12

certification

36.719

and we've been moving our way slowly but

39.52

getting there

40.48

i worked our way through some operators

42.079

and now today we're going to start

43.92

looking at

44.48

input and output before

47.6

i do one of the things we're not really

49.84

going to cover

51.52

because it is just not very interesting

54.64

to cover

56.079

is and there's usually only one or two

57.6

questions and most on it

59.28

but i do want to point out take a look

62.32

at uh basically just you can sort of

64.479

google

65.36

uh accuracy of floating point numbers

68.24

and probably you know in python

70.88

and you'll see some discussions of

74

why particularly floating point numbers

76.24

are not exact

78.08

and like as they are stored in the

80.96

computer

81.84

because there are some things that just

83.04

are not they

84.96

it is impossible to have you know

86.799

essentially because there's

88.4

you know the short description is if you

90.64

think about it there's essentially an

92.24

infinite number of numbers between zero

94

and one

95.119

you could have point one point one one

96.72

point one one one one point one one one

98.4

one two one one one one blah blah blah

99.759

blah

101.2

so since there's an infinite number you

103.119

don't have an exact representation

105.2

of each of those in the computer it

107.52

doesn't have an

108.24

infinite amount of bit combinations

111.119

basically to cover all of those

113.68

that means there's gaps you find this in

116.479

any programming language anything

118.24

in a computer but it's good to know

122.079

just to know about it to understand that

125.119

the way that a computer

127.28

lists something and the way it's going

129.599

to display it is not necessarily

131.28

identical

132.56

and this can cause problems when you get

134.16

into very

135.68

complicated math if you've got very very

138.4

large numbers

139.84

particularly very small numbers if you

141.52

get into a lot of decimal you know with

143.12

a lot of decimal points

145.12

and doing multiplication or adding a

147.76

whole lot of numbers together you'll

149.12

find things don't

150.239

quite work out as you can get rounding

152.879

errors basically

154.72

and short of that is do your best to you

156.959

know deal stick to integers but

159.04

that's not always obviously possible so

162.239

be aware and like i said they'll

164

probably be a question

165.28

on a on a test you know maybe not on all

167.599

of them so take a look at that accuracy

169.44

of floating point numbers in python

172.08

now we have talked about some of the

176.4

output things already we have already

178.159

seen print a couple times although we're

180

going to go back to it

181.36

we have not yet talked about but we will

183.12

talk about input

185.599

and then with this um we have

188.64

i believe we have looked at both string

191.36

whoops if i spell that right

194

and maybe int and maybe float

197.28

i can't or if we've looked at each of

198.48

those or not we're going to talk about

200.72

those

201.92

and then particularly with the print i

204.64

want to talk about the end

206.64

and the separator i'm going to look at

209.84

those

210.48

then i'm going to have some notes on

211.44

these as well so you know some

212.799

additional links for doing some

214.4

uh some more research on them so the

215.92

basic is first we have print

218.64

and so this will be now this is a

222

simple string output

225.04

easy enough if we do this

231.76

then we see that that string shows up

235.12

now we can also do let's do this

238.879

x equals

244.799

now one that we haven't looked about at

246.879

yet

248.72

is this which is similar

252.4

but i want to mention it

255.92

oh sorry actually i think we have to do

258.32

a print

259.12

of that to get it out

262.72

there we go and so you can see that

264.639

you've got

266.32

x is a value but if you want the uh

269.68

more information about it this

270.8

particularly as we get into like classes

272.56

and stuff like that

274.56

if you would force the representation

276.32

the repr then you're going to get it

278.24

with

278.8

quotes uh not a major thing but just one

281.6

of those neat little things in there

282.8

input is uh so we're going to do

286.88

y equals input

291.199

enter a value

295.12

print y and let's see what happens

298.08

because this is the most

299.04

likely thing to see

302.56

so enter value qwerty boom qwerty

306.88

so input's not terribly complex sort of

309.52

what you would expect

311.6

with input what you're end up doing is

313.039

you give it a string

315.36

that is going to be your prompt like you

317.759

see here

319.52

and then you could put like a i could

321.84

put a if i do

324.56

a line feed then do it there notice now

328.32

i'm entering on the other and i oops

331.199

depending on what i have i don't

332.4

necessarily have all my key mappings

334.88

so then it prints it back out

338.88

by default it's going to be on the same

340.32

line and you could also do it

342.88

let me just do this real quick which is

346.24

very complicated i can do it without

348.96

and so now it's just waiting for

350.16

something there you go

352.96

so you really probably don't want to do

355.039

that so if i do enter a value

356.8

in here i'm going to do

362.4

you entered

367.68

and i can do this

370.72

i believe that's the format let's find

372.96

out in a second

376.8

yeah that's not the exact format because

379.36

and let me flip

380

back over to that

383.84

uh that's not what i want i think this

385.68

is what i want

388.24

so i always forget

391.44

that little guy when we're talking about

394.319

it

395.199

oh it's dot format that's what it is

397.919

okay

400.24

so i can do this and it's gonna be dot

403.12

format

406.16

i think it has to be

410.08

let's see so if i just do that

414.08

i should be okay one what closes let's

416.479

see there we go so that closes that

419.44

oh let me get back to this guy

422.72

there we go uh oh wait

426.24

and here we go so you enter qwerty now i

428.479

could also do it

429.68

like this

435.68

let's see enter a second value

440

let's do another value and then you

443.44

entered that

444.16

and that

453.12

so now i can do red and another one blue

457.28

so i entered red and blue but i can also

460.4

swap these

463.52

i can put an order to this

467.36

red blue and see now it says blue and

470.56

red so i can actually

473.199

based on this is zero indexed

476.639

into the parameters that are part of the

479.12

format so i can actually get

481.039

i'm minority complicated pretty quick if

483.52

i wanted to do that

485.28

and so that's one of the ways that we

487.12

can do our

488.639

print another we can do is let's do a

492.08

equals uh let me just see i'm going to

494.08

still an example and this link will be

496.08

there

498.319

so let's do well let's do it like this

500.08

let's do a equals

506.56

that i know if i do print

509.84

a let's just start with this

514.24

and then let's actually do we'll play

516.08

around this a little bit

521.839

well

526.16

oh a b there we go and so the

528.8

representation is the same as it's going

530.48

to get so you

531.519

you're sending this array out but now

533.279

one of the things i can add

535.6

is uh let me get rid of this now i can

538.399

do

539.6

comma let's do a separator

542.8

equals and let's do a star

551.519

maybe i need to get rid of those inputs

553.92

oh

558

and let's go here

562.88

so if i if i print them out at just a

564.8

comma

566.08

a list so this was before this is an

568.399

array

569.519

but now this is just an actual list of

571.6

items so i can print

573.279

any number of items if i want to and

575.44

they can be all kinds of stuff so i can

576.88

mix it up i can make this

578.8

red i can make this a car

583.68

and i can make this 3.4 so if i do that

587.6

let me i'll leave those there for now

591.12

so if i do a b then we're going to see

593.519

that it has

594.399

each of those in there and it separates

596.24

them on by default with the space

599.279

but what i can also do

602.48

is i can come in and i can do a

604

separator

606.88

equals and then let's make the separator

609.839

a pipe

611.519

which these are kind of things you may

612.8

actually find rather value

614.8

when you're dealing with output and

617.2

stuff like that so now i have a nice

618.72

little pipe separator on each one of

620.16

those

622.079

if i also wanted to do something at the

624.8

end

627.519

equals

630.72

let's just do eol and we'll just do it

632.64

this way we'll do it like this

636.24

now if i do it we can see here

639.92

that i get the pipe separators and then

641.839

i get the eol now notice

643.68

that it does not i don't have at the end

645.36

of this thing

647.6

a uh online feed so i could take

655.44

here uh let's see how i want it well i

658

could do end of line let me do it this

659.2

way because i think if i do it like this

665.36

let's see what that does that may give

667.44

me what i need

670.72

yeah there we go so now that oil you

673.04

don't see the printed

674.64

end of line but note that that's

677.76

only going to be there because i forced

680.079

that in there

680.959

so i can add separators to accommodate

682.8

separate to a

685.2

list of comma separated values within

687.04

the print and it's not csv

688.959

text it's actually just a list of values

692

and i can add separators and i can do

694.32

n's to that

697.04

let's see what else we got so we've got

698.48

here so let's do

700.16

a equals b equals

704.04

2.5

707.519

and c equals

711.44

let's do this because that's a string

714.839

string

717.04

and let's say c equals 2.5

720.399

so now if i try to get to the end and

722.639

print

725.2

a plus one

730.16

we've run into this one before because

731.92

if i go in and do that

733.44

i can't that's uh that second part

736.72

cannot can only concatenate string not

739.12

integer to a string

741.519

so here what i would need to do is

744.56

convert that

746.88

and watch if i do that it's going to say

750.16

okay oh shoot i need to get my let me

752.48

print

757.519

let me just do this uh let me get a

759.92

couple lines here just to get that so we

762.079

can see a little better

764

there we go okay so we come in and now

766.48

it's printing 1 plus 1 equals 2 because

768.88

i'm converting them

770.32

now if i do the same thing

774.399

and i try to do now if i do b

778.639

if i do n b plus 1

782.56

then note

786.399

now i'm getting it in valor literal

788

because that's not an n it's not going

789.6

to convert this 2.5 to an n so in this

791.92

case

793.76

i need to do a float and if i do that

798.32

now we have it now note in this case

800.639

it's going to just

801.44

because it's a 1 it automatically is

804.24

going to convert that

805.2

over if i did that plus a

812.24

then note that i'm back to the problem

813.839

with a float and a string so i would

815.2

have to do

817.12

float a

821.44

to get that so now there we go so 3.5

827.36

so i need i've got my ants and my floats

830

and of course the other thing i can do

831.92

if i want to be really weird

833.36

is i can do let me convert both of those

836.079

to a string

839.839

and so now we're going to take 2.5 as a

842.639

string

843.199

plus 1.0 and let's see how this one

847.199

blows up for us

848.88

notice it's 2.5 and then it concatenates

852.32

1.0 because remember that the

854.16

plus does a concatenation of strings

859.519

so that gives us our prints our inputs

863.76

and um casting as both our int's our

867.6

strings

868.24

and our floats so it's a basic input and

871.6

output

872.48

and there is some there's some other

874.399

stuff we're going to deal with as far as

876.8

writing the files but this should give

879.68

us our first one and of course

880.959

importantly um because we use this a lot

884.24

actually

885.68

is the format and so that's just

888

remember

889.92

that i can say um

893.199

so i could do greeting equals

898

hello blah blah blah

904.639

i hope you realize it is

908.16

blah out and i need to spell that right

916.56

and then i can do print and let's see

919.76

let me do name equals

924.839

bob weather equals

927.76

sunny and now i can take the greeting

934.24

dot format and then i'm just going to

936.48

give it a list of parameters now

937.759

remember these could be

938.88

i could make this 0 and 1 just to be

940.959

specific

942.079

or i can just assume it's all going to

943.759

fall into place

946.72

so if i'm going to do that then one is

948.72

going to be name and the second one is

950.639

going to be

952.839

weather now i can

955.04

also do

958.88

um let's do parms equals

963.839

name comma weather

967.199

and i think this is going to work for me

968.959

as well

972.079

did i call it part i did so now do a

974.48

little inputs

979.04

and it is out of range so we did do

980.88

hello bob but that second one it did not

982.88

take that because it's a tuple

984.48

i'm sorry i think it's this way maybe

987.279

let me make sure i get this right

992.399

no okay so it's taking it oh because

995.68

unfortunately it is taking it as a it

998.72

takes that as a single variable

1001.12

so the format it's going to try to it's

1003.279

trying to format this

1004.399

instead of converting it in there's

1006.399

another way to do that but i forget how

1009.279

so i'm not going to mess with it because

1011.6

this is cleaner anyways

1014.079

so in doing so we can add all kinds of

1017.199

formatting

1018.079

and have like template strings and

1020.16

things of that nature that will help us

1021.839

out quite a bit

1023.36

i think that's a good point to wrap it

1024.64

up for this one

1026.559

and um i guess next time we're probably

1029.52

gonna

1029.839

i think we're gonna move on from this so

1031.6

the next thing we're gonna actually

1032.559

probably tackle

1033.6

there's gonna be some conditional

1034.72

statements so we'll start some ifs and

1036.48

else's and stuff and

1038.48

start playing around a little bit with

1040.079

creating some little you know scriptlet

1041.839

type applications as we're starting to

1043.679

now

1044.72

have a couple of things in place that we

1046.48

can use to uh to make something

1048.319

interesting

1049.84

as always this is updated this will be

1051.44

updated in the github repository and

1054

take a look at

1054.72

the examples there play around with it

1057.28

don't forget take a look at the

1059.84

limitations and issues that exist in

1062.08

floating point numbers

1063.919

in python the accuracy of those numbers

1068.32

and as always go out there and have

1070.24

yourself a great day a great week

1072.08

and we will talk to you next time

1090.4

you