Full transcript
0:00hello everyone and welcome back
0:02in a recent video i went over how to get
0:05started working with python we covered
0:08how to run a python file how to use
0:11comments how to use variables how to use
0:15operators
0:16and now
0:18as the next video in this intro to
0:19python sequence i want to cover
0:22what's called io so i'm going to make
0:25a new
0:27category of comments and codes in this
0:30main.py file we've been working in
0:32called io or
0:34input output
0:36let's start with output
0:38output is the goal or the action of
0:41trying to write something to some form
0:43of output something outside of our
0:45program
0:46and what we've been writing to has been
0:48our console or our terminal output
0:52we've been doing this using print so for
0:55example line 59 we print five mod two
0:59two goes into five evenly two times with
1:01remainder one so our output from line 59
1:04went to our terminal console and it
1:06displayed the character one
1:09so let's expand our knowledge of working
1:13with print to include customizing our
1:16print statements and then also how we
1:19can format floating point numbers such
1:22as rounding them to two decimal places
1:24because this is a common action that
1:26you'll want to do
1:29all right so let's start with some more
1:31on print
1:33you can see on my screen here a little
1:35overlay showing a preview of the
1:37documentation for print
1:40so you can see that print accepts
1:43values that are written to a stream or
1:46to sys dot standard out by default
1:49so sysdot standard out is our console
1:52output and that's why we're seeing
1:54everything we pass into this print
1:55function going to our terminal output
1:58there are also optional keyword
2:00arguments file sep end and flush
2:05all right
2:06so let's start
2:08by seeing how we can pass multiple
2:10values to print
2:12and how those are printed and then we'll
2:15start looking at this sep which is sort
2:18for short for separator
2:20a string inserted between values by
2:22default it's a space
2:24all right so let's say i'm going to
2:26print out
2:27a b
2:28c
2:29so i can pass in multiple values to the
2:32print statement we don't have to pass in
2:34just one value such as the result of 5
2:37mod 2.
2:40so by default we're getting a space
2:42inserted between each value that we're
2:45separating by a comma
2:47as we pass them in as arguments to this
2:50print function
2:51so according to the documentation
2:53that's because sep which like i said is
2:56short for separator is a string inserted
2:58between values which is default to space
3:01okay well what if i don't want to have a
3:04space in between my arguments to print
3:07when it is printed to standard output
3:11so i'm going to add to the end of my
3:13print statement another comma for
3:16another argument to print and this one's
3:18going to be a keyword argument which
3:20means i have to put the name of the
3:23argument which is sep and then an
3:25assignment operator and then the string
3:27value i want assigned to be sep
3:31so i'm going to run this again
3:33and now there are no spaces in between
3:36abc
3:38so let's keep our original print
3:40statement so we can compare it
3:47so here by default a space b space c and
3:51then overriding that default space
3:53behavior going in between each one of
3:55these arguments we're passing to print
3:57to be the empty string which is no space
4:00they're all jammed right next to each
4:01other
4:03let's do one more demo of step let's say
4:05i put in three stars
4:08now there will be three stars separating
4:10our arguments to print
4:12and remember we can put in as many
4:14arguments as we want i could do 1 2 3
4:18x
4:21x is a variable we were declaring
4:23earlier
4:24and then maybe z
4:26so these will all be separated by
4:28whatever string i save they should be
4:30separated by
4:35all right let's do another demo
4:38this time let's take a look at the end
4:41keyword
4:43by default the end keyword is a new line
4:47so let's take a look at the
4:48documentation it says
4:50end is the string appended after the
4:52last value default a new line that's why
4:55every time we have a print statement
4:56like line 51 53 55 57 59 60 through 62
5:02through 65 they're all showing up on
5:04their own lines in our terminal output
5:08but let's say i don't want them to show
5:11up on their own lines i want say
5:13multiple line output
5:15to span multiple print statements
5:18so let's try this
5:28so i've got two print statements
5:31this first one on line 65 overrides the
5:34default new line character to be the
5:36empty string so pretty much saying hey
5:38print don't append any character to the
5:42end of my values
5:44so here's the output from line 65. since
5:47there's no new line at the end of line
5:4965's output
5:50line 66 output xyz is just jammed right
5:54on the end
5:56now i didn't override the end behavior
5:59for line 66 print so there is a new line
6:02character appended to the end of x space
6:06y space z
6:07just like default
6:14all right now let's talk about how we
6:16can format
6:18decimal numbers say by rounding them to
6:20two decimal places
6:23let's use math.pi
6:25i imported the math module in a previous
6:28video up here
6:29import math
6:32if i print out math.pi i'm going to see
6:353.14159 and a lot of digits so this is a
6:38great floating point number to work with
6:40because it has so many digits and in
6:42many cases i don't want this many digits
6:44to display
6:46so let's see how we can round
6:49pi to two decimal places
6:53and there are actually a few ways to do
6:55this
7:01let's start with the
7:02pythonic way
7:04and the word pythonic just means like
7:06the python approach or how to do this in
7:08python style
7:11so we'll have a print statement
7:14where we'll have a string and we'll call
7:18dot format on the string
7:21and we'll pass in to
7:23this function or aka method
7:26some arguments that should be inserted
7:28into our string so that means we need
7:30placeholders for where the string should
7:32go
7:34so in my string i'm going to have a set
7:37of curly brackets and this will be a
7:39placeholder for
7:40a value to be inserted into so the value
7:44i want to insert is math.pi
7:48so math.pi is going to go right in here
7:51and now i'm going to provide some
7:52additional information
7:54to format about how i want this floating
7:57point number to be formatted as a string
8:00so i'll have a colon and then point two
8:05so the point two is going to specify two
8:07decimal places
8:08to the right of the point and then f
8:11saying this is going to be a placeholder
8:13for a floating point number
8:16now i run this and i get 3.14
8:22let's see another way to do this
8:27this is an old school c style way of
8:29doing it
8:30but you'll see it around a lot so let me
8:32show you
8:34very similar to what we're doing with
8:35placeholders but this time instead of
8:37curly brackets we're going to use a
8:39percent sign for a placeholder and then
8:42point to f and instead of dot format we
8:45will have percent
8:47open print close friend and in here
8:49we'll put our list of values to be
8:51inserted in our placeholders
8:54in our format string
8:56now we're only going to have one
8:57placeholder so only have one value to be
8:59inserted which will be math.pi
9:06and the last way i'll show you
9:08is to use
9:10the built-in
9:12round function
9:14the built-in round function actually
9:16does round the floating point number and
9:20essentially create a new floating point
9:21number for which you could print it or
9:24assign it to a variable or write it to a
9:26file
9:28this is unlike the previous two
9:29approaches which don't actually round
9:31the number and produce a new number
9:34these previous approaches produce a
9:35string
9:37so let's print round
9:39math dot pi
9:41if i don't pass in a second argument
9:43this is going to round a floating point
9:45number to the nearest integer
9:47but if i want to round this to say two
9:49decimal places then i'll pass in a
9:51second argument to round and now i'll
9:54get 3.14 just like my other two
9:57approaches
10:00now round is a built-in function
10:02we've actually seen a few other built-in
10:04functions so far
10:06print for example is a built-in function
10:09and type is also a built-in function
10:13now i'll mention that pow is not a
10:16built-in function we needed to import
10:19the math module in order to use it
10:22pi is not a function because we didn't
10:24have an open front close paren in order
10:26to invoke it it's not code
10:29so to speak
10:30it's a variable it's a value so we did
10:33need to import the math module to access
10:35this variable pi and you can see here
10:38it's a floating point number
10:43so this has been some more details on
10:46the o of i o
10:48output
10:49now let's do some examples of the i
10:53input
10:55so let's have a section here on
10:57getting input
10:59from the user
11:01right now our program is very one-sided
11:04our program can generate output for the
11:06user to view or consume or look at but
11:11we have no way of getting any data
11:15from the user right so we want to kind
11:17of make a bidirectional interactive
11:19program we can display some in the user
11:21we can get something from the user we
11:22can do something with that display
11:23something else to the user
11:25let's see how we can do that in python
11:29so let's say we want to
11:31get the user's favorite number
11:34the first thing we should do is put
11:35what's called a prompt in our code where
11:38we tell the user what we want from them
11:41so we can use a simple print statement
11:43for this
11:44so i'll say something like please enter
11:47your favorite
11:51number
11:54so when this runs the user will see this
11:57please enter your favorite number
11:59now what we want to happen is
12:01essentially a cursor
12:03kind of blinking at the keyboard waiting
12:06for the user to enter in a value
12:11so in order to get a value we'll use the
12:13built-in function input
12:15you can see the documentation here says
12:18read a string from standard input the
12:21trailing new line is stripped
12:23okay
12:25what does that mean
12:26well let's try and run it
12:30so it looks like our program
12:32waits on line 80 for some input from the
12:35user from standard in which is tied to
12:37the keyboard
12:38okay so let's say the user's favorite
12:41number is five
12:42so
12:43they type five to denote that they're
12:46done typing they're done
12:48entering their answer
12:49they press enter on the keyboard so
12:52enter
12:53is essentially
12:55tied to the new line character which is
12:58inserted at the end of the string so
13:00five newline character
13:02but
13:03input as the documentation stated is
13:06going to strip that out for us
13:08so we'll just get five
13:11so right now on line 80 we're not doing
13:13anything with the five that is returned
13:16by input
13:17so let's store it in a variable called
13:20favorite number
13:23so now i'm going to print out back to
13:26the user
13:27your favorite number
13:29is just to confirm
13:32that i can in fact get in store their
13:35favorite number
13:37so i run this i say my favorite number
13:39is
13:40five and then it echoes back out your
13:41favorite number is five
13:44so far so good
13:45but let's see what happens when we say
13:48try to do some arithmetic with favorite
13:51number we want to do something with it
13:53so let's do your favorite number
13:56doubled
13:58is and then i will do two times favorite
14:01number
14:02so if i enter in five as my favorite
14:05number
14:06two times five is ten
14:08i should see your favorite number
14:09doubled is ten
14:12so i run this
14:13and i say five is my favorite number but
14:16it actually says my favorite number
14:18doubled is 55.
14:20so this is really strange right
14:22let me explain what's happening
14:26input is going to return a string
14:29in a future video we'll talk about
14:31definitions for functions
14:34in python and how you can call a
14:36function all sorts of fun functional
14:38things
14:39but for now
14:41looking at this documentation it tells
14:43us that input is a function
14:45and that this function returns a stir a
14:47string that's what this arrow here means
14:49arrow stir
14:51so
14:52that means that this function is going
14:54to return a string
14:56i'm trying to multiply a string by two
14:58which
14:59is having the effect of just repeating
15:02that string twice
15:04so this is kind of fun when one of the
15:06operands
15:07of the multiplication operator is a
15:09string we don't get the standard
15:12arithmetic multiplication that we're
15:14used to we get what's called
15:17string repetition it becomes the string
15:19repetition operator
15:21let me show you a little demo of this
15:23let's say i do three times hello
15:28this is going to give me hello three
15:31times
15:32kind of interesting kind of fun
15:35so how can i confirm this suspicion that
15:38i'm getting the string repetition
15:40operator here and i'm getting the
15:42character 5 the character 5.
15:46well let's print out the type
15:48of favorite number
15:52and
15:53it does indeed show us that the type of
15:56favorite number is string
15:59okay
16:00well
16:00if i truly do want to do
16:02multiplication right i want to do
16:04arithmetic with favorite number i'm
16:06going gonna have to convert it to a
16:08numeric type such as an int or a double
16:11so that i can get two times five is ten
16:15not two times five the character string
16:19is fifty five five five the character
16:21string
16:23all right
16:24so
16:26let's take a little bit of notes right
16:27here
16:32let's say we really do want
16:38favorite number
16:40to be
16:41a numeric type for example a float or an
16:44int
16:49so what we'll need to do is something
16:51called type conversion where we convert
16:53a value from one type to another for
16:56example we want to convert a string into
16:59an int
17:00thankfully this is pretty
17:02straightforward to do in python because
17:05there's built-in functions that can help
17:07us do the conversion for example if i
17:10want to convert
17:12the value and favorite number to an end
17:14i can just use the built-in function
17:17end
17:18you can see here it says convert a
17:20number or string to an integer
17:23awesome so let's pass in
17:25favorite number
17:27this should return an integer
17:31so i'll store this integer in a variable
17:33called favorite number int
17:36i will print out favorite number int
17:40and i will also print out favorite
17:42number ins type
17:53so i'll enter in five
17:56so now you can see on line 89 we print
17:58out favorite number end it's a 5 it
18:01looks just like it did before
18:04but now the type is an end
18:06not a string
18:08so let's try
18:10multiplying 2 times favorite number
18:13again but this time we'll do favorite
18:15number int
18:19and now we get the output we originally
18:21expected which is 2 times 5 the integer
18:24is 10 the integer
18:30so to recap in this video we dove in a
18:33little bit more into how we can use
18:36print in order to
18:39write
18:40text
18:41and formatted decimal numbers to output
18:44which is our console
18:46we also learned about input how we can
18:48get input from the user using the
18:50built-in function input along the way we
18:53got to learn about type conversion which
18:55is really really really important
18:58now this example worked really well
19:00because the user entered in
19:02five which can be converted from the
19:06string five storing the character five
19:08to an integer
19:10but what if the user entered in
19:12something like hello
19:16so let's say my favorite number is hello
19:19oh no we get some kind of garbage output
19:21here it says traceback most recent call
19:24last
19:26main.py line 88 this is line 88.
19:30it says value error invalid literal for
19:33ins with base 10 hello
19:35essentially int is throwing what's
19:38called an error an exception
19:41saying that hello cannot be converted to
19:43an end
19:45so
19:46we're living in what's called happy user
19:47input land right now where
19:50we assume that all of the input we're
19:51going to get from the user is good and
19:53therefore our code is happy
19:56but as you start to learn more about
19:58python specifically what are called
20:00exceptions
20:01you'll see that you can write robust
20:04code that can handle cases like this
20:06where you don't actually get good input
20:09from the user
20:10so stay tuned for a future video to
20:12learn about exceptions and handling them
20:15in python but for now you can assume
20:18that the input you're going to get from
20:19the user is the type you want
20:22until you learn more and can write
20:23robust code to handle those kinds of
20:26cases
20:27thanks for watching and please subscribe
20:29to my channel and check out some future
20:32python videos