diff --git a/Exercises/solutions/gap_exercise1_basics.ipynb b/Exercises/solutions/gap_exercise1_basics.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..5c21523c775b17b837f468bf387e53f0a5d1fe46 --- /dev/null +++ b/Exercises/solutions/gap_exercise1_basics.ipynb @@ -0,0 +1,463 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "gaps_exercises1.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Python course - Exercises Gap text" + ], + "metadata": { + "id": "kH0p2uT-sQ6y", + "pycharm": { + "name": "#%% md\n" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "Correct the Code" + ], + "metadata": { + "id": "uW9SoYEyrlRL", + "pycharm": { + "name": "#%% md\n" + } + } + }, + { + "cell_type": "code", + "source": [ + "# What is missing here? Expected output: Hi!\n", + "print('Hi!')" + ], + "metadata": { + "id": "Qflb9AWfrykh", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 5, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hi!\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Here are two mistakes. Can you find them?\n", + "# Hi! I am a comment. How are you?\n", + "print('Hello world!')" + ], + "metadata": { + "id": "eemU0jTksxu5", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 6, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world!\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Multi-line comments\n", + "\"\"\"\n", + "I want to be a\n", + "mutli line comment.\n", + "\"\"\" \n", + "print('Hello world!')" + ], + "metadata": { + "id": "2ysNg9wDtaZ9", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 7, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world!\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Expected output: 5\n", + "c = 5\n", + "print(c)" + ], + "metadata": { + "id": "Vexbuvx1u1NS", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 8, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# How to print two strings as one. Expected output: Hello world\n", + "a = 'Hello '\n", + "b = 'world'\n", + "print( a + b )" + ], + "metadata": { + "id": "cGTJ95-9wzLT", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 9, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Variable types\n", + "# Does the error output help you?\n", + "a = 'Hello'\n", + "b = 0\n", + "print( a + str(b) )" + ], + "metadata": { + "id": "6QiU3l2_xFnP", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 10, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# What is missing?\n", + "a = 'Hello'\n", + "b = a\n", + "\n", + "if a == b:\n", + " print('Same!')" + ], + "metadata": { + "id": "_YtGF9xqzFoR", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 11, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Same!\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Print the variable type\n", + "# Expected output: a is of type <class 'str'>\n", + "a = 'How are you?'\n", + "print('a is of type', type(a))" + ], + "metadata": { + "id": "wH0ifA4czyDV", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 12, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is of type <class 'str'>\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Variable names: What is allowed and what not?\n", + "_1th_string = \"Hello\"\n", + "second_string = 'world'\n", + "third_string = '!'\n", + "FOURTH_STRING = 'How'\n", + "FIFTH_STRING = 'are'\n", + "_6th_string = 'you?'\n", + "\n", + "print(_1th_string, second_string, \n", + " third_string, FOURTH_STRING,\n", + " FIFTH_STRING, _6th_string\n", + " )" + ], + "metadata": { + "id": "Spcu5lGc0Uv9", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 13, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world ! How are you?\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# convert b into float\n", + "a = 2\n", + "b = float(a)\n", + "print(b)" + ], + "metadata": { + "id": "_TcdURMh2yJZ", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 14, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# How to access Lists?\n", + "my_list = [\"!\", \"Hello\", 'you', \"How\", '?', 1, 'are', 2, 3, \"world\"]\n", + "print(my_list)\n", + "print(my_list[ 0 ]) # print '!'\n", + "print(my_list[ 1 ]) # print 'Hello'\n", + "print(my_list[ -1 ]) # print the last element of a list without the real index; here 'world'\n", + "### print 'Hello world! How are you?':\n", + "print(my_list[ 1 ], my_list[ -1 ] ,my_list[0] ,my_list[ 3 ] ,my_list[ -4 ] ,my_list[ 2 ], my_list[4])" + ], + "metadata": { + "id": "5VripGXYxBf8", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 15, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['!', 'Hello', 'you', 'How', '?', 1, 'are', 2, 3, 'world']\n", + "!\n", + "Hello\n", + "world\n", + "Hello world ! How are you ?\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Indices of Strings\n", + "a = 'Hello world!'\n", + "print(a[ -1 ]) # only print !\n", + "print(a[ 0 ]) # only print H\n", + "print(a[ 6:-1 ]) # only print 'world', without '!'" + ], + "metadata": { + "id": "V3VJP2gg1HLT", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 16, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "!\n", + "H\n", + "world\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Dictionaries\n", + "my_dict = { 'chr1' : 'gene1', 'chr2' : 'gene3' }\n", + "print(my_dict[ 'chr1' ]) # print gene1 of the dictionary" + ], + "metadata": { + "id": "6e6KYBcQ0qa3", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 17, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gene1\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Dictionaries2; Find the mistake\n", + "my_dict = { 'chr1' : 'gene1', 'chr2' : 'gene3' }\n", + "print(my_dict[ 'chr1' ]) # print gene1 of the dictionary" + ], + "metadata": { + "id": "44dxJxK2bR2n", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 18, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gene1\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Dictionaries3\n", + "my_dict = { 'chr1' : ['gene1', 'gene2'], 'chr2' : 'gene3' }\n", + "print(my_dict[ 'chr1' ][ 1 ]) # print gene2 of the dictionary" + ], + "metadata": { + "id": "RdIWfnVsbpVF", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 19, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gene2\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Booleans\n", + "a = 0\n", + "b = 1\n", + "print(bool( b )) # print True; insert the right variable\n", + "print(bool( a )) # print False; insert the right variable" + ], + "metadata": { + "id": "PrmB8FZD0-PQ", + "pycharm": { + "name": "#%%\n" + } + }, + "execution_count": 20, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ] + } + ] +} \ No newline at end of file