<jupyter_start><jupyter_text>Week 02, Worksheet 1: Data structures (tuples, lists, and "slices")    To use this worksheet, click the Kernel menu and click Restart Kernel and Run All Cells to run the code below to set up the activity at the end.    Otherwise, you can press SHIFT + ENTER in each code cell to run the code to prepare it. `tuple` or not `tuple`: that is the questionLet's jump right in and have a look at a `tuple`:<jupyter_code>integers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
print(integers)<jupyter_output><empty_output><jupyter_text>You're probably reading this worksheet and thinking to yourself "OK, `tuple`s are basically lists. So what gives?" You're right, `tuple`s are like lists. They behave the same way _except_ that `tuple`s can't be modified in the same way. In addition, there are a few small differences in how we might use them:1. there is no `del` function that we can use on a tuple2. we can't `append` anything to them, though through some trickery we can `+` to themSo what are `tuple`s used for? Well, quite a few things that we'll see in the future, and quite a few things that you've already done. For example, any time we pass `arguments` to a `function` or a `method`, we're really using a secret `tuple`.Conveniently, we can test some of our `list` skills with them, and expand our knowledge of _data structures_. So, let's review a bit from our last worksheet. `print` each of the following: 1. The 6th item in the `integers` `tuple`.Hint: this is tricky because I'm asking for the 6th item.<jupyter_code>print(integers[5])<jupyter_output><empty_output><jupyter_text>2. What about the 1st item in the `tuple`?<jupyter_code>print(integers[0])<jupyter_output><empty_output><jupyter_text>3. What's the sum of the 2nd, 3rd, and 9th item in the `tuple`?<jupyter_code>print(integers[1] + integers[2] + integers[8])<jupyter_output><empty_output><jupyter_text>Joining `tuple`sAs referenced above, there is a sneaky way we can _add_ to `tuple`s. Just like the way the `+` operator works with number and `string` types, we can use the operator to _join_ tuples:<jupyter_code>negative_integers = (-9, -8, -7, -6, -5, -4, -3, -2, -1)
integers = negative_integers + integers
print(integers)<jupyter_output><empty_output><jupyter_text>What else so `tuple`s and `list`s have in common?One feature that we didn't talk about in the `list`s worksheet is something referred to as "slicing." This gives us the ability to get parts of a `tuple` or `list`. We're already familiar with `index`es:<jupyter_code># An example of an index
cat_names = ["Ulysses", "<NAME>", "Mr. U", "The Boss"]
print("My cat's name: ", cat_names[0]) # That's his real name, but we call him that the least<jupyter_output><empty_output><jupyter_text>Honestly, I think my cat has forgotten his name. We refer to him by the other three more often. So, let's extract those out into a variable I'm going to call `common_cat_names` using _slicing syntax_:<jupyter_code># Here, [1:] means "index 1 to the end
common_cat_names = cat_names[1:]
print(common_cat_names)<jupyter_output><empty_output><jupyter_text>But, we can slice this any number of ways, including _backwards_:<jupyter_code># The first three
print("First three:", cat_names[:3])

# A slice starting at position one and stopping, not including, position 3
print("Start at one, stop before position 3:", cat_names[1:3])

# Every two
print("Evens:", cat_names[1::2])
print("Odds:", cat_names[0::2])

# Backwards

print("Backwards:", cat_names[::-1])<jupyter_output><empty_output><jupyter_text>Negative indexingThe last one points at something else we can do: _negative indexing_, that is start counting from the _end_ of the `list` or `tuple`.<jupyter_code># Get the last value
print(cat_names[-1])
# Get the second to last value
print(cat_names[-2])<jupyter_output><empty_output><jupyter_text>Test driveLet's perform some operations on our variable `integers`. `print` the result of each of the following operations:   1. A slice starting at position 3 and continuing until position 10.<jupyter_code># TODO<jupyter_output><empty_output><jupyter_text>2. Every even number in `integers`<jupyter_code># TODO<jupyter_output><empty_output><jupyter_text>3. Every odd number in `integers`<jupyter_code># TODO<jupyter_output><empty_output><jupyter_text>4. The entire `tuple` backwards<jupyter_code># TODO<jupyter_output><empty_output><jupyter_text>5. A slice starting at the _last_ position in the `tuple` and showing every 3rd numberHint: Also can be read as "every multiple of 3"<jupyter_code># TODO<jupyter_output><empty_output><jupyter_text>6. Only the _last_ number in the `tuple`.<jupyter_code># TODO<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10205391527599486, 'fraction_numerical': 0.013906718014548566, 'mean_word_length': 5.956845238095238, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '30014', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1506, 'n_words': 599, 'path': 'worksheets/Week 03 - Worksheet 1 - Syntax - Data structures (tuples lists and slices).ipynb', 'repo_name': 'allegheny-college-cmpsc-100-fall-2020/cmpsc-100-fall-2020-week-03-data-structures', 'size': '4680'}