<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Strings2&nbsp;&nbsp;Lists3&nbsp;&nbsp;For loops: iterating4&nbsp;&nbsp;Commenting and variable names > All content here is under a Creative Commons Attribution [CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/) and all source code is released under a [BSD-2 clause license](https://en.wikipedia.org/wiki/BSD_licenses).>>Please reuse, remix, revise, and [reshare this content](https://github.com/kgdunn/python-basic-notebooks) in any way, keeping this notice.  Module 2: OverviewWe cover a diverse range of topics: * strings, * lists [also called  *vectors* , if you are used to MATLAB or C or Java]* for-loops.They seem unrelated, but they hang together conceptually: they are all about sequences, or collections: characters in a strings, items in a list, and loops to process the sequence. We will formally compare all sequence types later. For now let us just use them.At the end, and in between these sections we will cover some topics related to commenting. Preparing for this moduleYou should cover these resources (it can take quite some time!)* https://runestone.academy/runestone/static/fopp/Sequences/toctree.html and go through the entire chapter 6. You can interactively code on this website. Please also answer the "Check your understanding" questions as you go.* https://runestone.academy/runestone/static/fopp/Iteration/toctree.html and go through all of chapter 7, skipping section 7.8 unless you are interested in image analysis 😊* https://runestone.academy/runestone/static/fopp/Files/toctree.html and only complete up to section 10.5 (reading a file). We will cover writing to files in a later session.* https://www.w3schools.com/python/python_lists.asp and go through the presented examples on lists.  StringsStrings are some of the simplest objects in Python. In the [prior module](https://yint.org/pybasic01) you created several strings. Now create this string in Python:```pythons = """Secretly under development for the past three years, Bezos said the "Blue Moon" lander, using a powerful new hydrogen-powered engine generating upto 10,000 pounds of thrust, will be capable of landing up to 6.5 metric tons of equipment on the lunar surface."""```Now use the above string to perform the following actions. Look up the Standard library help files for ``strings`` (like we showed last time) to find the methods required.1. Print it to screen completely in upper case.1. Print it to screen but with lower and uppercase characters switched around.1. Try the following: ``print(s * 8)``.1. Try the following: ``print(s + s)``. *Do these two mathematical operations make sense for strings?*1. What is the length of this string?1. How many times does the word "the" appear in the string?1. At which position in the string does the word ``Secretly`` appear? *How does this differ with MATLAB?*1. At which position in the string does the word ``Bezos`` appear?1. Return a boolean ``True`` or ``False`` if the string ``endswith`` a full stop.1. Return the string, replacing the instance of 'hydrogen' with 'nuclear'.1. Replace every space in the above sentence with a newline character, and reprint the sentence to the screen. The above are all effectively done using what are called ***methods***.> A method an *attribute* of an *object*.In the above, a ``string`` is your *object* and objects have one or more attributes.Some tips:1. You can get a **list** [we cover lists next!] of all attributes using the ``dir(...)`` command.```pythons = """Secretly under development for ... the lunar surface."""dir(s)['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__','__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']```* You can ignore all the attributes beginning and ending with a double underscore, for example ``__add__``. The attributes which are of practical use to you are the ones starting from ``capitalize``, all the way to the end.* You don't need to create a string ``s`` first to get a list of the attributes. You can also use this shortcut:```pythondir('')dir(str)```* If you see an attribute that looks interesting, you can request help on it:  ``help(''.startswith)`` or ``help("".startswith)``. Notice the ``''`` in the brackets: it creates an empty string, and then accesses the attribute ``.startswith`` and then asks for help on that. * You will get a piece of help text printed to the screen. This is helpful later on when you are comfortable with Python. In the beginning it is more helpful to search in a search engine, which will give you a page with examples. The built-in Python help is usually very very brief.Use this knowledge know to figure out what the difference is between ``s.find`` and ``s.index``.  Make sense? You can do what is called *slicing* on a string. Slicing is the ability to get sub-parts of a string:```pythonword = 'landing'print(word[1:4])```* How many characters are in the text which is printed on the screen? * Again, for MATLAB users: how does that differ with what you are used to?* What is returned with ``word[3:]``?* What is returned with ``word[3:99]``?* What is returned with ``word[2:6:3]``?* And try this: ``word[6:2:-1]``* And lastly ``word[-4:-7:-1]`` Speaking of DNA ... create this sequence in Python:```pythonseq = """TAGGGGCCTCCAATTCATCCAACACTCTACGCCTTCTCCAAGAGCTAGTAGGGCACCCTGCAGTTGGAAAGGGAACTATTTCGTAGGGCGAGCCCATACCGTCTCTCTTGCGGAAGACTTAACACGATAGGAAGCTGGAATAGTTTCGAACGATGGTTATTAATCCTAATAACGGAACGCTGTCTGGAGGATGAGTGTGACGGAGTGTAACTCGATGAGTTACCCGCTAATCGAACTGGGCGAGAGATCCCAGCGCTGATGCACTCGATCCCGAGGCCTGACCCGACATATCAGCTCAGACTAGAGCGGGGCTGTTGACGTTTGGGGTTGAAAAAATCTATTGTACCAATCGGCTTCAACGTGCTCCACGGCTGGCGCCTGAGGAGGGGCCCACACCGAGGAAGTAGACTGTTGCACGTTGGCGATGGCGGTAGCTAACTAAGTCGCCTGCCACAACAACAGTATCAAAGCCGTATAAAGGGAACATCCACACTTTAGTGAATCGAAGCGCGGCATCAGAATTTCCTTTTGGATACCTGATACAAAGCCCATCGTGGTCCTTAGACTTCGTGCACATACAGCTGCACCGCACGCATGTGGAATTAGAGGCGAAGTACGATTCCTAGACCGACGTACGATACAACTATGTGGATGTGACGAGCTTCTTTTATATGCTTCGCCCGCCGGACCGGCCTCGCGATGGCGTAG"""```* What is the first occurrence of ``GATTAG`` in the sequence?* How many times does ``TTTT`` occur?* Replace all ``A`` entries with ``T``'s and all ``C`` entries with ``G``'s.* Reset the string back again, but now try something a bit more advanced: switch all ``T`` entries to ``A`` and all ``A`` entries to ``T``.  ListsWe will cover creating, adding, accessing and using lists of objects.You have seen this before: create a list with the square bracket characters: ``[`` and ``]``.For example: ``words = ['Mary', 'loved', 'chocolate.']``One of the most useful functions in Python is ``len(...)``. Verify that it returns an integer value of 3. Does it have the **type** you expect? The entries in the list can be mixed types (contrast this to most other programming languages!)    ```pythongroup = ['yeast', 'bacillus', 994, 'aspergillus' ]```An important test is to check if the list contains something:```python'aspergillus' in group499 in group```Like we saw with strings, you can use the ``*`` and ``+`` operators:```pythongroup * 3group + group    might not do what you expect!group - group    oooops```And like strings, you refer to them based on the position counter of 0:```pythongroup[0] but this is also possible:group[-3] however, is this expected?group[4]``` Lists, also have have some methods that you can use. Lists in fact have far fewer methods than strings. Remember how to get a list of methods from the [prior module](https://yint.org/pybasic01)?```pythondir(....)  what do you fill in here?```How many methods do you see which you can apply to a list? Let's try a few of them out:1. Try ``append`` a new entry to the ``group`` list you created above: add the entry "Candida albicans"1. Create a new list ``reptiles = ['crocodile', 'turtle']`` and then try: ``group.extend(reptiles)``.1. Print the list. Remove the ``crocodile`` entry from the list. Print it again to verify it succeeded. 1. Now try to remove the entry again. What happens?1. Use the following command: ``group.reverse()``, and print the ``group`` variable to the screen.1. Now try this instead: ``group = group.reverse()`` and print the ``group`` variable to the screen. What happened this time?1. So you are back to square one: make a new list variable ``group = ['yeast', 'bacillus', 'aspergillus' ]`` and try ``group.sort()``. Notice that ``.sort()``, like the ``.reverse()`` method operate *in-place*: there is no need to assign the output of the action to a new variable. In fact, you cannot.1. Here's something to be aware of: create ``group = ['yeast', 'bacillus', 994, 'aspergillus' ]``; and now try ``group.sort()``. What does the error message tell you? Lists behave like a stack: you can add things to the end using ``.append()`` and you can remove them again with ``.pop()``.Think of a stack of plates: last appended, first removed.Try it:```pythonspecies  = ['chimp', 'bacillus', 'aspergillus']species.append('hoooman')first_out = species.pop()print(first_out)```* What is the length of the list after running this code?* Try adding a new entry ``arachnid`` between ``chimp`` and ``bacillus`` using the ``.insert()`` command. Print the list to verify it. > If you don't know how to use the ``.insert()`` method, but you know if exists, you can type ``help([].insert)`` at the command prompt to get a quick help. Or you can search the web which gives more comprehensive help, with examples.* First use the ``.index()`` function to find the index of "bacillus". Then use the ``.pop()`` method to remove it. In other words, do not directly provide ``.pop()`` the integer index to remove. Assign the popped entry to a new variable.* Overwrite the entry that is currently in the second position with a new value: "neanderthalensis".  For loops: iteratingThe ``for`` loop is used to run a piece of code a certain number of times. The basic structure is shown, with an example that prints the integer values from 3 up to, and including, 8:```python This is one way to do it:for i in range(3, 9):     You can have many lines of code in the for-loop.     As an example, two for-loop statements are shown here.    print(i)    print('-----')    ```Before the command ``print(i)`` is a tab character or 4 spaces. Please use spaces, and not tabs. Especially if you will interact with other colleagues writing code. Therefore the letter ``p`` from ``print`` goes exactly under the ``i``. That ``i`` is the *loop counter*. The ``range(3, 9)`` tells how many times the loop will iterate.Use ``list(range(3, 9))`` to see a list representation of the ``range()`` function. Try creating these ranges:* Every integer from 0, up to and including 12.* Every integer from 0, up to and including 12, in steps of 2* Every integer from 12 down to and including 0, in steps of -3* Use a ``range`` command to create the values ``[-10, -40, -70]``* Values between 0.5, up to and including 9.5, in steps of 0.5Notice how these behave exactly as the string slices seen above. 1. Inside the for loop you can write one or more statements. In the above there are 2 statements and a comment. It is usual to start your comment - if it is required -- with an indent as well. This way it is clear the comment refers to the contents of the for-loop.2. You can call the *loop counter* anything you like, as long as it is a valid variable name. Remember those from [last time](https://yint.org/pybasic01)?You can loop over many types of objects in Python. Try this:```pythonreptiles = ['crocodile', 'turtle', 12.34, 'lizard', 'snake', False]for animal in reptiles:    print('The "animal" object is of type ' + str(type(animal)))```and here you can see *dynamic typing* at its finest: the ``animal`` variable is dynamically changing its type in the loop.You can also iterate over the entries of a string!```pythonsequence =  "TAGGGGCCTCCA"number = 1for base in sequence:    print('Base number {} is {}'.format(number, base))    number += 1```In the above we introduced another concept: that you can print with the ``.format()`` command. We will see more of this later, but then it won't be a surprise. Now that you have seen how you can iterate over the items of a list, let's try to put this to use:1. Print the 3-times table, from 1 up till 12, like you learned in school:> 3 times 1 is 3>> 3 times 2 is 6>> 3 times 3 is 9> ...2. If you haven't done so already, re-write your code to use the ``.format()`` command, as demonstrated above.3. With 1 line of code find at which position in the list the value of 42 appears: ``[0, 3, 9, 12, 27, 35, 42, 50, 66]``4. *Based on a real example that I had to code last week*: find the value in the previous list closest to ``19``. Note: don't worry about short code, or efficiency. Just find the answer. In the real example the list was thousands of entries long and was to find the closest time within $\pm$ 5 minutes. Then you need to worry about efficiency. **Advanced tip:** sometimes you want to iterate through a list, but also know which entry you are iterating on. You can do both simultaneously with the ``enumerate`` command.```pythonnames = ['Leonardo', 'Carl', 'Amiah', 'Yaretzi', 'Destiny', 'Alan']for index, name in enumerate(names):    print('{} is number {} in the list'.format(name, index+1))```What ``enumerate`` does is to create a ``tuple`` with 2 entries. These two entries are dynamically assigned: the first one is an ``integer`` assigned to ``index`` and the second one is assigned to ``name`` in this example. You are free to choose both variable names.Further self-development:1. Rewrite the code above for DNA bases using the ``enumerate`` function, eliminating the manual ``number`` tracking.2. Look up the ``reversed`` keyword, which can be used inside ``enumerate`` to run your for-loop in reverse.  Commenting and variable namesComments are often as important as the code itself. But it takes time to write them.The choice of variable names is related to the topic of comments. In many ways, the syntax of Python makes the code self-documenting, meaning you do not need to add comments at all. But it definitely is assisted by choosing meaningful variable names:>```python>for genome in genome_list:>    ```This quite clearly shows that we are iterating over the all genomes in some iterable (it could be a list, tuple, or set, for example) container variables of sequenced genomes.But here the code structure is identical:>```python>for k in seq:>    ```Later on in the code it might not be clear what ``k`` represents. It is also not clear what ``seq`` is, or contains.    Comments should be added in these places and cases:* At the top of your file: name and date, and a few sentences on the purpose of the code. It is also helpful to note which Python version you use, or expect.* Refer to any publications or internal company reports for algorithms implemented * Refer to a website if you use any interesting/unusual shortcut code or non-obvious code. This is more for yourself, and your future colleagues.  To cover during the interactive session:* Creating code cells in Spyder: `` %% text here (in Spyder)``* The differences between lists and strings: *mutable* and *immutable*.<jupyter_code># IGNORE this. Execute this cell to load the notebook's style sheet.
from IPython.core.display import HTML
css_file = './images/style.css'
HTML(open(css_file, "r").read())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10699869589517481, 'fraction_numerical': 0.010246537912190275, 'mean_word_length': 4.979948013367991, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 10, 'lorem ipsum': 0, 'www.': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '51355', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 4729, 'n_tokens_neox': 4436, 'n_words': 2276, 'path': 'Module-02-interactive.ipynb', 'repo_name': 'kgdunn/python-basic-notebooks', 'size': '16103'}