Transcript for:
Main Concepts of Input and Output in Python

output is data that originates inside of a program and is sent to some destination outside of the program the print function for example sends a string of text output to standard output or the console where the user can see and read it input is data that originates outside of a program and is read into the program the input function in Python can be used to read input from standard input by default this is text that is typed into the console by the user The Prompt that should be displayed to the user is passed as an argument into the function for example input with a left parentheses and a right parentheses and then between those parentheses we have the string enter your age this will print the prompt enter your age to the console and then the program will pause until the user types a response and hits the enter key at the end the input function will return the text that is typed by the user we have already discussed that some functions need input to do their work and the input function is no exception it requires the prompt to be passed in as an argument this prompt should be a string that informs the user about what it is that they're actually supposed to be typing in some functions also produce output values that are returned from the function after it is finished and can be used elsewhere in the program the input function is one such function it returns turns the text entered into standard input by the user let's see how it works from my command line I'm going to run python minus I to start the python interpreter and then I am going to run the input function and I'm going to put enter your age as the prompt and then I'm going to hit the enter key note that the program has now paused at this point what we're used to seeing is the prompt coming up again and asking us to type in another python statement but here it is paused and it's waiting for me to actually type something in once I have typed that value and we can see it print out as a string the single quoted string 49 and then the prompt is waiting for me to enter some more text so what if I actually want to use that 49 elsewhere in the program it's not enough that the input function has returned that value I need to save it somewhere the input function Returns the text that the user typed into standard input in this case the console but what can we do with it we'd like to use the return value in the print function to print it back out to the user but how can we do that it's often the case that we would like to save a value somewhere in memory so that we can use or change that value later a variable is a name for a location in memory in which some useful data has been stored we can use the variable name to access the data without needing to know exactly where it is stored an identifier is used to name something in a Python program valid identifiers need to follow some rules the first character in an identifier must be either an alphabetic letter A to Z uppercase or lowercase or an underscore the rest of the name of the identifier must only contain letters numbers and underscores that means no punctuation or special characters of any kind once a variable name has been used to store a variable in memory the same name can be used to retrieve and use it later before a variable can be used it must be declared meaning given a name and initialized meaning given a value using what is called an assignment statement in the format identifier on the left the equal sign and then the value on the right so here are a couple examples we're creating a variable named myor name and we're setting it equal to the string root Hagrid in the second example we are creating a variable called myor Ag and we are setting its value to the string 60 if a function returns a value the return value can also be used to initialize a variable so here's an example your underscore name equals and then we call the input function and prompt the user to enter their name when the value is returned by the function it will be stored in the memory location associated with the variable your name assigning a value to a variable will store it somewhere in memory after which the variable name can be used to retrieve that value and use it in your program so here we're using the print function with myor name which then retrieves the value that the user entered into the prompt and prints it to the console let's take a look at how this works so using the examples from the slide I'm going to create a couple of variables my name remember a variable or an identifier in Python has to include include only letters numbers and underscores and must start with either a letter or an underscore so I'm going to assign my name to a variable and my age to a second variable now if I type the name of the variable the python interpreter is going to print the value of that variable now let let's say that I want to actually print these values to standard output I can do that using the print function finally let's say that I want to prompt the user to enter some information like we did a few minutes ago but this time I want to save that information in a variable so that I can use it later so remember when we call the input function we have to provide an argument that is the prompt that the user will see that tells them what they need to type so that text has now been saved in the variable called aore variable if I print that variable back out we'll see that it has been retrieved from memory and printed using the print function and argument is a value that is passed into a function for example the text printed to the console by the print function or the prompt printed to the console by the input function an argument may be a literal value for example a quoted string of characters that are typed directly into the function call this is what we did when we wrote the first hello world program we used the print function with a literal string that was just typed directly between the parentheses in the call to the print function and that literal string is what is printed to standard output an argument may also be a variable so I just showed you a couple of examples of that specified using its identifier so for example print your underscore age will retrieve the value of your age from memory and print it to the console some functions may accept multiple arguments separated by commas in these cases you may mix literal values and variables together for example you're calling some function and you give it the literal string 45 that is just typed the variable color another literal string Hagrid that is just typed and then two more variables X and Y we can tell that the variables are variables and the literals are literals because the variables are not enclosed in quotes the print function is one such function any number of arguments may be passed into the function separated by commas they will be printed in order left to right on the same line and separated by spaces for example if I call the print function with a one and fluffy all three of those are literal strings this is going to print the text a space one space fluffy to the console the print function will accept any number of arguments separated by commas so in this example we have two variables my age that's been assigned the value 60 my name which has been assigned the value rubius Hagrid and then we print four different values the literal string test the variable my age the literal string 1 23 and the variable My Name by default the output will be printed separated by spaces so what we will see print to the console is test 60123 rubius Hagrid with no quotes so let's take a look at how that works in the python interpreter picking up where we left off I'm going to call the print function with several different arguments so here I have four different arguments the literal string my name the variable mycore name the literal string my age and the variable mycore AG what gets printed to the console is that literal string my name followed by the value of the variable mycore name follow followed by my age the literal string followed by the value of the mycore AG variable