BYU logo Computer Science


We have previously used integers:

number = 7

A float is a floating point number, or in other words, a number with a decimal point:

number = 7.34

If you want to read a decimal using input(), remember that input() always returns a string. You will need to convert the string to a float:

if __name__ == '__main__':
    response = input('Enter a rating between 1 and 10: ')
    rating = float(response)
    print(f"Your rating: {rating}")

You can also do this in one step:

if __name__ == '__main__':
    rating = float(input('Enter a rating between 1 and 10: '))
    print(f"Your rating: {rating}")


Often when you work with floats you will want to round them:

number = 7.3478
rounded = round(number, 2)

This will round 7.3478 to two decimal places, printing:


You can round to any number of decimal places you want:

number = 7.3478
rounded = round(number, 3)

This will print:


Here is another example:

if __name__ == '__main__':
    apples = 7
    people = 3
    per_person = round(apples / people, 1)
    print(f'Each person gets { per_person } apples')

This prints:

Each person gets 2.3 apples