Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Basic I/O2:26 with Kenneth Love
Sending information out to the user and taking user input.
Variable names have to start with either a letter or an underscore (
_) and can't contain special characters, symbols, spaces, or hyphens (
input() - Function to get information from a user. Always gets a string, even if the content is a number.
Variable - A name tag for a piece of data. More than one variable can point to the same data.
Assignment operator - In Python, this is a single equals sign
=. It assigns the data on the right to the variable name on the left. E.g.
name = "Kenneth".
[SOUND] In our last stage we used the print
function to make Python print stuff out for us.
Let's do it again just to make sure we have it under control.
So we're back in our console inside of the Python interpreter.
And we're gonna use the print function, and we're gonna print, hi from Python.
And we immediately get back, hi from Python.
When I make strings, it doesn't matter if we use single or double quotes.
So we can do print singles quote hi from Python, and we get back the same thing.
We can also ask for input from a user if we want to.
We do this with the input function, so let's give that one a try.
And we say input, and then what we include here is
the prompt, so the, the question we want to ask them.
So let's ask, what do you like to eat?
And we get the prompt, what do we like to eat?
And we can enter in an answer.
I like to eat SPAM.
And we immediately get back the string SPAM.
We should save that somewhere though.
And that somewhere's called a variable.
Variables are really just name tags for different bits of data.
We can use any numbers or letters that we want or an underscore.
But it can't start with a number.
Typically too, variables are always lower case.
Let's call our variable food.
So we do food.
And then a single equal sign, which is our assignment operator.
It says, put whatever in on the right side, in to the name on the left side.
And so we're gonna do input, what do you like to eat?
Again, press Return, again, Python asks us.
We type in SPAM, press Return, and we do not get the string back.
The reason is, the string SPAM now lives inside the food variable.
We can look at food just by typing in the variable name and pressing Return.
Or we can print food [SOUND] and get the
information that it contains and print it out to us.
Taking user input immediately opens up new horizons for
us, because now we can interact with the outside world.
No more dark basements for me.
You need to sign up for Treehouse in order to download course files.Sign up