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.