2.5 Standard Input and Formatting
We can write Python applications with only numbers, strings, and lists, but that would get boring pretty quickly. So instead of stopping there, I will introduce you to a simple way to interact with the user by accepting input strings and formatting output.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
2.5 Standard Input and Formatting
At the end of the last lesson I gave you a little bit of a hint that this lesson was gonna be a little bit of a bonus because I want to give you a little bit more functionality that you can add into an application and use just a couple basic data types. And in this particular example, namely numbers and strings, I want to build an application. I wanna build an interactive program or Python application that a user can interact with. So the way we're going to do this is we're going to introduce allowing the user to interact with the standard input. So what does that mean? Well, that means I want to be able to prompt the end user for some data. And I want to receive that data into my application and then I want to do something with it. So let's see a very simple example. So I'm gonna open up Notepad++. And I ultimately want to just create a simple flow of an application here so you can see how this is going to work. Now in order to get a hold of standard input, and prompt the end user and ultimately receive data, we're going to use a very simple built-in function called input. Now input is going to take in a parameter that's going to be a string that you want to display to the end user that's going to tell them hey, give me some data. So in this particular example, I'm going to just enter in a string here that says give me a number like that. And so as you can see here and I'm putting this within a string and I'm using a colon here just to say all right now I'm gonna need that number from you so what's gonna happen when the user actually types in a number and hits enter what happens with this function well its going to return back the value that was given to me. So I'm gonna say that number is going to be equal to the input here. So once I receive that data, I'm just going to originally pass it back so you can see that this works. So I'm going to do a simple print statement, and I'm going to print back, and I'm going to do a very simple concatenation using the print function, and I'm going to show you another version in just a moment. But I'm going to put in here the user entered, and we'll just put another colon here, and we'll use a comma as the separator number. And we'll go ahead and save this. We'll save it into this Python directory and we'll just call this test input. And we'll make sure that this is going to be a Python file. And we'll save that. So now I can open up my shell. And I can now type in Python and the name of this file, which is the test input file. As you can see, I'm prompted immediately with give me a number, so that's where this string is coming into play, and now my cursor is sitting here waiting for a number. So let's just say I entered in 45. Now it's gonna say the user entered in 45, and as you can see, it looks like we have two spaces in here. And the reason because of that is because within my print statement, I did concatenate these things together with a comma, but I entered in a space here. So that's just a little bit of a lesson to bear in mind is that when I am concatenating a number of different parameters together using the print function I don't have to enter in an extra space, it's going to do that for me. So I can go ahead and save that and once again I will run this again, and this time give me a number 22. And as you can see here, the user entered 22. All right, that's pretty good, but now I would like to maybe ask for another number and create a very simple calculator. I want to add two numbers together, so how would I do that? Well we're gonna call this number one, so we'll say give me a number. And now I'm going to do the same thing. I'll say number two is going to be equal to input. And this one's going to be give me another number. And I'm going to get back another number here and then I want to add those two things together and then print the sum. So let's then say that the value here is going to be equal to total, and that's gonna be equal to number1 + number2. And once I have received that we'll say that the sum is total. And we'll save that. So that seems relatively simple, but you're gonna find that we're going to run into a bit of an issue, or at least get something that you wouldn't expect. So once we do that, let's go ahead and run our application again, so give me a number. We'll say 10 and give me another number we'll say 20 and its gonna say the sum is 1,020. Well what exactly just happened here well the problem that we've run into here is the fact that when you run this input function and you get a value back from the user you're actually getting it back as a string. So at this point this number one variable and this number two variable are both strings. So remember in the string lesson, if I use the concatenation or the + symbol between two strings, it's going to concatenate them together. And then assign them here to total. And then it's gonna print out those concatenations. And a concatenation of two strings is just putting them back to back, and then returning that value. So how do we get away from that? How do I get to a point where I'm dealing with numbers here, cuz that's ultimately what I want. Well if you recall it did say that there are actual types built into Python, although you typically don't use them very often. You can absolutely use them for casting values or casting one data type into another. And it just so happens that int is one of those. So I can use int as a function here. And if I were to use int as a function it's going to convert whatever I pass in here. Hopefully at this point this is actually a string into the number representation or the integer representation of that string. So I'll do the same thing on both sides so at this point we would hope that whatever the first value is when it was a string got converted to an integer. Number two was a string gets converted to an integer. Now when we use this plus symbol we're not concatenating, we're actually doing a mathematical operation to add these two integers together and get a total and then output it to the screen. So let's go ahead and save this and see what happens. So I'll clear this out, I'll go ahead and run my application again. So give me a number ten, give me another number 20, and hit Enter. Now we are getting what we would expect, the 30. So that's just one thing to keep in mind when you're getting values back from the end user. We wanna make sure that they are truly the types that we are expecting, and then in a mathematical sense that's gonna become very important. Now let's see how that can cause problems though as we start to play around with different types of mathematical operations. Let's say I'm given two numbers and I want to divide them. So I'm going to divide these and I'm going to say that the answer is it will go ahead and save this. Now we'll come back in here and we will run our application again and this time we'll do 17 and 3, just cuz I know that comes out to be a fairly large number. So this is fine, I get this kind of big number, and that's the correct answer to whatever level of precision we're given by default from a floating point. As you can see, it is rounded here at the end. But what if I don't wanna deal with numbers like that? What if I don't wanna deal with numbers out to ten or 12 decimal places? I only wanna deal with them up to say two, or three, or four, or whatever level of decimal place, how can I do that? Well we can use a little bit of trickery using some formatting to actually get that down to something a little bit more reasonable. So what we'll do in here is instead of using this print this concatenation in my print function, I'm actually going to use something a little bit different. I'm going to use some string formatting. So the way that we're going to handle the formatting here is I do wanna print prints of the answer is, but then I wanna put a value in there of whatever this result is rounded to say two decimal places. So we're going to use a convention here of the open and close curly braces. And we're gonna specify some values in here within the curly braces. But before we do that, I'm gonna get rid of this total and this comma, and what I want to do is I want to format this string. Now one thing we didn't necessarily talk about before is that when in Python when we are using strings, we can use string literals as actual objects or variables. And then refer to them some of the functions of the string type on them. So one of the functions on the string type is the .format function. So you can see here, I'm dealing with this string literal as an actual string, and I'm gonna call the format function on this particular string. So it doesn't necessarily have to be a variable representing the string. I can simply use the dot notation, and then call a function on this particular string. So that's a nice, little convenience thing that you can use. And so what do we actually want to be formatting? Well we want to format the total value. So now we're going to format this first parameter that we're passing into the format function. Now the order in which we put things into the parameters, or the arguments to the format function, becomes very important. Now in this case there's only one, so it's not really a big deal. But for completeness it's gonna be very important that you remember it's index, because once we come into these little curly braces, we're gonna want to be as complete as possible. So that when other people read our code, it's gonna become very clear as to what it is we were doing. So when we are doing the formatting in this way, we're going to give a number or an index to what it is, to which value in the parameter list of the format function we want to be referring to. So in this case it's zero, and then we're gonna specify an optional and then we can pass into this the value of how we want to format this. And as I said before, we want to format this out to two decimal places. Well, the way that we do that is by specifying a decimal, two, and we want to specify that this is truly a floating point value. So we want to specify the letter F to be very explicit that this is going to be a floating point number. So just one more time as a refresher. We are going to work with this string here the answer is, and then sum value. And then to do this sort of formatting magic we're going to use the open and close curly braces. And then, within there, we're going to specify the index of the value within the format function. So zero index is going to be the first which is total. And then we can specify a colon. And on the right side of the colon we can specify how we want to format it. And this is gonna say we want to take this to two decimal places. So we are going to specify a decimal and two, and this is a floating point number. So once I've done that I can now save this, and I can come back to my shell, and I can rerun my application. And this time I'm gonna say 17 for the first number three for the second number. And as you can see here, we have now been rounded to two decimal places. So just having learned a little bit about the input function to actually get some input from the user. And then a little bit of casting magic by using the integer function here and then a little bit of string formatting really allows us to get to a point where we can start to make something a little bit more meaningful that the user is going to be able to use. So now that we have these tricks in our tool belt let's go into the next lesson we're actually going to build a very simple application.