<jupyter_start><jupyter_text>Chapter 5: Lists, Tuples, and Sets[Source Text Book: The Quick Python Book 3rd Edition by <NAME>, Manning Publications, 2018.](https://www.amazon.com/gp/product/1617294039/ref=dbs_a_def_rwt_bibl_vppi_i0)This Jupyter Notebook contains only the quick notes and code excerpted from the above Naomi Ceder's book, __The Quick Python Book__, for the ease of presentation of teaching. Please follow the above link to purchase a copy of the book while taking this course.Chapters 5, 6, and 7 describe the five powerful built-in Python data types: lists, tuples, sets, strings, and dictionaries.  Lists* List is an ordered collection of objects.* A list automatically grows or shrinks as needed.* A list can contain different types of elements.<jupyter_code>[]
[1]
x = [1, 2, 3]
print(x)
x = [2, "two", [1, 2, 3]]
print(x)
len(x)<jupyter_output><empty_output><jupyter_text>List indices<jupyter_code>x = ["first", "second", "third", "fourth"]
print(x)
x[0]
x[2]
# If indices are negative numbers, they indicate position counting from the end of the list.
x[-1]
x[-2]<jupyter_output><empty_output><jupyter_text>Slicing* Extract or assign to an entire sublist at once.<jupyter_code>x[1:-1]
x[0:3]
x[-2:-1]
# Error
x[-1, 2]<jupyter_output><empty_output><jupyter_text>Slicing a list without index1 or index2.<jupyter_code>x[:3]
x[2:]<jupyter_output><empty_output><jupyter_text>Ommiting both indices make a new list that goes from the beginning to the end of the original list.<jupyter_code>y = x[:]
print(str.format("x={0}", x))
print(str.format("y={0}", y))
y[0] = '1 st'
print(y)<jupyter_output><empty_output><jupyter_text>---<jupyter_code>z = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(z)
print(str.format("1st half: {0}", z[0:(len(z)//2)]))
print(str.format("2nd half: {0}", z[(len(z)//2):len(z)]))<jupyter_output><empty_output><jupyter_text>Modifying lists<jupyter_code>x = [1, 2, 3, 4]
print(x)
x[1] = "two"
print(x)
# Appends list to the end of list
x = [1, 2, 3, 4]
print(x)
x[len(x):] = [5, 6, 7]
print(x)
# Appends list to the front of list
x[:0] = [-1, 0]
x
# Removes elements from list
x[1:-1] = []
print(x)
# Appends a single element to a list
x = [1, 2 ,3]
print(x)

x.append("four")
print(x)
# Using the append method to append one list to another.
x = [1, 2, 3, 4]
y = [5, 6, 7]
print(x)
print(y)

x.append(y)
print(x)
# Using the append method to append one list to another.
x = [1, 2, 3, 4]
y = [5, 6, 7]
x.extend(y)
print(x)
# Using insert method to insert new list element
x = [1, 2, 3]
print(x)

x.insert(2, "hello")
print(x)
x.insert(0, "start")
print(x)
x<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15608263198163733, 'fraction_numerical': 0.03940321346595256, 'mean_word_length': 5.393643031784841, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '574636', 'license': 'Apache-2.0', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1027, 'n_words': 331, 'path': 'code/Chapter_05_Lists_Tuples_and_Sets.ipynb', 'repo_name': 'chiachang100/YesYouCanCodeWithPython', 'size': '2619'}