Skip to content

Commit

Permalink
Added directory for demonstrating basics of Python
Browse files Browse the repository at this point in the history
- Also added file zip.py, which demonstrates how to convert between
  sequences and tuples
  • Loading branch information
cfinch committed Jul 2, 2011
1 parent bbe7774 commit e7355f0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Fundamentals/zip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python

# From sequences to a sequence of tuples
a = range(0, 5)
b = range(5, 10)
c = range(10, 15)

sequence_of_tuples = zip(a, b, c)
print("Sequence of tuples:")
print(sequence_of_tuples)

# From a sequence of tuples to multiple sequences
a1, b1, c1 = zip(*sequence_of_tuples)
print("Separate sequences:")
print(a1)
print(b1)
print(c1)

# How it works
def test_fn(*args):
"""Number of arguments is not known in advance"""
print("Arguments passed to function:")
for a in args:
print a

test_fn(*sequence_of_tuples)

# Convert tuples to lists (optional)
a1 = list(a1)
print(type(a1))

0 comments on commit e7355f0

Please sign in to comment.