{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A3 – Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Store the following lines in a suitable Python object while maintaining the indentation format:\n", "\n", " Twinkle, twinkle, little star,\n", " How I wonder what you are!\n", " Up above the world so high,\n", " Like a diamond in the sky.\n", " \n", " Twinkle, twinkle, little star,\n", " How I wonder what you are\n", " \n", "Display the object on the screen." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Twinkle, twinkle, little star,\n", " How I wonder what you are!\n", " Up above the world so high,\n", " Like a diamond in the sky.\n", "\n", "Twinkle, twinkle, little star,\n", " How I wonder what you are\n" ] } ], "source": [ "# Use double quotes\n", "poem = \"Twinkle, twinkle, little star,\\n How I wonder what you are!\\n Up above the world so high,\\n Like a diamond in the sky.\\n\\nTwinkle, twinkle, little star,\\n How I wonder what you are\"\n", "# Do not use tabs by the way: tabs are dangerous!\n", "print(poem)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Twinkle, twinkle, little star,\n", " How I wonder what you are!\n", " Up above the world so high,\n", " Like a diamond in the sky.\n", "\n", "Twinkle, twinkle, little star,\n", " How I wonder what you are\n" ] } ], "source": [ "# Make this more readable\n", "poem = (\n", " \"Twinkle, twinkle, little star,\\n\"\n", " \" How I wonder what you are!\\n\"\n", " \" Up above the world so high,\\n\"\n", " \" Like a diamond in the sky.\\n\\n\"\n", " \"Twinkle, twinkle, little star,\\n\"\n", " \" How I wonder what you are\"\n", " )\n", "# Do not use tabs by the way: tabs are dangerous!\n", "print(poem)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Twinkle, twinkle, little star,\n", " How I wonder what you are!\n", " Up above the world so high,\n", " Like a diamond in the sky.\n", " \n", "Twinkle, twinkle, little star,\n", " How I wonder what you are\n" ] } ], "source": [ "# Or use triple double quotes\n", "poem = \"\"\"Twinkle, twinkle, little star,\n", " How I wonder what you are!\n", " Up above the world so high,\n", " Like a diamond in the sky.\n", " \n", "Twinkle, twinkle, little star,\n", " How I wonder what you are\"\"\"\n", "print(poem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Imagine you have a structure file from the RCS Protein Database (PDB) named `\"HSA-glucose_complexe.pdb\"`. Store the file name in a Python variable. Extract the base name of the file and the file extension and store both in separate variables. Now substitute the underscore (`_`) in the file name with a space (`\" \"`). Hint: the file extension corresponds to the four last characters of the the file name." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Filename: HSA-glucose complexe\n", "Extension: .pdb\n" ] } ], "source": [ "file_ = \"HSA-glucose_complexe.pdb\"\n", "fname = file_[:-4]\n", "ext = file_[-4:]\n", "fname = fname.replace(\"_\", \" \")\n", "print(f\"Filename: {fname}\\nExtension: {ext}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an alternative solution you can use the `split()` method of a string.\n", "This method splits the string into substrings and returns a list. We will\n", "learn more about lists in a later exercise." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['HSA-glucose_complexe', 'pdb']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file_.split(\".\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3__ \"A nut for a jar of tuna\" -- could this be a palindrome? How would you check with Python? Remove whitespace and convert all letters to lowercase while you do! What about the words \"lived\" and \"devils\"? Maybe they are if your join them." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "anut fo raj a rof tun A\n", "anutforajaroftuna == anutforajaroftuna\n" ] } ], "source": [ "sentence = \"A nut for a jar of tuna\"\n", "print(sentence[::-1])\n", "print(sentence.replace(\" \", \"\").lower(), \"==\", sentence.replace(\" \", \"\").lower()[::-1])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "devilslived == devilslived\n" ] } ], "source": [ "a = \"devils\"\n", "b = \"lived\"\n", "\n", "print((a + b).replace(\" \", \"\").lower(), \"==\", (a + b).replace(\" \", \"\").lower()[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 1__ Indentation is important in Python as we will see soon. It is recommended to use spaces instead of tabs. Imagine you have text stored as a string, where indentation is done using tabs (escape sequence `\\t`). Use the `expandtabs` string method (find out how it works) to convert tabs into 4 spaces." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lorem ipsum dolor sit amet,\n", "\tconsectetur adipiscing elit,\n", "\tsed do eiusmod tempor incididunt ut labore\n", "et dolore magna aliqua.\n" ] } ], "source": [ "code = (\n", " \"Lorem ipsum dolor sit amet,\\n\"\n", " \"\\tconsectetur adipiscing elit,\\n\"\n", " \"\\tsed do eiusmod tempor incididunt ut labore\\n\"\n", " \"et dolore magna aliqua.\"\n", " )\n", "print(code)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lorem ipsum dolor sit amet,\n", " consectetur adipiscing elit,\n", " sed do eiusmod tempor incididunt ut labore\n", "et dolore magna aliqua.\n" ] } ], "source": [ "print(code.expandtabs(4))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }