Get User Input
Get User Input
[00:00:08.47] RYAN AHMED: Hello, everyone, and welcome to this lesson on getting user
input. In this lesson, we will learn how to get data from the user using the input function. Input is
a built-in function in Python that is used to get data from the user via the keyboard. The data that
the user will enter will be stored as a string in a specific variable. Please note that by the end of
this lesson and coding lab, we will know how to build a simple Python application that can get
data from the user, store this data in variables, perform math operations on them, and display
data to the user.
[00:00:45.32] Here are the key learning objectives of this lesson. Use Python built-in input
function to get data from the user via the keyboard. Develop a simple application that can take
data from the user. Analyze the data and display a message to the screen. Perform type casting
by changing a variable from one data type to another. So let's head over to our Jupyter notebook
and get started.
[00:01:17.64] All right. So right now we are in the Jupyter notebook titled Get User Input. So so
far we learned how to define variables, how to perform math operations. We also learned about
the various data types that we have available in Python. We also learned how to print a message
to the screen and also print a generalized message as well. So what I wanted to show you right
now is I wanted to show you how we can get data from the user.
[00:01:48.37] So in Python, user input is obtained using the input built-in function. All you need
to do is to say the following. Input, you open parentheses, you specify a message to be displayed
to the user. For example, here I'm asking the user to enter their name. So I'm saying welcome to
the bank, what's your name?
[00:02:11.34] The user is going to use the keyboard to enter data, and this data is going to be
placed in a variable. Here I call it the variable name, but you can go ahead and call it essentially
any name you'd like. And the data, by default, is going to be stored in a string data type. So let
me go ahead and jump into the code and show you that in details.
[00:02:38.46] All right, so let's go ahead and show you the code. Basically what I have here is
I'm going to use the input, open parentheses, and then provide a message to the user. Going to
say welcome to the bank, what's your name? The user is going to use the keyboard to enter their
name, and whatever value the user will enter is going to be stored in the variable name in here.
Again, you can call that x, y, any name you like, or any variable name you like.
[00:03:05.88] If you press Shift and Enter, you will notice something interesting. You will notice
that on the left hand side there is an asterisk here, or a star, indicating that this cell right now is
stuck in a loop. So it's thinking, basically. It is just waiting for the user to enter their name and
press Enter on the keyboard. And you will notice that the message that I added here has been
displayed as part of the output from the code. So here we're going to say, welcome to the bank,
what's your name? And now I need to enter simply my input.
[00:03:41.14] So for example, here I'm going to call, let's say, the name. Let's say Sarah, for
example. And then you press Enter. Once you do that, first you would notice that the star or the
asterisk now is gone, and now I got a number here instead, and that means that this cell has been
executed successfully. And here I wanted to print the content of name, and you will see whatever
the user has entered, which is Sarah, has been stored in the variable name. If you wanted to
check out the data type, if you say type of name and you press Shift and Enter, here we go. You
will get simply the data type is str, which stands for string. OK.
[00:04:22.77] Let's assume that we want to obtain more than one information from the user. For
example, I wanted to get the user name and I also wanted to get the amount they would like to
deposit at a bank, for example. To do that, I'm going to say input, welcome to the bank, please
enter your name. The user is going to insert a value, and that is going to be stored here in name.
[00:04:47.31] And then I'm going to ask the user to enter the amount that they would like to
deposit. So I can say input, enter the amount you'd like to deposit. The user is going to enter a
value, which is the amount. Please note that I'm going to show you how we can convert from a
string data type to another data type, such as integer or floating point in the next cell. But for
now, I'm going to assume the user will insert a value, and that is going to be stored as a string in
the amount variable.
[00:05:18.83] After I get that data from the user, I'm going to print data to the screen. I'm going
to say welcome name to the bank, the amount you would like to deposit or you want to deposit
equals to dollar value and then placeholder or curly braces. If you recall, we covered the print
operation before in the previous lesson, and you just pass along here name. So name will be
placed in the curly braces here, that placeholder, and the amount is going to be placed in the
second one.
[00:05:47.64] So if you press Shift and Enter right now, here we go. Welcome to the bank,
please enter your name. So we're going to say, for example, Sarah. WE press Enter. Enter the
amount you would like to deposit. And here, for example, I'm going to insert, let's say, 5,000.
And then you press Enter. Here we go. You will get, welcome Sarah to the bank. The amount
you want to deposit equals to 5,000.
[00:06:09.14] OK. Finally, I would like to show you an additional example, and I'm going to
assume that you work as a financial analyst at a bank and you have been tasked to develop a
simple application that calculates the dividend per share for a given company. Let's call it XYZ,
Inc. And the company has the following information. We have next year sales revenue is $150
million.
[00:06:34.40] The net profit margin is 10%. So basically, the company makes revenue of 150
million. 10% of that, which is only 15 million, only is going to be used as our net profit. And
then dividend payout ratio is going to be 50%, and the number of outstanding shares is 6 million.
And basically what I'm asking you to do is to calculate the dividend. Do here the calculation for
that. So these are the two formulas we are going to use.
[00:07:09.35] So if you multiply the sales revenue by the net profit margin, that is going to
generate the net earnings. And then if you take that net earnings, multiply times the payout ratio,
and then you divide it by the number of outstanding shares, that is going to give you the
dividends per share. And then I'm going to show you in details the numbers coming up next. So
what I'm going to do first is I want to obtain these variables from the user.
[00:07:35.30] So I'm going to say input, enter sales revenue, and then I'm going to place that data
in sales revenue. Please note that what you notice here is I've added what we called type casting.
Basically I'm going to convert the data type from string, which is a default data type. I'm going to
convert it into a float before I store it in the sales revenue. So please note that this is the input
function, and then I added additional parentheses here.
[00:08:04.81] And then I added the float to cast that input and convert it from string to float and
then place it in the sales_revenue. Same deal as well for the net profit margin, and then I'm going
to place that in net_profit_margin. And then I'm going to get the dividend payout ratio and I'm
going to store it in dividend_payout_ratio. And then finally I'm going to get the number of
outstanding shares, and then I'm going to place it in num_outstanding_shares. And please note
that the number of outstanding shares here could be integer, so I can cast it in an int data type if
you like.
[00:08:39.61] If you press Shift and Enter, here we go. Enter sales revenue. So the sales revenue
according to the input here, the company was making 150 million. So I'm going to say 150% and
then 1, 2, 3, 1, 2, 3. So basically six zeros. And then you press Enter. Enter net profit margin. So
if you go up, the net profit margin is 10%. So I'm going to say 0.1, and then you press Enter.
[00:09:08.95] Enter the dividend payout ratio. So the payout ratio is 50%, so I'm going to see
0.5. Then you press Enter. And then enter the number of outstanding shares, and number of
outstanding shares is 6 million. So I'm going to say 6, 1, 2, 3, 1, 3, 4, and then you press Enter.
And now I have been able to get the data from the user.
[00:09:27.22] Next, what I'm going to do is I'm going to just implement these formulas, and we
learned how to do math operations in the previous couple of lessons. So I'm going to multiply
sales revenue times the net profit margin. That is going to give me the net earnings. And then I'm
going to get the net earnings multiplied by the dividend payout ratio. So basically, out of these
earnings, the company could either pay that in dividends to shareholders or I can take these
earnings and reinvest them in the company.
[00:10:00.49] Here, the company is going to allocate 50% of its net earnings to pay shareholder's
dividends. And then once I get the total amount of paid dividends, now I'm going to take them
and divide them by the number of outstanding shares, which is 6 million, and that is going to
give me the dividend per share. So if you run this, you press Shift-Enter, here we go. Simply you
will get the company's net earnings equals to 150 million. I'm sorry, 15 million.
[00:10:33.03] And then the total amount of paid dividends, that is going to be 7.5 million, and
out of those I'm going to simply divide that by the 6 million that I've got, which is the number of
outstanding shares, and I will end up with the company's dividend per share, and that would be
1.25. So $1.25. And that's simply all I have for this lesson. I hope you enjoyed it. In the next
lesson, we can have our practice opportunity. Please stay tuned, best of luck, and I will see you
in the next lesson.