Featured Post

How To Use Python Input Function

 How To Use Python Input Function



Introduction 

This Python Beginner tutorial will guide you on the usage of input() function in python. We use input function in all our Python Projects, so, it's necessary to understand,  what is input function, how it is used and why it is used in python projects. 


What is input() function 

Python input() function is a function that accepts input values from a user when prompted to provide an input.
It is an inbuilt function that reads the input values from the user and after evaluating it, by default, the input() function will return the input as a string.

The input function doesn't have parameters inside its parenthesis, but, it takes a prompt in the form of a string inside it's parenthesis.

For example:
Consider a variable that you created , name, and you want to get input of different name values from the user, so you use the input function with a prompt inside it's parenthesis as shown below:

CODE
name = input("enter your name: ")

Then , you use write the print() function to show the output of the input received by writing the print function as shown below.

CODE
print(name)

How input function accepts  different data types

By default, whatever value is taken by the input() function it will be converted and stored as a string. So, if we want the function to accept an input as an integer or float, then, we need to take extra measures in order to let the function accept the input as an integer or float respectively. 

Let us consider some examples below:

CODE

age = int(input("enter your age"))
print(age)


In the above code block, we created a variable called age and used the input function to receive, interpret and store users' age values. Note, we wrote the input() function inside the int() function, because we need to write the int() function first in order to convert the default input values which takes inputs as strings to integer values and store the integer values while interpreting the number values as integer values and not strings. Obviously, we cannot have a user's age as string , it needs to be an integer right?

So, what happens with the above code block is, when we use the int() function and then inside the int() function, we write the input() function with the prompt to input the users' age, the input function will now convert the user's age information from string to an integer or rather interpret it as an intger and store the integer values inside the age variable.

This process of converting the default string values of the input values to the required data format, that is, integer, in this case, is actually referred to as 'typecast'. 

If you wish to check the type of data that was received from the input function, you may use the type() function to find out the data type. For example, for checking the data of the input of name values, you can write, 

CODE

print(type(name))
and for checking the type of data for the age values from the input function, you can write,

CODE

print(type(age))


In the above cases, you will find in the console of your code editor that type(name) will yield the type of data as a string and using the type function with the age inputs will yield the type of data as an integer since you already typecast the input value to an integer by using the int() function

In case, you wish to get number values with decimals from the input values , then, you will need to use the float() function in order to convert the default string values to decimal points. 

As you can obsesrve below, we write the float() function first and then inside the float function parameter, we insert the input() function. 

This method will help to interpret, convert and store the default stored string inputs to decimal or float inputs. Amounts could contain decimal points or rather it is very common to have amount spent or amount earned as float , hence the usage of the float() function in the below case. 

If we use the type() function to evaluate the type of data for the float input, we will get the output of the data type as a float type. This is just to check that the typecast using the float() function over the input() function was successful in converting the input to a float value intake.

CODE

amount = float(input("amount spent: "))
print(amount)
print(type(amount))


Advantages of using the input() function

The usage of the input() function permits a smooth and efficient flow of a program since it is impossible to run the program without getting the user inputs especially for this projects/programs that depends on the user generated  input values.

The other advantage of the input() function is the level of interaction with the users because, the usage of the  input function enables the programmer or developer to interact with the user by prompting him or her to provide a specific input value as per the requirement of the program.

Also, for programs that require user generated inputs, it is essential to use the input() function in order to read the various input values provided by the users. 

Lastly, the input() function also enables us to visibly display the users' input on the output screen as shown in the above examples.

Conclusion

We learnt the essentials about using the Python inbuilt input() function, the usage, what it is all about  and some of its advantages as well. 

Now that you have got a clear picture about using the input() function, do not hesitate to use it in your Python program as and when required to do so without having to google it every time.

Related Tutorials:




Comments

Popular Posts

Build A Random Quote Machine in React

Build A Calculator in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Guide to Learn Coding Efficiently and Effectively