├── bonus.ipynb ├── exercise.ipynb ├── extra.ipynb └── main.ipynb /bonus.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Write a program that would receive a long string that groups several names of equal length, and the names are delimited by “:”. The program should produce an output list of each name on a separate line." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def split_print_names(names):\n", 17 | " names_list = names.split(\":\")\n", 18 | " for name in names_list:\n", 19 | " print(name)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 4, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "kareim\n", 32 | "ahmed\n", 33 | "hamada\n", 34 | "mohamed\n", 35 | "ibraheim\n", 36 | "john\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "names = \"kareim:ahmed:hamada:mohamed:ibraheim:john\"\n", 42 | "split_print_names(names)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | } 52 | ], 53 | "metadata": { 54 | "kernelspec": { 55 | "display_name": "Python 3", 56 | "language": "python", 57 | "name": "python3" 58 | }, 59 | "language_info": { 60 | "codemirror_mode": { 61 | "name": "ipython", 62 | "version": 3 63 | }, 64 | "file_extension": ".py", 65 | "mimetype": "text/x-python", 66 | "name": "python", 67 | "nbconvert_exporter": "python", 68 | "pygments_lexer": "ipython3", 69 | "version": "3.13.0" 70 | } 71 | }, 72 | "nbformat": 4, 73 | "nbformat_minor": 2 74 | } 75 | -------------------------------------------------------------------------------- /exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Write a Python program to get a string made of the first 2 and the last 2 chars from a given string. If the string length is less than or equal 2, return the empty string instead.\n", 8 | "\n", 9 | "- ‘udacity-deci’ -> Expected Result : ‘udci’\n", 10 | "- ‘deci’ -> Expected Result : 'deci'\n", 11 | "- 'w' -> Expected Result : Empty String\n", 12 | "- ‘de’ -> Expected Result: ‘dede’\n", 13 | "- ‘dec’ -> Expected Result: ‘deec’" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "def first_last_2(string):\n", 23 | " if len(string) < 2:\n", 24 | " return ''\n", 25 | " return string[0:2] + string[-2:]" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 5, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "udci\n", 38 | "deci\n", 39 | "\n", 40 | "dede\n", 41 | "deec\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "print(first_last_2('udacity-deci'))\n", 47 | "print(first_last_2('deci'))\n", 48 | "print(first_last_2('w'))\n", 49 | "print(first_last_2('de'))\n", 50 | "print(first_last_2('dec'))" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.13.0" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 2 82 | } 83 | -------------------------------------------------------------------------------- /extra.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Write a program to create a new string made of the middle three characters of an input string.\n", 8 | "- str1 = \"JhonDipPeta\" -> Expected Result = Dip\n", 9 | "- str2 = \"JaSonAy\" -> Expected Result = Son" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def get_middle_three(string):\n", 19 | " if len(string) < 3:\n", 20 | " return ''\n", 21 | " remaining = len(string) - 3\n", 22 | " start_index = remaining // 2\n", 23 | " return string[start_index:start_index+3]" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Dip\n", 36 | "Son\n", 37 | "bcd\n", 38 | "\n", 39 | "abc\n", 40 | "\n", 41 | "\n", 42 | "\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "print(get_middle_three('JhonDipPeta'))\n", 48 | "print(get_middle_three('JaSonAy'))\n", 49 | "print(get_middle_three('abcde'))\n", 50 | "print(get_middle_three('ab'))\n", 51 | "print(get_middle_three('abc'))\n", 52 | "print(get_middle_three(''))\n", 53 | "print(get_middle_three('a'))\n", 54 | "print(get_middle_three('ab'))" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 3", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.13.0" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 2 86 | } 87 | -------------------------------------------------------------------------------- /main.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Indexing, Slicing, and Concatenation\n", 8 | "\n", 9 | "## Variables\n", 10 | "\n", 11 | "- Variables are placeholders or containers that store data in memory.\n", 12 | "- They can be reassigned to different values during the execution of a program.\n", 13 | "\n", 14 | "## Literals\n", 15 | "\n", 16 | "- Literals are fixed values that are directly used in the code.\n", 17 | "- They represent specific values like numbers, strings, or Boolean values." 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "x = 5 # Here, 'x' is a variable storing the integer value 5\n", 27 | "x = \"Hello\" # Now, 'x' stores the string \"Hello\"\n", 28 | "\n", 29 | "num = 10 # Here, '10' is a numeric literal assigned to the variable 'num'\n", 30 | "message = 'Welcome' # 'Welcome' is a string literal assigned to 'message'" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "## Indexing\n", 38 | "\n", 39 | "- Indexing refers to accessing individual elements within an ordered collection like lists, tuples, or strings.\n", 40 | "- It is achieved by using square brackets [] along with the index position.\n", 41 | "- Indexing in Python starts from 0 for the first element.\n", 42 | "- Used to retrieve specific elements from sequences like lists, strings, or tuples.\n", 43 | "- Facilitates modifications or extraction of particular elements based on their position within the sequence." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 1, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "30\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "# Accessing the element at index 2 (which is 30)\n", 61 | "\n", 62 | "my_list = [10, 20, 30, 40, 50]\n", 63 | "print(my_list[2])" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "## Slicing\n", 71 | "\n", 72 | "- Slicing refers to extracting a specific portion of a sequence by specifying a range using the colon `:` operator.\n", 73 | "- The syntax is `sequence[start:stop:step]`, where start is the starting index, stop is the ending index (exclusive), and step is the step size.\n", 74 | "- Used to extract a segment of a sequence (list, string, tuple) based on specified start and stop indices.\n", 75 | "- Facilitates working with specific portions of data without modifying the original sequence.\n" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 2, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "[2, 3, 4]\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "# Slicing the list from index 1 to index 3 (output: [2, 3, 4])\n", 93 | "my_list = [1, 2, 3, 4, 5]\n", 94 | "print(my_list[1:4])" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "## Concatenation\n", 102 | "\n", 103 | "- Concatenation is the process of combining two or more strings, lists, or other sequences into one.\n", 104 | "- In Python, concatenating strings is done using the + operator.\n", 105 | "- Concatenation can be applied after slicing to merge specific parts of sequences obtained through slicing.\n", 106 | "- Delimiter: A delimiter is a character or sequence of characters used to separate or define boundaries between data elements. Common delimiters include commas (,), spaces, tabs, pipes (|), etc." 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "## Exercise\n", 114 | "\n", 115 | "In the provided examples try to guess the output of the print statements." 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 3, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "Pto-rgamn-Prog\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "# Example 1\n", 133 | "text = \"Python-Programming\"\n", 134 | "result = text[::2] + \"-\" + text[7:11]\n", 135 | "print(result)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 4, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "102030\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "# Example 2\n", 153 | "x = 10\n", 154 | "y = \"20\"\n", 155 | "output = str(x) + y[0] + y[1] + str(int(y) + x)\n", 156 | "print(output)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 5, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "[2, 4, 6, 8, 1, 10]\n" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "# Example 3\n", 174 | "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 175 | "res = data[1:9:2] + [data[0]] + [data[-1]]\n", 176 | "print(res)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": null, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [] 185 | } 186 | ], 187 | "metadata": { 188 | "kernelspec": { 189 | "display_name": "Python 3", 190 | "language": "python", 191 | "name": "python3" 192 | }, 193 | "language_info": { 194 | "codemirror_mode": { 195 | "name": "ipython", 196 | "version": 3 197 | }, 198 | "file_extension": ".py", 199 | "mimetype": "text/x-python", 200 | "name": "python", 201 | "nbconvert_exporter": "python", 202 | "pygments_lexer": "ipython3", 203 | "version": "3.13.0" 204 | } 205 | }, 206 | "nbformat": 4, 207 | "nbformat_minor": 2 208 | } 209 | --------------------------------------------------------------------------------