Post thumbnail
PROGRAMMING LANGUAGES

Python-How To Read Input As Numbers?[The Easy Guide]

By Archana

How can I read input as numbers? Is this what is pondering you?

You might be wondering how developers often interact with the users. Well, it is possible with the use of the input function. Most importantly, the prime purpose of using the inputs is to get data from the users or give them. 

Nowadays, several programs have dialog boxes to ask the users to provide similar or distant inputs. In Python 2, there are two different kinds of in-built functions. These functions help in reading input given by the user using keywords.

Below, we have provided all the necessary details to answer your question- how can I read input as numbers. Moreover, we have provided valuable examples that help you understand each concept of the input function.

gif - how to read input as numbers in python

Table of contents


    • What are the two different types of Input in Python 2.x?
    • To print ‘type’ of the input value
    • How can I read input as numbers in Python 3?
    • Does it mean that Python 3 does not identify the input data type?
    • Can I put floating numbers rather than an integer in Python?
    • Can I put multiple integer or string input values in Python?
    • Is it possible to split the string into the list of integers in Python?
    • Conclusion
    • Multiple Choice Questions: Test what you have studied in this blog!

What are the two different types of Input in Python 2.x?

  • input( ): Initially, it will take the information from the user. The next step will evaluate the expression (here, Python automatically identifies the input value as a string or list, or a number). 

That is to say if the user’s input is incorrect, it will either show a syntax error or an exception in Python. For instance–

Output

Enter your name:

123

Hello 123

Output

Enter number:123

123

To print ‘type’ of the input value

Output

type of number <class ‘str’>

type of name <class ‘str’>

  • raw_input ( ): It takes what exactly is typed by the user from the keyboard. It converts to string and then returns the value to the variable as we like to store. For instance –

Output

Enter input to test raw_input() function:0

<type ‘str’>

How can I read input as numbers in Python 3?

To sum up, the key point to remember is a small difference between Python 2 input and Python 3 input. In Python 2, the input() function automatically evaluates the input data and converts it into an integer. 

However, in the case of Python 3, the input() function does not evaluate the user input. This means that Python 2’s raw_input() is equivalent to the Python3 input()_ function.

So, let’s understand it with the help of an example:

Python 2 input() function

This image has an empty alt attribute; its file name is B6Bh7cjxJKXrMjQgKwqPsrXuhFT-nTTFwM00wdP5yqzbNYd19N_liYZtnpOWhqL8aekXTXH0acdt72MIYYn3zccp70gVE8IphivUqlMv40Bo2lQ6MG4T9xa05_b_h44jvyMKZvo

Output

Enter a number: 5 

(5, <type ‘int’>)

Python 2 raw_input() function

Output

Enter a number: 5 

(5, <type ‘str’>)

NOTE: Here, you will notice that the input() function will show the data type like int, whereas the raw_input() shows the same data type as str. That is to say that raw_input does not evaluate the function and read it as it is.

Python 3 input() function

Output

Enter a number: 5 

(5, <type ‘str’>)

NOTE: Remember that the raw_input() function is not available in Python 3.

MDN

Does it mean that Python 3 does not identify the input data type?

No, it doesn’t mean that! As we have already mentioned that the raw_input does not exist in Python 3. So, the older raw_input() is replaced with the input(), and the older input is replaced. 

Do not worry; the input data type can be evaluated by eval(input()) function. However, beware of the use of the eval() function. Let’s take an example of how to use the eval() function.

eval(input()) function

Output

Enter a number: 5 

(5, <type ‘int’>)

Can I put floating numbers rather than an integer in Python?

Yes, you can! In short, to provide the floating values to the Python program, you need to write float instead of int function. Let us understand how the float function works with an easy example:

Output

Enter a number:1.5

(1.5, <type ‘float’>)

Can I put multiple integer or string input values in Python?

Yes, you can do it easily using the split() function. Moreover, the user can put a split() function to divide the input value with ‘,’. Let’s understand it with the help of an example.

Output

#take the input from the user

Enter multiple names 

space-separated:- ‘1 2’

Enter multiple names 

space-separated:- [‘1’, ‘2’]

NOTE: Also, you can convert each input to the integer value using the map and int value function. How? Check the below section.

Is it possible to split the string into the list of integers in Python?

Yes, it is possible with the help of str.split() and map() function. Call str.split() to represent each number. Then use map(func, lis) to create the map objects. Finally, use list(mapping) to change mapping. This is how you can create a map to the list.

Let’s take an example of it:

Output

# split the data and convert each string number to int

‘1 2’

# print the data

Enter multiple numbers:- [1, 2]

Conclusion

Finally, it has been noticed that each program requires data from the user. And, the user can provide the data from the keyboard. But it is always necessary that the program must understand each input data type on its own. That is why users can use input() function in Python 2 and eval(raw_input()) function in Python 3.

Above, we have provided enough examples that help you to understand how can I read input as numbers. If you still have any queries with your Python program, you can comment on it in the comment section. We will try our best to help you with your Python programs. Hope you like the blog; if you find it worthy, don’t forget to share it with the future Python geeks.

Master Python with ease, with GUVI .”

MDN

Multiple Choice Questions: Test what you have studied in this blog! 

1. Anything that you enter as input in Python3, the input() function changes it to a string. 

(A) True 

(B) False

Correct Answer: (A). We have discussed that raw_input() of Python 2 and input() of Python 3 is equivalent to each other. Therefore, it takes all input as a string.

2. Which function is used in Python3 to accept input values from a user? 

(A) raw_input() 

(B) string() 

(C) input() 

(D) rawinput() 

Correct Answer: (C) In Python3, the user uses the input() function to accept the user’s values.

3. What would be the output of the given code : 

print type(type(int)) 

(A) Error 

(B) 0 

(C) type ‘int’ 

(D) type ‘type’ 

Correct Answer: (D) The type() function will give the argument’s class to which the object belongs. Hence, type(int) will give the value of the type ‘type’ object.

4. What is the output of: 

num1 = int(input(“Enter first number “)) 

num2 = int(input(“Enter second number “)) 

res = num1 + num2 

print(“Sum is”, res) 

print (type(res)) 

[input: num1 =5, num2 = 2] 

(A) Sum is 7 <class ‘int’> 

(B) Sum is 7 <class ‘str’>

(C) Error 

(D) res is undefined 

Correct Answer: (A) The addition of 5 +2 (that is num1 + num2) = 7. Moreover, the defined input is of integer type; therefore, the output will be class ‘int’.

5. What is the output of: 

str = input(“Enter multiple names:- “) 

str = str.split() 

print(str) 

[Input: ‘John Emma’] 

(A) Error 

(B) 0 

(C) [‘John’, ‘Emma’] 

(D) Undefined value

Correct Answer: (C) The split function is used to change the string into the list.

Career transition

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Share logo Copy link
Free Webinar
Free Webinar Icon
Free Webinar
Get the latest notifications! 🔔
close
Table of contents Table of contents
Table of contents Articles
Close button

    • What are the two different types of Input in Python 2.x?
    • To print ‘type’ of the input value
    • How can I read input as numbers in Python 3?
    • Does it mean that Python 3 does not identify the input data type?
    • Can I put floating numbers rather than an integer in Python?
    • Can I put multiple integer or string input values in Python?
    • Is it possible to split the string into the list of integers in Python?
    • Conclusion
    • Multiple Choice Questions: Test what you have studied in this blog!