0% found this document useful (0 votes)
20 views5 pages

PYTHON100-MOD8-USER INPUT-V2-en

Py8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

PYTHON100-MOD8-USER INPUT-V2-en

Py8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

- [Catherine] Welcome to Python Fundamentals.

This is module eight, user input.


So, so far, what we've been doing
when we've been writing our code is
putting the variable values in the code, right in the code.
And in the real world it really doesn't work
like that too much.
We typically read values from a file
or we get values from a sensor
or we ask the user, you know, what's your name?
What's your age, right?
So typically those kinds
of things are things that we input from somewhere else.
So in this module, we're gonna learn how to ask users
for input into our program.
So we're gonna do user input.
Okay, so let's get to see how we're gonna ask
for user input.
So we're gonna use the python's input function.
We're gonna use something else we've learned, which,
of course, is the assignment sign to put what the user types
in into a variable.
We're also gonna learn this module how
to do some fancier output with concatenation,
and that's done with the plus sign.
And finally, at the end,
I'll give you a little note about dynamic typing in Python.
So let's go into Anaconda,
get a new Jupyter notebook up, and write the code.
I'm using a new Jupyter notebook
for each one of the modules.
You don't have to do that.
You can actually have all the code in one notebook.
I just find it a little bit easier to chunk things
into separate notebooks, but you certainly could have it all
in one notebook if you like.
So let's go into Jupyter Notebook.
I'm gonna start a new notebook.
Let's see, this is module eight.
So I am going to rename the notebook file, rename.
I'm gonna call it module eight and rename it.
So the input function is how we get something from the user.
So the first thing we need to recognize though,
which I said a couple modules ago,
is if you want to do something
with a value, you have to store it in a variable.
So we have to have a variable name to get the user input
and put the user input in a variable.
So I'm going to create a variable, I'm gonna call it name,
which is not very exciting,
but that's what I'm asking the user for.
So I'm gonna call it name and I'm gonna do the equal sign.
So remember the equal sign and Python is assignment, right?
So what Python does is it takes everything
on the right hand side, it does whatever needs to do
with everything on the right hand side.
And what's resulting from that whole right hand side is put
in a name.
So if I do something very simple,
like I say name equals Mickey,
Python doesn't have to do a whole lot.
It just takes, Mickey creates a variable
in computer memory called name,
and it just puts the value of Mickey in there, right?
That's what we learned before.
That's the easiest.
But now we're gonna do something really different instead
of coding the name right in the code.
And that's called hard coding.
When you put the values right in the code,
it's called hard coding.
And in some cases that's allowed
and exactly what we wanna do.
But in this case,
we wanna ask the user.
Users all have different names,
so we can't hard code and assume the user's name.
So instead we wanna ask them.
So what we're gonna do is
we're gonna use the input function.
So instead on the right hand side
of the equal sign, I'm gonna say input.
And then I have two parentheses.
Now, you know, you probably have noticed that there's a lot
of Python functions that have parentheses,
very common in computer programming.
Sometimes there's stuff
inside the parentheses and sometimes there's not.
And the syntax rules for that are just something
that you have to learn over time.
The input function does have something
inside the parentheses.
What goes in the parentheses is the prompt
that you want to give the user, right?
So obviously my user prompt is going to be something
like what's your name, right?
So here what I'm doing is telling the computer
to display that string, what's your name,
to wait for the user to type something in,
and after they type it in to put it in the variable name.
So if you look at this code,
it's actually pretty complicated, right?
So we have this string here that I'm doing
in blue that's asking the user what's their name.
That's the prompt that's gonna be displayed.
And I have a function here called input.
Input is the python function that displays that prompt,
takes what the user types in, and then puts it
in the variable name, okay?
So let's, let's run it and see what happens.
Okay, so this looks a little bit different, right,
than we've seen before.
We see that python's displayed the prompt.
It's got a star here, in where we normally see the number
of the code snippet.
The star means that the python's not done executing.
Why is it not done?
It's not done
'cause I didn't type anything in in response to the prompt.
So right now, Python's waiting on me, the user,
and python will com patiently wait forever, right?
So I'm gonna type in Minnie Mouse over here
and I'm gonna hit the enter button to enter it.
And now we see the code snippet number has changed to one
'cause the code is done executing and there was no errors.
Now let's see if Minnie Mouse is gone
into the name variable.
So I'm gonna do name, and indeed,
Minnie Mouse is there.
So actually, if we were to go and look in computer memory
this is what it would look like.
We would have a variable called name.
And what's in there right now is Minnie Mouse.
After that code snippet is run,
we have a variable called name,
and what's in there is the string Minnie Mouse,
including the period and everything.
So we go back to our code, get rid of the clear ink.
Now we know what the user's name is
and we can do some fun things with it.
We can print it out.
That's not that exciting,
but maybe we could make the printout
a little bit more exciting.
We could talk to the user a bit now that we know their name,
right?
So let's do that.
So the print function here,
inside the parentheses of the print function,
we can put more than just the name of a variable.
We can actually put strings as well.
So we could say print, hello.
So I can print out hello.
But let me show you how to combine those two things.
Let's print out hello and the user's name.
Let's do that.
So we want to do something called concatenation.
Concatenation is putting two things together.
In this case they're two strings, right?
It's the hello and the user's name.
So I just need to put a plus sign in between them.
So let me show you that code.
So I'm gonna do print, I'm gonna do hello,
and then I'm going to concatenate name.
So, let's pick apart this code
and make sure we understand what's going on.
The print function takes everything inside the parentheses
and outputs it in the output area under the code cell.
So the concatenation,
the plus sign, is gonna take the string, hello,
and what's inside name.
So just to take a peek, we know what's inside name.
It's right here, Minnie Mouse.
So it's gonna concatenate, hello,
and it's going to take Minnie Mouse
and it's going to to put them together in the output.
So the plus sign,
the plus sign in Python has two different functions.
It can add two numbers together
or it can concatenate two things together.
So you might be saying,
well, how does Python know if I wanna add
or I wanna concatenate?
Python looks at what the two things are.
If they're both numeric, it does addition.
If one of them is a string, it does concatenation.
Let's run our code and see what happens.
Okay, let me get rid
of this drawing and here's our concatenation.
Now it doesn't look pretty.
Why is hello smashed up next to Minnie?
It's smashed up because there is no space
in the string, hello.
If you look at it, there's no space after O.
We get right, go right to the quotation marks.
And if you look at Minnie Mouse,
there's no space in front of the M.
It starts immediately after the quotation marks.
That means when we concatenate them,
they're gonna be smashed together.
How could we fix that?
We can add a space after the O and hello.
Hello Minnie Mouse.
So now we have hello Minnie Mouse.
We can even make it even more user friendly
by putting some punctuation at the end.
Maybe an exclamation point.
Now we have hello Minnie Mouse.
We could do both lines of code together
in the same cell if we want, together in the same cell.
So I'm going to take this input code up here at the top
and copy it, and I'm gonna put it down here
in snippet seven.
So now I'm gonna run two lines of code in the same cell.
So this is important for future modules.
You can put many lines of code in a cell and what happens
in Python, unless you have loops or you,
unless you tell Python to go in a different order,
Python will always go from the first line of code,
the second, the third, the fourth in order.
That's the order of execution unless you write code to go
in a different order.
So what I have here in code snippet seven,
first, Python's gonna ask me for my name,
then it's going to print out hello with that name.
So let me run it.
Now, you see it didn't print out hello yet
because it's still working online,
the first line asking me my name.
So I'm gonna put in Donald Duck, hit enter,
and then it's going to say, hello Donald Duck.
So you can see that I got two lines of output
from running those two lines of code.
If I run it again
with a totally different name, what's your name?
Mickey Mouse.
Oops, I have to hit enter.
Then it'll say hello, Mickey Mouse.
The final thing that I wanted to go
over in this module is a note about dynamic types.
So we've talked about four basic types in Python.
Remember, there's integers, floating points, boolean,
and string.
So the way that Python knows what type your variable is
is by what you, the value you assign to the variable.
So for example,
going back to the code,
Python believes name is a string type
because I'm putting a string in name.
But watch this, down here in the last code snippet,
I could say name equals 156.7.
And now, if I tell Python to print out name,
it prints out 156.7.
You might be saying, wait a minute,
I thought name was Mickey Mouse,
and it was back in code snippet nine, but I changed it.
This is where Python's extremely flexible,
but a little bit dangerous.
So if you reuse a variable name, it'll get a new type,
and a new value, completely legal in Python,
but a little dangerous
because you might accidentally do that
and then think name has Mickey Mouse in it
and now it's got a number
and what's going on in your program.
So when you name variables,
be careful that you're not reusing names
and accidentally taint changing their types.
So I want to be clear here, there's a difference
between putting different values in a variable,
which is pretty much what you wanna do in programming
and fine if you declare an integer variable
and you put different integers in
as you're calculating over the course of programming.
But changing a variable
to a different type is a little dangerous to do.
Okay, see you in the next module.
(exuberant music)

You might also like