Tuple

A tuple data type is analogous to a vector in mathematics. For example:

tuple = (integer_number, float_number).

Input:

# source_code/appendix_c_python/example05_tuple.py
import math

point_a = (1.2,2.5)
point_b = (5.7,4.8)
#math.sqrt computes the square root of a float number.
#math.pow computes the power of a float number.
segment_length = math.sqrt(
        math.pow(point_a[0] - point_b[0], 2) +
        math.pow(point_a[1] - point_b[1], 2))
print "Let the point A have the coordinates", point_a, "cm."
print "Let the point B have the coordinates", point_b, "cm."
print "Then the length of the line segment AB is", segment_length, "cm."

Output:

$ python example05_tuple.py
Let the point A have the coordinates (1.2, 2.5) cm.
Let the point B have the coordinates (5.7, 4.8) cm.
Then the length of the line segment AB is 5.0537115074 cm.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset