# How to take float input in Python

In this tutorial, we will see how to take float input in Python

There is difference in how you can take input in python 2.x and 3.x. You have to use `raw_input` in python 2.x and `input` in Python 3.x

In Python 3.x, `raw_input` was renamed to `input` and the Python 2.x input was removed.

💡 Did you know?

`raw_input` always returns a String object and same is the case with `input` in Python 3.x

Let’s see with the help of example.

Output:

Enter a float:23.43
value of x: 23.43
type of x:

if you want to take float as input, then you need to use `float()` function to explicitly convert String to float.

## Python 3.x example

x = float(input(“Enter a float: “))
y = float(input(“Enter a float: “))

Let’s understand with the help of example.

``` x = float(input("Enter an float: ")) y = float(input("Enter an float: ")) print("Sum of x and y:",x + y) print("Multiplication of x and y:",x * y) ```

Output:

Enter an float: 23.43
Enter an float: 34.32
Sum of x and y: 57.75
Multiplication of x and y: 804.1176

## Python 2.x example

p = float(raw_input(“Enter a float: “))
q = float(raw_input(“Enter a float: “))

Let’s understand with the help of example.

``` p = float(raw_input("Enter an float: ")) q = float(raw_input("Enter an float: ")) print("Sum of p and q:",p + q) print("Multiplication of p and q:",p * q) ```

Output:

Enter an float: 10.34
Enter an float: 20.21
Sum of p and q: 30.55
Multiplication of p and q: 208.97140000000002

If user does not provide float input, then it will raise `ValueError: could not convert string to float`.
Let’s see with the help of example:

Enter an float: 23.41
Enter an float: NA
Traceback (most recent call last):
File “main.py”, line 2, in
q = float(input(“Enter an float: “))
ValueError: could not convert string to float: ‘NA’

That’s all about How to take float input in Python.

## Related Posts

• 22 January

### FizzBuzz program in Python

In this post, we will see how to program FizzBuzz in Python. As per wikipedia, Fizz buzz is a group word game for children to teach them about division. Here are the rules of the game: First player starts the game by saying number 1. Next player says next number but fun part is If […]

• 29 December

### [Fixed] no module named ‘sklearn.cross_validation’

Learn about how to fix modulenotfounderror:no module named 'sklearn.cross_validation' in Python

• 29 December

### Reverse array in Python

In this post, we will see a different ways to reverse array in Python. Let’s see the example first. Example: [crayon-60110c9e304ef183935352/] As we know, Python language has not come up with an array data structure. So in python there are some ways to create an array like data structure. Some ways like: use list, use […]

• 21 December

### Print environment variables in Python

Learn about how to print environment variables in Python.

• 21 December

### Matrix multiplication in Python using user input

Learn about how to Matrix multiplication in Python using user input.

• 20 November

### How to initialize array in Python

In this article, we will see a different ways to initialize an array in Python. Array is collection of elements of same type. In Python, we can use Python list to represent an array. Using for loop, range() function and append() method of list Let’s see different ways to initiaze arrays Intialize empty array You […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.