forked from cfinch/Shocksolution_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added directory for demonstrating basics of Python
- Also added file zip.py, which demonstrates how to convert between sequences and tuples
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |