forked from bloominstituteoftechnology/Intro-Python-I
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuples.py
More file actions
33 lines (22 loc) · 792 Bytes
/
Copy pathtuples.py
File metadata and controls
33 lines (22 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Tuples are like lists, but are immutable and are usually used to hold
# heterogenous data. They use parens instead of square brackets.
# Example:
import math
def dist(a, b):
"""Compute the distance between two x,y points."""
x0, y0 = a # Destructuring assignment
x1, y1 = b
return math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
a = (2, 7) # <-- x,y coordinates stored in tuples
b = (-14, 72)
# Prints "Distance is 66.94"
print("Distance is: {:.2f}".format(dist(a, b)))
# Write a function that prints all the values in a tuple
def print_tuple(t):
for i in t:
print(i)
t = (1, 2, 5, 7, 99)
print_tuple(t) # Prints 1 2 5 7 99, one per line
# Declare a tuple of 1 element then print it
u = (1,) # What needs to be added to make this work?
print_tuple(u)