Home Lesson-2.3

Lesson-2.2

Input

Accepting input from the user routinely happens in programming. Any piece of software shipped to a customer needs to have a functional interface that will let the user interact with the software. We all have used apps like Facebook, Instagram and Twitter. These apps regularly accept input from the user, though we seldom look at it from a programming perspective. Take the case of commenting on a post in Facebook. The text entered in the comment-box is the input. The code running in the backend processes this input and then displays it as a comment in a visually appealing form.

Python provides a built-in function called input() to accept input from the user. This is simple yet powerful:

Execute the code given above and head to the console. Here the interpreter waits patiently for you to enter text. Press enter after entering the input. This acts as a cue for the interpreter to understand that you have completed entering your input. This text is stored in the variable x. The way it looks in the console is as follows:

Sometimes we may want to prompt the user to enter a particular type of input. This can be done by passing the instruction as an argument to the input function:

Let us now look at the type of the variable x:

Execute the above code with the following input types: int, float, str and bool. What is the output in each case? We see that the input() function always returns a string. Even if the user enters a number, say 123, that is processed as the string '123'. If we want to accept an integer as input, how do we do it? We take the help of an operation called type conversion.

 

Type Conversion

If we want to convert a string into an integer, Python provides a built-in function called int:

The operation in line-3 is called type conversion, i.e., we are converting an object of type str into an object of type int. The inverse operation also works. Predictably, the function needed for this purpose is called str:

If we want to accept an integer input from the user, we first take a string as input and then convert it into an integer:

Instead of writing this in two lines, we could write this in a single line:

What we have done in line-1 is to compose two functions. That is, pass the output of the inner function - input() - as the input of the outer function - int(). In the above code, what happens if the input entered is a float value?

The code will throw a ValueError. Let us take a concrete example. When the command int('1.23') is entered, the interpreter tries to convert the string '1.23' into an integer. But the number enclosed within the quotes is not an int, but a float. This number cannot be converted into an integer, hence the error.

 

Built-in Functions

We have been using the term built-in functions quite often. These are functions that have already been defined. Loosely speaking, a function in Python is an object that accepts inputs and produces outputs. For example, print is a built-in function that accepts an input and prints it to the console.

We will look at few more functions which will come in handy.

The Python documentation provides an exhaustive list of built-in functions.