Python Programming For Beginners Lesson 3 - User Input

in #programming7 years ago (edited)


Introduction

Hello Everyone and welcome to the third lesson of a new series about the Python language. I have just finished a series on the Java programming language which can be found here https://steemit.com/programming/@robertlyon/java-programming-for-beginners-course.

The second lesson of Python Programming for Beginners can be found here https://steemit.com/programming/@robertlyon/nv7w5-python-programming-for-beginners-lesson-2-variables

In this lesson, I am going to be covering user input in python. This will allow you to input your own information into your program and store that information in variables.

So let's dive straight in.

User Input

User input is a very important part of most applications. The ability to interact with an application is what makes it useful for the users(not all programs need user interaction). For example, if we were creating a video game, we would need to program the game so that the user could interact with their character and tell them where to go. Another example is typing a document in a text editor such as the one here on Steemit, their needs to be something in place for the user to be able to enter text from their keyboard to it being displayed on the screen.

We will not be looking as in-depth as video games in this tutorial but we will be looking at typing user input from the keyboard and seeing that input being displayed in the Python shell.

Let's have a look at the program below and try to figure out what it does before we go through the technical details.



Figure 1 Code Example


So in the example above we can see two lines of code, you have already seen most of this code, the only new things are the input() function and the + operator.

On line 1 we can see that a variable name "name" has been and we have set that variable to be equal to input("Please input your name: "). The string inside of the parentheses is the prompt, this tells the user what you want them to input, in this case, a name. Once you have entered your name, it will be stored in the variable "name".

On line 2 we can now use this variable to print out a welcome message to the screen. In this message, we are using 2 strings and a string variable all separated by the + operator. The plus operator in this instance adds the strings together. This is known as concatenation e.g we are concatenating the strings.

Hav a look at the 3 images below to see what the output of this program is.

It is good practice to run this program yourself a few times so that you can try out different names or words when you are prompted for a name.



Figure 2 Prompt for input


This shows that the program is waiting for input before it continues running.



Figure 3 Inputting name


I have now input my name and all I need to do now is press the enter key so that the program knows that I am finished with my input.



Figure 4 Finished program


The finished program shows that my input has been accepted and that the variable "name" does, in fact, hold the value "Robert" which is what I typed in as my input.

Conclusion

In this simple lesson, I have covered how to input data to a Python programming and how that data can be stored into a variable and used again at a later date. I also briefly covered the use of the + operator and how it is used to concatenate strings. I will cover operators in more depth in the next lesson where we will build a very simple calculator.

As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.

Thank you for reading and I hope that someone will get some use out of these tutorials.

Message to readers

Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.

Sort:  

Hey Robert, nice tutorial!
Reminds me of computer science university course where you learn such things at the start.

Hopefully, this will help people that are new to Python to get started!
Keep it up!

Thanks for the kind words, always good to see more programmers on here doing tutorials. Keep up the good work :)

You're welcome!
I agree, I'm looking around searching for fellow developers, that's how I stumbled upon you.
I will thanks, you too!

I have one quick question if you don't mind me asking - I haven't used Python in ages, I'm just curious about something. Because Python is dynamically typed language, if user wanted to input some number, you'd need to cast it/convert it to int/float before storing in variable, correct?

I mean, especially if you wanted to do some arithmetic with such stored number later, I'm sure by default Python could store number as a string and display it properly by default.

If so, that might be something you could also add (think name + age), given that strings and numbers are two most commonly used basic data types. But it's nice and short tutorial as is, so that's just one suggestion because you were asking for some

Yes, you are correct, so if you wanted to store an integer in a variable using the input function you would do something like this num = int(input("Enter a number: "))or you could do this, the number will be stored as a string until you wish to use it print(int(num) + 5).

I appreciate the suggestion and I do have number input planned for the next lesson where I will be looking at operators and creating a very simple calculator program. This lesson was kind of the setup for that one so that I don't have to cover too much information in just one lesson.

Thanks for the feedback

Thanks for clarifying that.
Actually your comment is so succinct and nice that you can "pretty much" copy and paste it into your post above, heh.

But yeah if you've planned that already, do like you intended to - I just wanted to give you feedback! You're welcome fellow developer!

This post received a 74% upvote from @morwhale team thanks to @robertlyon! For more information, click here! , TeamMorocco! .
STEEM Price : 3.246 $

Congratulations @robertlyon! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

You got a 5.74% upvote from @upme requested by: @robertlyon.
Send at least 1.5 SBD to @upme with a post link in the memo field to receive upvote next round.
To support our activity, please vote for my master @suggeelson, as a STEEM Witness

This post has received a 7.51 % upvote from @buildawhale thanks to: @robertlyon. Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.

To support our daily curation initiative, please vote on my owner, @themarkymark, as a Steem Witness

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 96907.08
ETH 3380.66
USDT 1.00
SBD 3.23