Input and Output in Python
If you think about it, you will realise that most of the real world programs or softwares that you use, requires some input from the user.
Be it any smartphone application, like Facebook or Instagram where you have to first input your email/username and password to login, then some posts in form of text or photos in order to make it available on your profile. Or, in any ticket booking software where you first have to specify the train/bus/movie name; etc.
Software is a bundle of programs, working together to perform a task or solve a problem, that makes our life easier. And in order to perform any task or solve the problem, the software must know the parameters of the problem(or, task to perform). This is where programs take input from the user. Also, after taking these inputs the program must process them and return some result. This is where output comes into the picture.
In this lesson we will learn how to create a program in python that can take some input from the user and print it back.
Taking Input from user
If you remember we used a code snippet in the first tutorial in order to demonstrate that how short and convenient python syntax can be compared to other programming languages.
We compared python's code to that of C++
and Java
. If you remember, both C++ and Java required some additional header files (similar to modules in python) to get some user input, while in python all it took was just one line.
So lets say, you want to make a simple calculator in python. For that, you would have to take one or maybe two input values from the user, then perform the user requested mathematical operation on the input numbers and return the output. For simplicity, let's just fix the user requested mathematical operation is addition for now.
We can add more operations to our simple calculator later, but let's stick to addition for now. In order to perform addition, you will have to take two numbers as input from the user (much like an ordinary calculator). The inputs must be assigned to or stored in variables (say x
and y
). Now, below is the code, to accept user input, lets see:
>>> x = input()
Write the above code line in the IDLE and just press the Enter key. You will notice that the cursor will not move to the new >>>
line. Rather, it will start waiting for the user input.
This is the time when you enter any number from the keyboard (lets say, 3
). Just enter any number and press the Enter key again and now you will see the newline with >>>
.
And just like that, using the input()
function (yes, it is a built-in function) you just stored a value in the variable x
. In order to make it more fancy, try this:
>>> x = input("Enter the first number:")
Live Example →
Now you will notice that the next line will print the message: "Enter the first number:" and will wait for the user input. As you might have already guessed, printing a message like this will inform the user that they should enter a value/number/input for the program to proceed ahead. A good program should always have such intuitive messages.
Next, ask for the value of y
variable.
>>> y = input("Enter the second number:")
Now, its time to play around a little. Try taking input for variable x
or y
again and this time instead of typing a number try typing any alphabet or any string. As you press the Enter key, you will see python will throw an error.
But in python 3.x raw_input() function is removed and input() is used to take numeri,strings etc all kinds of values.
The Output
Now how can we display the input values on screen? You might think that all we have to do is just type the variable and press the Enter key. Well, it is true that we have been doing this the whole time, but this only works when you are working on IDLE.
While creating real world python programs you have to write statements that outputs the strings, or numbers explicitly.
We use the print
statement to do so. Consider, printing a popular statement "Hello, World". To do so, just type,
>>> print ("Hello, World");
Don't forget the quotes, double or single, both will do just fine. Press the Enter key and you will see the statement being printed at the bottom. If you try to just write "Hello, World" and remove print statement, like,
>>> "Hello, World"
You will see that this will also print the line, however this time with quotes around it. This is how you can differentiate among the two methods. Next, lets try printing a number:
>>> print (9)
Now try, printing two strings together. Create two variables, a
and b
. Assign them two string values,
>>> a = "Hello"
>>> b = "World"
Now print them together.
>>> print (a + b);
HelloWorld
This is how it must appear. This is the example, showcasing how python can interpret one single thing in two ways. Here the +
operator, is used to join two strings instead of performing mathematical addition (which is actually impossible to perform on two strings anyways). Since, the mathematical addition of two strings doesn't make any sense, hence whenever python encounter two strings with a +
operator in between them, it will just join them both.
The above operation can be performed in an another way as well:
>>> print ("Hello" + "World");
HelloWorld
Now try printing a number and string together.
>>> print (5 + " is a number");
You will get an Error. Can you guess why?
This was because we tried to join an integer with a string. So lets establish a rule here that-
"While printing we can't mix up two literals of different types."
Solution? We will have to convert the number 5
into a string. One way can be,
>>> print ("5" + " is a number")
Or,
>>> print (str(5)+ " is a number");
str()
function, is just another built-in function that can be used to convert an integer into a string. It is quite useful when it comes to converting a numeral variable (variable consisting of numbers) into string. Like,
>>> x = 1
>>> print (str(x)+ "st rule of Fight Club we don't talk about Fight Club");
1st rule of Fight Club we don't talk about Fight Club
To conclude, let's finish our calculator with addition function. So, far this is what we have done:
>>> x = input("Enter the first number")
>>> y = input("Enter the second number")
Now, its time for the output.
>>> print (x+y);
Or,
>>> print (str(x+y));
Both will give the same output. Though, result of x+y
will be a number, while the result of str(x+y)
will be a string.