<jupyter_start><jupyter_text>Lesson 3: Extra material---  Endless loops---With `while` loops, there's always a risk of accidentally creating a conditional that never becomes `False`, leading to an endless loop. Luckily, this can usually be avoided just by checking over your loop logic carefully.Let's look at an example. The following code is an endless loop:    count = 1    while (count <= 10):	    print countOutput:        1    1    1    1    1    ... (never ending...)    Don't try running this code in the notebook -- it will just cause the notebook to freeze up because it never stops running! (Note: if you ever create an endless loop in your Jupyter notebook, click the "interrupt kernel" (square) button at the top to stop it.)So what is going on here? Notice that unlike our other examples so far, we never increment `count` within the loop. Therefore `count` always remains 1, and so the `while` condition is always `True` and we keep looping forever.**Let's see if you can spot an endless loop before it occurs!** For the following examples, guess whether or not the loop will be endless (note that these blocks will **not** be runable to prevent you from freezing up your notebook; see below for the answers).<jupyter_code># example 1

count = 0
while (count < 10):
	print count
	count = count + 1
# example 2

count = 0
while (count > 5):
	print count
	count = count + 1
# example 3

count = 0
while (count != 5):
	print count
	count = count + 1
# example 4

count = 0
while (count != 5):
	print count
count = count + 1
# example 5

count = 0
while (count != 5):
	print count
	count = count + 2<jupyter_output><empty_output><jupyter_text>> Answer Key> 1. No2. No (in fact this won't print anything at all, since the condition "`count > 5`" is never `True`)3. No4. Yes (we never increment `count` within the loop, so it never becomes equal to 5.)5. Yes (since we're incrementing `count` by 2 each time, count takes the values 0, 2, 4, 6, 8, etc. So `count` never equals 5, so the condition "`count != 5`" never becomes `False`, and we keep looping forever.)  More on `range()`---As it turns out, `range()` is a pretty cool function that can create many different kinds of number series. Here's the full definition:**[ Definition ] `range()`****Purpose:** Creates a list with the indicated range of integers from `start` to `end-1`. If only one argument is given, this argument is assumed to be the `end` value and the `start` is assumed to be 0, so a list of integers from 0 to `end-1` is created.**Syntax:**    range(end)    range(start, end, interval)**Notes:**      - Notice that this function does different things depending on how many parameters you give it. This is true of many functions in Python.   - It may seem a little odd that the default is to create the list from 0 to end-1, but since most things in programming start counting at 0, it ends up being more convenient this way. More on this next time.**Examples:**<jupyter_code>print range(5)
print range(1, 6)
print range(0, 11, 2)<jupyter_output><empty_output><jupyter_text>`range()` in actionBelow are several examples of code using `range()`. For each code block, first try to guess what the output will be, and then run the block to see the answer.<jupyter_code>print range(4)
print range(4, 8)
print range(0, 50, 10)
print range(50, 0, -10)<jupyter_output><empty_output><jupyter_text>More on reading and writing files--- Other ways to read filesYou don't have to read a file using a loop. You can also read it manually line by line, or dump all the lines into a list. We won't go over those methods much here, but here is a list of some of the more useful functions that are available:- `.read()` - reads in the entire file at once as a single string- `.readline()` - reads one line at a time- `.readlines()` - reads all lines in file into a list- `.close()` - close the file Example:<jupyter_code>inFile = open("genes.txt", 'r')  

header = inFile.readline()       #read first line of file (often we do this to get rid of a header)
line = inFile.readline()         #read second line of file (repeatedly calling .readline() progresses through the file)
restOfLines = inFile.readlines() #read rest into list (each line will be an element)

inFile.close()                   

print header
print line
print restOfLines<jupyter_output><empty_output><jupyter_text>Reading and writing can be done at the same time (as long as it's to different files!)<jupyter_code>infileName = "genes.txt"
outfileName = "output3.txt"

inFile = open(infileName, 'r')
outFile = open(outfileName, 'w')

for line in inFile:
    line = line.rstrip('\r\n')
    outFile.write("Found " + line + "\n")

outFile.close()
inFile.close()<jupyter_output><empty_output><jupyter_text>This code prints the following to "output3.txt":    Found uc007afd.1    Found uc007aln.1    Found uc007afr.1    Found uc007atn.1    Found uc007bcd.1    Found uc007bmh.1    Found uc007byr.1  The `\t` character---`\t` is the tab character. Much like `\n`, it is invisible in most text editors. You can use it like this:<jupyter_code>print "Hello\tHello\tHello"<jupyter_output><empty_output><jupyter_text>As you'll see in the next lesson and saw in the Unix lessons earlier in the course, we often work with tab-delimited text files, so it's useful to know how to make a tab in your output files.  The += shortcut---In this lesson, we saw a lot of situations like this:<jupyter_code>count = 0

while count < 10:
    count = count + 1
    
print count
string = ""

for i in range(5):
    string = string + "a"
    
print string<jupyter_output><empty_output><jupyter_text>Basically, these are situations where we're adding or concatonating something to an existing variable, and then overwriting the old value of the variable with the new value. We do this so much in programming that there's actually a shorthand for it: `+=`. Here's what the same examples look like using this shortcut:<jupyter_code>count = 0

while count < 10:
    count += 1
    
print count
string = ""

for i in range(5):
    string += "a"
    
print string<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09134220469853786, 'fraction_numerical': 0.01856415311319205, 'mean_word_length': 3.969795918367347, 'pattern_counts': {'":': 1, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '206963', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1749, 'n_words': 880, 'path': '98_Archive/25_Python-III/Lesson3_extra_material.ipynb', 'repo_name': 'bvoight/GCB535', 'size': '6087'}