Home

If you're new to Python
and VPython: Introduction

A VPython tutorial

Pictures of 3D objects

What's new in VPython 6

VPython web site
VPython license
Python web site
Math module (sqrt etc.)
Numpy module (arrays)

 

Floating Division

For versions of Python prior to 3.0, Python performs integer division with truncation, so that 3/4 is 0, not 0.75. This is inconvenient when doing scientific computations, and can lead to hard-to-find bugs in programs. You can write 3./4., which is 0.75 by the rules of "floating-point" division.

You can change the default so that 3/4 is treated as 0.75. Place this at the start of your program:

from __future__ import division

There are two underscores ("_" and "_") before "future" and two after.

Starting with Python 3.0, 3/4 means 0.75 and from __future__ import division is no longer needed and is ignored. You may wish to include this statement, however, so that your program will run correctly on earlier versions of Python.

The Visual module converts integers to floating-point numbers for you when specifying attributes of objects:

    object.pos = (1,2,3) is equivalent to object.pos = (1.,2.,3.)

Another difference between Python 3.0 and earlier versions is that instead of writing print x+y one must now write print(x+y). That is, print is now a function. To make sure that print works properly in all versions of Python, place the following at the start of your program:

from __future__ import division, print_function

This statement is ignored by Python 3.0 and later versions.