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 a simple yet powerful function:
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 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.
round()
accepts a number as input and returns the integer closest to it. For example,round(1.2)
returns1
, whileround(1.9)
returns2
.abs()
accepts a number as input and returns its absolute value. For example,abs(-1.2)
returns1.2
.int()
is a bit involved. If an integer enclosed within quotes (string) is entered as input, then the output is that integer. We have already seen this:int('123')
is123
. If a float is entered as input, then the decimal part is thrown away and the integer part is returned. For example,int(1.2)
returns1
andint(-2.5)
returns-2
. Do note that if afloat
is passed in the form of a string, aValueError
will be thrown i.e.,int('2.5')
will result in the following message:pow()
is another useful function.pow(x, y)
returns the value of \(x^{y}\). This performs the same function as the**
operator. In general, the**
operator is faster than thepow
function. But for small numbers, the difference is not perceptible. In fact, using thepow()
function increases readability of code. An extra feature ofpow()
is that it supports a third argument:pow(x, y, z)
returns the value of \(x^{y} \text{ mod } z\). That is, it gives the remainder when \(x^y\) is divided by \(z\).isinstance()
is used to check if an object is of a specified type. For exampleisinstance(3, int)
returns the valueTrue
as the literal3
is of typeint
. The first argument could be any object, not just a literal. For example, ifx
is a variable of typestr
then,isinstance(x, str)
will again returnTrue
.
The Python documentation provides an exhaustive list of built-in functions.