How do I make a float in Python?

cjm2671

If I do:

width =  14
height = 6
aspect = width/height

I get the result aspect = 2 rather than 2.33. I'm new to Python and expected it automatically cast this; have I missed something? Do I need to explicitly declare a float?

unutbu

There are many options:

aspect = float(width)/height

or

width = 14.       # <-- The decimal point makes width a float.
height 6
aspect = width/height

or

from __future__ import division   # Place this as the top of the file
width =  14
height = 6
aspect = width/height

In Python2, division of integers returns an integer (or ZeroDivisionError). In Python3, division of integers can return a float. The

from __future__ import division

tells Python2 to make division behave as it would in Python3.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get a float result? (Python)

From Java

How do I make my Custom Control float in Xamarin Forms?

From Dev

How do I make an ImageView float above a LinearLayout?

From Dev

How do I make a floating image float to the top right of a div?

From Dev

How do I make an ImageView float above a LinearLayout?

From Dev

How do I make a python script executable?

From Dev

Numerical Python - how do I make this a ufunc?

From Dev

How do I make an object mutable in python?

From Dev

how do i make a nested dictionary in python?

From Dev

How do i make a game director in python?

From Dev

How do I make a grid in python?

From Dev

How do I make this python app installable?

From Dev

How do I make a timer in python?

From Dev

How do I ONLY round a number/float down in Python?

From Dev

How do I convert float('nan') to binary in python?

From Dev

How do I check if a string starts with a negative number and a float? [python]

From Dev

How do I float: up?

From Dev

How do I float: up?

From Dev

How do I modify a python array of strings to strip any alphanumerics, convert to float, and then store the float in the same array?

From Dev

How do I make two divs in the same "row" float in different directions?

From Dev

How do I make hover enlarge drop shadow float above other pictures?

From Dev

How do I make Python3 the default Python in Geany

From Dev

How to make mpmath function return python float?

From Dev

How do I make the terminal run python 3.1?

From Dev

How do I make a JSON file out of a list in Python?

From Dev

How do I import an external class and make an object out of it in Python?

From Java

How do I make python wait for a pressed key?

From Dev

How Do I Make Private Variables Inaccessable in Python?

From Dev

how do I make a 2.7 python context manager threadsafe

Related Related

  1. 1

    How do I get a float result? (Python)

  2. 2

    How do I make my Custom Control float in Xamarin Forms?

  3. 3

    How do I make an ImageView float above a LinearLayout?

  4. 4

    How do I make a floating image float to the top right of a div?

  5. 5

    How do I make an ImageView float above a LinearLayout?

  6. 6

    How do I make a python script executable?

  7. 7

    Numerical Python - how do I make this a ufunc?

  8. 8

    How do I make an object mutable in python?

  9. 9

    how do i make a nested dictionary in python?

  10. 10

    How do i make a game director in python?

  11. 11

    How do I make a grid in python?

  12. 12

    How do I make this python app installable?

  13. 13

    How do I make a timer in python?

  14. 14

    How do I ONLY round a number/float down in Python?

  15. 15

    How do I convert float('nan') to binary in python?

  16. 16

    How do I check if a string starts with a negative number and a float? [python]

  17. 17

    How do I float: up?

  18. 18

    How do I float: up?

  19. 19

    How do I modify a python array of strings to strip any alphanumerics, convert to float, and then store the float in the same array?

  20. 20

    How do I make two divs in the same "row" float in different directions?

  21. 21

    How do I make hover enlarge drop shadow float above other pictures?

  22. 22

    How do I make Python3 the default Python in Geany

  23. 23

    How to make mpmath function return python float?

  24. 24

    How do I make the terminal run python 3.1?

  25. 25

    How do I make a JSON file out of a list in Python?

  26. 26

    How do I import an external class and make an object out of it in Python?

  27. 27

    How do I make python wait for a pressed key?

  28. 28

    How Do I Make Private Variables Inaccessable in Python?

  29. 29

    how do I make a 2.7 python context manager threadsafe

HotTag

Archive