diff --git a/.gitignore b/.gitignore index e43b0f98..a9c348f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +.venv/ diff --git a/04_python_practice/01_Python_Basics/Basics/1_Numeric_Variable_Types.ipynb b/04_python_practice/01_Python_Basics/Basics/1_Numeric_Variable_Types.ipynb index 82530e84..d74ae397 100644 --- a/04_python_practice/01_Python_Basics/Basics/1_Numeric_Variable_Types.ipynb +++ b/04_python_practice/01_Python_Basics/Basics/1_Numeric_Variable_Types.ipynb @@ -46,88 +46,165 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.429214Z", "start_time": "2020-06-30T11:34:51.418682Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "32 + 23" + "32 + 23\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.456179Z", "start_time": "2020-06-30T11:34:51.436541Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-2" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "19 - 21" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.503183Z", "start_time": "2020-06-30T11:34:51.463300Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "121" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "11 * 11" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.554866Z", "start_time": "2020-06-30T11:34:51.510152Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.375" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "12 / 32" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.565917Z", "start_time": "2020-06-30T11:34:51.558759Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "3 ** 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "4 + 6 * 5" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "(4 + 6) * 5" ] @@ -145,9 +222,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Floor division \n", "34 // 4" @@ -163,9 +251,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Modulo\n", "34 % 4" @@ -183,45 +282,100 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2.14" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "abs(2.14)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2.14" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "abs(-2.14)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "min(14, 21, 32, 11, 44, 23)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "44" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "max(14, 21, 32, 11, 44, 23)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3.142" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "round(3.14159265359, 3)" ] @@ -246,7 +400,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.694614Z", @@ -261,42 +415,75 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.711513Z", "start_time": "2020-06-30T11:34:51.703416Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "b" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.723777Z", "start_time": "2020-06-30T11:34:51.714309Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a - 4" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.736043Z", "start_time": "2020-06-30T11:34:51.726241Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'HelloHelloHelloHelloHelloHelloHelloHello'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a * b" ] @@ -319,14 +506,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.753269Z", "start_time": "2020-06-30T11:34:51.738671Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a = a + 5\n", "a" @@ -381,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.223748Z", @@ -389,14 +587,25 @@ }, "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "14 # Int" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.302631Z", @@ -404,63 +613,118 @@ }, "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "32.2" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "32.2 # Float" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.353655Z", "start_time": "2020-06-30T11:34:51.313466Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(5+6j)" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "complex(5, 6) # complex" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.379983Z", "start_time": "2020-06-30T11:34:51.361564Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(14)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.396272Z", "start_time": "2020-06-30T11:34:51.385142Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(82.6)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2020-06-30T11:34:51.410079Z", "start_time": "2020-06-30T11:34:51.399690Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "complex" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(complex(5, 6))" ] @@ -530,6 +794,30 @@ "6. `(21 / 3) % 2`" ] }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8.0 3.0 144 4 2.857142857142857 1.0\n" + ] + } + ], + "source": [ + "x1 = 19.0 - 11\n", + "x2 = 12 * 0.25\n", + "x3 = 12 ** 2\n", + "x4 = 25 // 6\n", + "x5 = (12 + 8) / 7\n", + "x6 = (21 / 3) % 2\n", + "\n", + "print(x1, x2, x3, x4, x5, x6)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -550,6 +838,36 @@ "What are the values of x and y after each line?" ] }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "82\n", + "30\n", + "112\n", + "109\n", + "8\n" + ] + } + ], + "source": [ + "x = 82\n", + "print(x)\n", + "y = 30\n", + "print(y)\n", + "x += y\n", + "print(x)\n", + "y = x - 3\n", + "print(y)\n", + "x -= y - 5\n", + "print(x)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -563,6 +881,66 @@ "3. `complex(-5, -10.2)`" ] }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(100.32)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "complex" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(complex(-5, -10.2))" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -575,10 +953,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27.666666666666668\n" + ] + } + ], + "source": [ + "p1 = 21 \n", + "p2 = 43\n", + "p3 = 19\n", + "p_mean = (p1 + p2 +p3)/ 3 \n", + "print(p_mean)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27.67\n" + ] + } + ], + "source": [ + "print (round(p_mean, 2))" + ] } ], "metadata": { @@ -597,7 +1006,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "toc": { "base_numbering": 1, diff --git a/04_python_practice/01_Python_Basics/Basics/2_Strings.ipynb b/04_python_practice/01_Python_Basics/Basics/2_Strings.ipynb index 50502e17..95b9b214 100644 --- a/04_python_practice/01_Python_Basics/Basics/2_Strings.ipynb +++ b/04_python_practice/01_Python_Basics/Basics/2_Strings.ipynb @@ -132,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:16.315983Z", @@ -146,14 +146,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:16.520959Z", "start_time": "2020-02-03T15:12:16.516886Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a string variable.'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable # my_str_variable holds the string that we put in it in the above cell. " ] @@ -170,28 +181,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:17.160652Z", "start_time": "2020-02-03T15:12:17.156894Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'My first stringMy second string'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'My first string' + 'My second string'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:17.391947Z", "start_time": "2020-02-03T15:12:17.388407Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Repeating stringRepeating stringRepeating string'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'Repeating string' * 3" ] @@ -206,28 +239,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:18.143676Z", "start_time": "2020-02-03T15:12:18.140185Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'My first string My second string'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'My first string' + ' ' + 'My second string'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:18.339653Z", "start_time": "2020-02-03T15:12:18.336299Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Repeating string Repeating string Repeating string '" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'Repeating string ' * 3" ] @@ -242,28 +297,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:18.726966Z", "start_time": "2020-02-03T15:12:18.723621Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Repeating string Repeating string Repeating string'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'Repeating string Repeating string Repeating string '.strip()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:18.916816Z", "start_time": "2020-02-03T15:12:18.912908Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Repeating string Repeating string Repeating string'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "' Repeating string Repeating string Repeating string '.strip()" ] @@ -280,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:19.338107Z", @@ -294,28 +371,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:19.524151Z", "start_time": "2020-02-03T15:12:19.520640Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'This is my string to play around with.'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable.capitalize()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:19.719967Z", "start_time": "2020-02-03T15:12:19.716671Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'THIS IS MY STRING TO PLAY AROUND WITH.'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable.upper()" ] @@ -323,45 +422,94 @@ { "cell_type": "code", "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:19.959612Z", "start_time": "2020-02-03T15:12:19.956274Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'this is my string to play around with.'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable.lower()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:20.275946Z", "start_time": "2020-02-03T15:12:20.272448Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'this IS my flING to PLAY around WITH.'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable.replace('STR', 'fl')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:12:20.525214Z", "start_time": "2020-02-03T15:12:20.521608Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['this', 'IS', 'my', 'STRING', 'to', 'PLAY', 'around', 'WITH.']" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable.split()" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_str_variable.strip" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -407,14 +555,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:15.822311Z", "start_time": "2020-02-03T15:44:15.816404Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'e'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable = 'Test String'\n", "my_str_variable[1]" @@ -422,42 +581,75 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:16.031105Z", "start_time": "2020-02-03T15:44:16.025334Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'S'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[5]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:16.223295Z", "start_time": "2020-02-03T15:44:16.218071Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'g'" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[-1]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:16.412098Z", "start_time": "2020-02-03T15:44:16.408059Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'i'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[-3]" ] @@ -474,70 +666,125 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:16.770139Z", "start_time": "2020-02-03T15:44:16.764956Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'es'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[1:3]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:16.957348Z", "start_time": "2020-02-03T15:44:16.951604Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Stri'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[5:9]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:17.157518Z", "start_time": "2020-02-03T15:44:17.152361Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Strin'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[-6:-1]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:17.357536Z", "start_time": "2020-02-03T15:44:17.352390Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'est String'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[1:]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:17.655484Z", "start_time": "2020-02-03T15:44:17.650201Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Test Strin'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[:-1]" ] @@ -556,28 +803,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:20.584891Z", "start_time": "2020-02-03T15:44:20.579449Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Ts tig'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[::2]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:21.319490Z", "start_time": "2020-02-03T15:44:21.313733Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'sSi'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str_variable[2:10:3]" ] @@ -596,14 +865,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:23.717933Z", "start_time": "2020-02-03T15:44:23.712350Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "h\n", + "e\n", + "l\n", + "l\n", + "o\n" + ] + } + ], "source": [ "my_str, idx = 'hello', 0\n", "while idx < 5:\n", @@ -621,14 +902,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T15:44:41.876818Z", "start_time": "2020-02-03T15:44:41.871637Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_str = 'hello'\n", "\n", @@ -662,7 +954,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "my_str,ind= 'hello', 0\n", + "while idx < len(my_str):\n", + " print(my_str[idx])\n", + " idx += 1\n" + ] }, { "attachments": {}, @@ -682,10 +979,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n" + ] + } + ], + "source": [ + "my_str = 'Hello'\n", + "for idx in range(len(my_str)):\n", + " print(my_str[idx])" + ] }, { "attachments": {}, @@ -741,6 +1054,31 @@ "```" ] }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Sean\n", + "Hello Sean\n", + "Hello Sean\n" + ] + } + ], + "source": [ + "my_name = 'Sean'\n", + "\n", + "print('Hello %s' % my_name)\n", + "\n", + "print('Hello {}'.format(my_name))\n", + "\n", + "print(f'Hello {my_name}')" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -757,6 +1095,13 @@ "```" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "attachments": {}, "cell_type": "markdown", @@ -784,12 +1129,130 @@ " * What happens if you split by a comma and a space (`', '`)?" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Answer 1. when the string itself contains quots. " + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "They told me not to do this, but I didn't listen.\n" + ] + } + ], + "source": [ + "my_str_variable = \"They told me not to do this, but I didn't listen.\"\n", + "print(my_str_variable)" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello elham\n" + ] + } + ], + "source": [ + "a = 'elham'\n", + "b = 'Sara'\n", + "print('Hello ' +a)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Htllo, Stan'" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_string = 'Hello, Sean'\n", + "my_string. replace('e' , 't')" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello,', 'Sean']" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_string.split()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello', ' Sean']" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_string.split(',')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello', 'Sean']" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_string.split(', ')" + ] } ], "metadata": { @@ -808,7 +1271,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "toc": { "base_numbering": 1, diff --git a/04_python_practice/01_Python_Basics/Basics/3_If_Statement.ipynb b/04_python_practice/01_Python_Basics/Basics/3_If_Statement.ipynb index c8babd23..9cf7d159 100644 --- a/04_python_practice/01_Python_Basics/Basics/3_If_Statement.ipynb +++ b/04_python_practice/01_Python_Basics/Basics/3_If_Statement.ipynb @@ -71,28 +71,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.372095Z", "start_time": "2020-02-03T11:53:01.362664Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(True)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.377605Z", "start_time": "2020-02-03T11:53:01.373799Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(False)" ] @@ -107,84 +129,150 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.383289Z", "start_time": "2020-02-03T11:53:01.379756Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1 == 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.388673Z", "start_time": "2020-02-03T11:53:01.385142Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1 != 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.394825Z", "start_time": "2020-02-03T11:53:01.390376Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1 < 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.400648Z", "start_time": "2020-02-03T11:53:01.396761Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1 > 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.406052Z", "start_time": "2020-02-03T11:53:01.402467Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1 <= 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:01.411632Z", "start_time": "2020-02-03T11:53:01.408212Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1 >= 2" ] @@ -219,14 +307,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:03.876556Z", "start_time": "2020-02-03T11:53:01.413240Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ "x = input('Please enter a number: ')\n", "print(x)" @@ -242,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:44.215172Z", @@ -250,7 +346,15 @@ }, "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "x = int(input('Please enter a number: '))\n", "if x > 5:\n", @@ -268,13 +372,6 @@ "**Note**: `input()` actually interprets the input as strings, so we have to manually tell Python to treat the number we pass as an integer with `int()`." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "attachments": {}, "cell_type": "markdown", @@ -316,7 +413,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T14:44:59.606574Z", @@ -324,7 +421,15 @@ }, "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You entered a positive number.\n" + ] + } + ], "source": [ "x = int(input('Please enter a number: '))\n", "if x < 0:\n", @@ -353,14 +458,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T14:45:15.855255Z", "start_time": "2020-02-03T14:45:13.319331Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You entered a number bigger than 5.\n" + ] + } + ], "source": [ "x = int(input('Please enter a number: '))\n", "if x > 5:\n", @@ -387,13 +500,6 @@ "**Note**: The `else` part of the statement is actually optional. If it is not included, then we'd notice that at most one of the conditional blocks in an `if`-`else` statement will be evaluated." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "attachments": {}, "cell_type": "markdown", @@ -412,7 +518,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:08.522244Z", @@ -441,14 +547,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:08.528373Z", "start_time": "2020-02-03T11:53:08.524546Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ "x = 4\n", "if x > 10 or x < 5:\n", @@ -457,7 +571,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T11:53:08.536062Z", @@ -489,9 +603,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You are either too young or in the wrong country!\n" + ] + } + ], "source": [ "country = \"US\"\n", "age = 18\n", @@ -542,6 +664,141 @@ "3. Write the following condition in Python: if x isn't a positive number print x.\n", "4. Write the following condition in Python: if x is between 1 and 50 except the numbers between 25 to 30 print x." ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is smaller than 100\n" + ] + } + ], + "source": [ + "x = 14\n", + "if x< 100:\n", + " print('The number is smaller than 100')" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You entered a positive number.\n", + "The number is: 5\n" + ] + } + ], + "source": [ + "x = int(input('Please enter a number: '))\n", + "if x >= 0:\n", + " print('You entered a positive number.')\n", + "elif (x + 50) > 0:\n", + " print('By adding 50, it will be positive.')\n", + "print('The number is:', x)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a big number.\n" + ] + } + ], + "source": [ + "x = 100\n", + "if (x // 10) == 0:\n", + " print('This is a single digit number.')\n", + "elif (x // 100) == 0:\n", + " print('This is a double digit number.')\n", + "else:\n", + " print('This is a big number.')" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is between 20 and 30.\n" + ] + } + ], + "source": [ + "x = 25\n", + "if (x >= 20 or x >= 30) : \n", + " print('The number is between 20 and 30.')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20\n" + ] + } + ], + "source": [ + "x = 20\n", + "if x > 10 :\n", + " print (x)\n", + "\n", + "if (x // 10) > 0:\n", + " print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "x = 20\n", + "if not (x > 0) :\n", + " print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "45\n" + ] + } + ], + "source": [ + "if x>=1 and x<=50 and not(x>=25 and x<=30):\n", + " print(x)" + ] } ], "metadata": { @@ -560,7 +817,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.13" + "version": "3.9.6" }, "toc": { "base_numbering": 1, diff --git a/04_python_practice/01_Python_Basics/Basics/4_Loops.ipynb b/04_python_practice/01_Python_Basics/Basics/4_Loops.ipynb index 71dc925d..4e46b0ab 100644 --- a/04_python_practice/01_Python_Basics/Basics/4_Loops.ipynb +++ b/04_python_practice/01_Python_Basics/Basics/4_Loops.ipynb @@ -211,9 +211,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "8\n", + "10\n" + ] + } + ], "source": [ "my_list = [5, 8, 10]\n", "for i in my_list:\n", @@ -222,9 +232,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max\n", + "Moritz\n", + "Monika\n" + ] + } + ], "source": [ "names = [\"Max\", \"Moritz\", \"Monika\"]\n", "for name in names:\n", @@ -233,9 +253,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n" + ] + } + ], "source": [ "for x in range(10):\n", " print(x)" @@ -279,14 +316,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T13:52:12.530455Z", "start_time": "2020-02-03T13:52:12.522080Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loop #: 1, x: 1 and Total: 1\n", + "Loop #: 2, x: 2 and Total: 3\n", + "Loop #: 3, x: 3 and Total: 6\n", + "Loop #: 4, x: 4 and Total: 10\n", + "Loop #: 5, skipped x: 5 and Total: 10\n", + "Loop #: 6, x: 6 and Total: 16\n", + "Loop #: 7, x: 7 and Total: 23\n", + "Loop #: 8, x: 8 and Total: 31\n", + "Loop #: 9, final x: 9 and final total: 31\n" + ] + } + ], "source": [ "total, x = 0, 1\n", "loop_counter = 1\n", @@ -341,14 +394,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-02-03T13:52:24.516622Z", "start_time": "2020-02-03T13:52:24.510260Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loop #: 1, x: 1 and Total: 1\n", + "Loop #: 2, x: 2 and Total: 3\n", + "Loop #: 3, x: 3 and Total: 6\n", + "Loop #: 4, x: 4 and Total: 10\n", + "Loop #: 5, x: 5 and Total: 15\n", + "Loop #: 6, x: 6 and Total: 21\n", + "Loop #: 7, x: 7 and Total: 28\n", + "At Loop # 8 the while-loop was terminated.\n", + "The sum exceeded the max value of 25. Your Total is set to 28.\n" + ] + } + ], "source": [ "total, x = 0, 1\n", "loop_counter = 1\n", @@ -422,7 +491,17 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + } + ], "source": [ "for i in range(0, 10):\n", " if i == 3:\n", @@ -434,7 +513,23 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n" + ] + } + ], "source": [ "for i in range(0, 10):\n", " if i == 3:\n", @@ -443,7 +538,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -451,7 +545,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -474,24 +567,80 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2020-02-03T13:52:54.890048Z", - "start_time": "2020-02-03T13:52:43.542233Z" + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "45\n" + ] } - }, - "outputs": [], + ], "source": [ - "total, x = 0, 1\n", - "while x >= 0:\n", - " total += x\n", - " x += 1\n", - "print(total)" + "i = 1\n", + "my_sum = 0\n", + "while i != 10:\n", + " my_sum += i\n", + " i += 1\n", + "print(my_sum)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "44\n" + ] + } + ], + "source": [ + "i = 2\n", + "my_sum = 0\n", + "while i != 10:\n", + " my_sum += i\n", + " i += 1\n", + "print(my_sum)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "48\n" + ] + } + ], + "source": [ + "i = 3\n", + "my_sum = 0\n", + "while (i <= 14):\n", + " if i%2 != 0:\n", + " my_sum += i\n", + " i += 2\n", + "print(my_sum)" ] }, { "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Check your understanding" + ] + }, + { "cell_type": "markdown", "metadata": {}, "source": [ @@ -504,6 +653,89 @@ "4. You have a list with all continents ( continents = [\"Africa\", \"Antarctica\", \"Asia\", \"Australia and Oceania\", \"Europe\", \"North America\", \"South America\"] ). Can you print only the continents from the southern hemisphere?" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "45\n" + ] + } + ], + "source": [ + "sum = 0\n", + "for i in range(10):\n", + " sum += i\n", + "print(sum)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "44\n" + ] + } + ], + "source": [ + "sum = 0\n", + "for i in range(2,10) :\n", + " sum += i\n", + "print(sum)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "48\n" + ] + } + ], + "source": [ + "sum = 0\n", + "for i in range(3,14,2):\n", + " sum += i\n", + "print(sum)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Antarctica\n", + "Asia\n", + "North America\n" + ] + } + ], + "source": [ + "continents = [\"Africa\", \"Antarctica\", \"Asia\", \"Australia and Oceania\", \"Europe\", \"North America\", \"South America\"]\n", + "\n", + "for i in continents:\n", + " if (i=='Antarctica') or (i=='Europa') or (i=='North America') or (i=='Asia'):\n", + " print(i)\n" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -519,6 +751,13 @@ " " ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "attachments": {}, "cell_type": "markdown", @@ -577,7 +816,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "metadata": { "interpreter": { diff --git a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/1_Lists.ipynb b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/1_Lists.ipynb index da8a262f..af4d057b 100644 --- a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/1_Lists.ipynb +++ b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/1_Lists.ipynb @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -64,16 +64,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 'hello', 3, 'goodbye']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_first_lst" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -82,9 +93,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['h', 'e', 'l', 'l', 'o']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_second_lst" ] @@ -99,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -108,9 +130,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3], ['str1', 'str2', 'str3'], [1, 'mixed', 3]]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_lst_of_lsts" ] @@ -157,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -166,9 +199,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# indexing\n", "my_lst[1]" @@ -176,9 +220,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# slicing\n", "my_lst[2:3]" @@ -195,18 +250,40 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 'hello', 'goodbye']" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_lst[:]" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'goodbye'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_lst[-1]" ] @@ -221,7 +298,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -230,9 +307,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 7, 10]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# grab every third element of whole list\n", "my_lst[::3]" @@ -240,9 +328,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# grab every third element ending with element #4\n", "my_lst[:4:3]" @@ -266,7 +365,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -275,9 +374,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], "source": [ "for num in my_lst:\n", " print(num)" @@ -297,9 +408,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 1\n", + "1 2\n", + "2 3\n", + "3 4\n", + "4 5\n" + ] + } + ], "source": [ "for idx, num in enumerate(my_lst):\n", " print(idx, num)" @@ -354,6 +477,44 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4]\n", + "[3, 45, 78, 6, 0]\n", + "[1.2, 4.3, 5.9, 6.4, 8.34]\n", + "[1.2, 3, 't4']\n", + "[[0, 1, 2, 3, 4], [1.2, 3, 't4'], ['a', 'f', 't']]\n", + "[(0, 1.2, 'H'), (1, 3, 'e'), (2, 't4', 'l')]\n" + ] + } + ], + "source": [ + "mylist1 = list(range(5))\n", + "print(mylist1)\n", + "\n", + "mylist2 = [3, 45, 78, 6, 0]\n", + "print(mylist2)\n", + "\n", + "mylist3 = [1.2, 4.3, 5.9, 6.4, 8.34]\n", + "print(mylist3)\n", + "\n", + "mylist3 = [1.2, 3, 't4']\n", + "print(mylist3)\n", + "\n", + "mylist_of_list1= [mylist1, mylist3, ['a', 'f', 't']]\n", + "print(mylist_of_list1)\n", + "\n", + "mylist_of_list2= list(zip(mylist1, mylist3, list('Hello')))\n", + "print(mylist_of_list2)\n" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -373,6 +534,62 @@ "9. How many items are now in your list? (you can check if your right with `len(my_list)`)" ] }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10]\n", + "[2, 4, 6, 8, 10, 12]\n", + "[12, 10, 8, 6, 4, 2]\n", + "The number of value 12 in list is: 1\n", + "The number of value 12 in list is: 2\n", + "[2, 4, 6, 8, 10, 12, 12]\n", + "[2, 4, 6, 8, 10, 12]\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "even_lst = []\n", + "for i in range(1,11):\n", + " if i%2 == 0 :\n", + " even_lst.append(i) \n", + "print(even_lst)\n", + "\n", + "even_lst.append(12)\n", + "print(even_lst)\n", + "\n", + "even_lst.reverse()\n", + "print(even_lst)\n", + "\n", + "print('The number of value 12 in list is:' , even_lst.count(12))\n", + "\n", + "even_lst.append(12)\n", + "print('The number of value 12 in list is:' , even_lst.count(12))\n", + "\n", + "even_lst.sort()\n", + "print(even_lst)\n", + "\n", + "even_lst.pop()\n", + "print(even_lst)\n", + "\n", + "len(even_lst)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -387,6 +604,31 @@ "3. How would I use interval indexing to grab 7 and 15 from the list?" ] }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "22\n", + "12\n", + "[7, 15]\n" + ] + } + ], + "source": [ + "pre_lst = [3, 7, 12, 15, 22]\n", + "\n", + "print(pre_lst[1])\n", + "print(pre_lst[4])\n", + "print(pre_lst[-3])\n", + "print(pre_lst[1:4:2])" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -404,6 +646,58 @@ "4. Now modify the `for` loop so that we print out the index of the elements along with the elements themselves." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "[0, 2, 4, 6, 8, 10]\n", + "1\n", + "3\n", + "5\n", + "7\n", + "9\n", + "[1, 3, 5, 7, 9]\n", + "0 0\n", + "1 2\n", + "2 4\n", + "3 6\n", + "4 8\n", + "5 10\n" + ] + } + ], + "source": [ + "evens =[]\n", + "my_list = range(0,11)\n", + "for i in my_list:\n", + " if i%2 == 0:\n", + " print(i)\n", + " evens.append(i)\n", + "print(evens)\n", + "\n", + "odds =[]\n", + "my_list = range(0,11)\n", + "for i in my_list:\n", + " if i%2 == 1:\n", + " print(i)\n", + " odds.append(i)\n", + "print(odds)\n", + "3\n", + "for indx, value in enumerate(evens):\n", + " print(indx, value)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -442,7 +736,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, "outputs": [], "source": [ @@ -456,8 +750,67 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "'Customer 3: Gina is 85 years oldand has bought Photo Album'" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "f\"Customer 3: {customers[2][0]} is {customers[2][1]} years oldand has bought {customers[2][2]}\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Paul is 33 yearsn old and has bought Macbook Pro .\n", + "Anna is 12 yearsn old and has bought Looping Louie .\n", + "Gina is 85 yearsn old and has bought Photo Album .\n", + "Kim is 59 yearsn old and has bought Plants .\n" + ] + } + ], + "source": [ + "for name_cust, age_cust, prod_cust in customers:\n", + " print(name_cust, 'is',age_cust,'yearsn old and has bought', prod_cust,'.')\n", + " f\"Customer {name_cust} is {age_cust} years old and has bought {prod_cust}.\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer 0: Paul is 33 years old and has bought Macbook Pro.\n", + "Customer 1: Anna is 12 years old and has bought Looping Louie.\n", + "Customer 2: Gina is 85 years old and has bought Photo Album.\n", + "Customer 3: Kim is 59 years old and has bought Plants.\n" + ] + } + ], + "source": [ + "\n", + "for i, cust in enumerate(customers):\n", + " name_cust, age_cust, prod_cust = cust\n", + " print(f\"Customer {i}: {name_cust} is {age_cust} years old and has bought {prod_cust}.\")\n" + ] } ], "metadata": { @@ -476,7 +829,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "vscode": { "interpreter": { diff --git a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/2_Sets.ipynb b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/2_Sets.ipynb index df2f69b1..be70e2f0 100644 --- a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/2_Sets.ipynb +++ b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/2_Sets.ipynb @@ -42,7 +42,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -60,9 +60,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# we create identical sets:\n", "my_set == my_other_set" @@ -89,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -98,18 +109,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 5, 6, 7}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_set.union(my_other_set)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_set.add(4)\n", "my_set" @@ -117,9 +150,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5, 6, 7}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_set.update(my_other_set)\n", "my_set" @@ -127,9 +171,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 6, 7}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_set.remove(5)\n", "my_set" @@ -137,9 +192,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{6, 7}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_set.intersection(my_other_set)" ] @@ -176,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -185,7 +251,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -194,18 +260,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "790 µs ± 2.06 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n" + ] + } + ], "source": [ "timeit 100000 in my_list" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "26.2 ns ± 0.185 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n" + ] + } + ], "source": [ "timeit 100000 in my_set" ] @@ -239,6 +321,33 @@ "5. In one line of code, add all the elements of `second_set` to `first_set`." ] }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9, 'Hello'}" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_set = set(range(1,11))\n", + "second_set = set(range(5,16))\n", + "first_set.add(11)\n", + "second_set.add('Hello')\n", + "\n", + "first_set.intersection(second_set)\n", + "\n", + "first_set.union(second_set)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -253,15 +362,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'chicken wings', 'soda'}\n", + "True\n" + ] + } + ], + "source": [ + "first_list = ['hello', 'there', 'things', 'stuff', 'other', 'soda', 'chicken wings', 'things', 'soda']\n", + "second_list = ['turkey sandwich', 'guacamole', 'chicken wings', 'OJ', 'soda']\n", + "uniq_elm = set(first_list)\n", + "inter_elm = set(first_list).intersection(set(second_list))\n", + "print(inter_elm)\n", + "\n", + "uni_elm = set(first_list).union(set(second_list))\n", + "\n", + "bool_pizza = uni_elm.isdisjoint('pizza')\n", + "print(bool_pizza)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3.9.4 ('.venv': venv)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -275,12 +404,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.4" - }, - "vscode": { - "interpreter": { - "hash": "856105b4be0f061912d9e3bd57601f265232cc0f0021e629e6ca084c3e4ca463" - } + "version": "3.9.6" } }, "nbformat": 4, diff --git a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/3_Mutability.ipynb b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/3_Mutability.ipynb index 821ac13a..1991cd1f 100644 --- a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/3_Mutability.ipynb +++ b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/3_Mutability.ipynb @@ -42,9 +42,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "l = list(range(10))\n", "l" @@ -52,9 +63,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 0, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "l[4] = 0\n", "l" @@ -62,9 +84,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 0, 5, 6, 7, 8, 9, 1]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "l.append(1)\n", "l" @@ -81,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -91,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -130,9 +163,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_first_tuple = tuple([1, 2])\n", "my_first_tuple" @@ -140,9 +184,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_other_tuple = (1, 2)\n", "my_other_tuple" @@ -150,9 +205,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2)" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_third_tuple = 1, 2\n", "my_third_tuple" @@ -167,9 +233,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "evens = []\n", "for element in range(1, 21):\n", @@ -189,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -198,27 +275,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(t[0])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(t[1])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(t[2])" ] @@ -227,7 +337,18 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(t[3])" ] @@ -243,7 +364,18 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 'bird', [1, 2, 3], (1, 2))" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "t[2].append(3)\n", "t" @@ -285,6 +417,45 @@ "6. Make sure that you tried out the code from the above examples. Did those operations return anything? Why or why not?\n" ] }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 200, 6, 7, 8, 9, 10]\n", + "[1, 2, 3, 4, 200, 6, 7, 8, 9, 10, 9]\n", + "[1, 2, 3, 4, 6, 7, 8, 9, 9, 10, 200]\n", + "['Hello', 1, 2, 3, 4, 6, 7, 8, 9, 9, 10, 200]\n", + "[200, 10, 9, 9, 8, 7, 6, 4, 3, 2, 1, 'Hello']\n" + ] + } + ], + "source": [ + "my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "my_list[4] = 200\n", + "print(my_list)\n", + "my_list.append(9)\n", + "print(my_list)\n", + "my_list.sort()\n", + "print(my_list)\n", + "my_list.insert(0, 'Hello')\n", + "print(my_list)\n", + "my_list.reverse()\n", + "print(my_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5 - Because List are Mutable. \n", + "6 - Because the changes are applied directly to the existing list instead of creating new one. these oprerations returns None. " + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -300,6 +471,68 @@ " 2. Add the word `\"hello\"` to the list in the tuple as the first element in the list." ] }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "Hello\n" + ] + } + ], + "source": [ + "my_tuple = (1, 'Hello')\n", + "print(my_tuple[0])\n", + "print(my_tuple[1])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2- No, because tuples are not mutable. " + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "other_tuple = ('other', [])\n", + "other_tuple[1].append('there')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3.1- Bcause lists in tuples are mutable. " + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('other', ['Hello', 'there'])\n" + ] + } + ], + "source": [ + "other_tuple[1].insert(0, 'Hello')\n", + "print(other_tuple)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -324,7 +557,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.4" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/4_Dictionaries.ipynb b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/4_Dictionaries.ipynb index 5d8dfb4b..f66e33c1 100644 --- a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/4_Dictionaries.ipynb +++ b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/4_Dictionaries.ipynb @@ -33,9 +33,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Georgia', 'Atlanta'), ('Colorado', 'Denver'), ('Indiana', 'Indianapolis')]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "states_caps = [('Georgia', 'Atlanta'), ('Colorado', 'Denver'), ('Indiana', 'Indianapolis')]\n", "states_caps" @@ -51,9 +62,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Indianapolis'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "search_state = 'Indiana'\n", "capital = 'State not found'\n", @@ -76,9 +98,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Georgia': 'Atlanta', 'Colorado': 'Denver', 'Indiana': 'Indianapolis'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "states_caps_dict = {'Georgia': 'Atlanta', 'Colorado': 'Denver', 'Indiana': 'Indianapolis'}\n", "states_caps_dict" @@ -96,18 +129,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Indianapolis'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "states_caps_dict['Indiana']" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'Washington'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mstates_caps_dict\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mWashington\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n", + "\u001b[0;31mKeyError\u001b[0m: 'Washington'" + ] + } + ], "source": [ "states_caps_dict['Washington']" ] @@ -124,9 +180,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'State not found'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "states_caps_dict.get('Washington', 'State not found')" ] @@ -159,9 +226,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_dict = {'thing': 1, 'other': 2}\n", "my_dict['thing']" @@ -169,9 +247,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'thing': 3, 'other': 2}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_dict['thing'] = 3\n", "my_dict" @@ -179,14 +268,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'thing': 3, 'other': 2, 'thingy': 4}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "my_dict['thingy'] = 4\n", "my_dict" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "attachments": {}, "cell_type": "markdown", @@ -229,9 +336,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "unhashable type: 'list'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[13], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m my_bad_key \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mkey\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m my_dict \u001b[38;5;241m=\u001b[39m {my_bad_key: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mThis wont work\u001b[39m\u001b[38;5;124m'\u001b[39m}\n", + "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" + ] + } + ], "source": [ "my_bad_key = ['key']\n", "my_dict = {my_bad_key: 'This wont work'}" @@ -268,9 +387,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n" + ] + } + ], "source": [ "for element in range(10):\n", " if element % 2 == 0:\n", @@ -287,9 +418,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Georgia': 'Atlanta', 'Colorado': 'Denver', 'Indiana': 'Indianapolis'}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "states_caps_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Georgia\n", + "Colorado\n", + "Indiana\n" + ] + } + ], "source": [ "for thing in states_caps_dict:\n", " print(thing)" @@ -307,9 +468,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Atlanta\n", + "Denver\n", + "Indianapolis\n" + ] + } + ], "source": [ "for value in states_caps_dict.values():\n", " print(value)" @@ -327,9 +498,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Georgia Atlanta\n", + "Colorado Denver\n", + "Indiana Indianapolis\n" + ] + } + ], "source": [ "for state, capital in states_caps_dict.items():\n", " print(state, capital)" @@ -345,12 +526,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Georgia', 'Atlanta')\n", + "('Colorado', 'Denver')\n", + "('Indiana', 'Indianapolis')\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "for thing in states_caps_dict.items():\n", - " print(thing)" + " print(thing)\n", + "\n", + "len(thing)" ] }, { @@ -386,6 +589,45 @@ "3. What if you don't know whether or not `'Outback Steakhouse'` is in the `restaurant_types` dictionary - how would you go about trying to get it's restaurant type and make sure that you won't get an error?" ] }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fast Food\n" + ] + } + ], + "source": [ + "resturant_types = {'Red Lobster': 'Seafood', 'Burger King': 'Fast Food' , 'Safeway':'Grocery Store'}\n", + "x = resturant_types['Burger King']\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Restaurnt not found'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "resturant_types.get('Outback Steakhouse', 'Restaurnt not found')" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -401,6 +643,31 @@ " 3. Google it." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'Burger King'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[37], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m resturant_types [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mOutback Steakhouse\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDelicious!\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 2\u001b[0m resturant_types [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSafeway\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mGrocery!\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mresturant_types\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpop\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mBurger King\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4\u001b[0m resturant_types\n", + "\u001b[0;31mKeyError\u001b[0m: 'Burger King'" + ] + } + ], + "source": [ + "\n", + "resturant_types ['Outback Steakhouse'] = 'Delicious!'\n", + "resturant_types ['Safeway'] = 'Grocery!'\n", + "resturant_types.pop('Burger King')\n", + "resturant_types" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -413,6 +680,47 @@ "3. Write a for loop that prints the all the restaurants that are of type `'Seafood'`." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Red Lobster\n", + "Safeway\n", + "Outback Steakhouse\n" + ] + } + ], + "source": [ + "for all_keys in resturant_types.keys():\n", + " print(all_keys)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Red Lobster\n", + "Outback Steakhouse\n" + ] + } + ], + "source": [ + "for all_key in resturant_types.keys():\n", + " if len(all_key) > 10 : \n", + " print(all_key)\n" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -439,6 +747,48 @@ "2. Check if a certain key (1 and 7) already exist in your dictionary. If not please return \"Key not found\"." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dic1={1:10, 2:20}\n", + "dic2={3:30, 4:40}\n", + "dic3={5:50, 6:60}\n", + "list_dict = [dic1, dic2, dic3]\n", + "dic_concat ={}\n", + "for i in list_dict:\n", + " for thing in i.items():\n", + " dic_concat.update([thing])\n", + "print(dic_concat)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}\n" + ] + } + ], + "source": [ + "dic1={1:10, 2:20}\n", + "dic2={3:30, 4:40}\n", + "dic3={5:50, 6:60}\n", + "list_dict = [dic1, dic2, dic3]\n", + "dic_concat ={}\n", + "for i in list_dict:\n", + " for thing in i.items():\n", + " dic_concat.update([thing])\n", + "print(dic_concat)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -447,6 +797,26 @@ "3. Write a Python script to generate and print a dictionary that contains a number (between 1 and 10) in the form (x, x*x). (hint: use a for loop)" ] }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}\n" + ] + } + ], + "source": [ + "dic_power={}\n", + "for i in range(1,11):\n", + " dic_power[i] = i*i\n", + "print(dic_power)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -455,6 +825,26 @@ "4. Write a Python program to sum all the items in dictionary." ] }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "385" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(dic_power.values())" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -467,8 +857,21 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Sara': 24, 'Alex': 65, 'Amir': 45, 'Mary': 37, 'Mona': 10}\n" + ] + } + ], + "source": [ + "my_list_name = ['Sara', 'Alex', 'Amir', 'Mary', 'Mona']\n", + "my_list_age = [24, 65, 45, 37, 10]\n", + "\n", + "dict_zip = dict(zip(my_list_name,my_list_age))" + ] } ], "metadata": { @@ -487,7 +890,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/5_Comprehension.ipynb b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/5_Comprehension.ipynb index d483b931..cac573b0 100644 --- a/04_python_practice/01_Python_Basics/Data_Structures_in_Python/5_Comprehension.ipynb +++ b/04_python_practice/01_Python_Basics/Data_Structures_in_Python/5_Comprehension.ipynb @@ -56,14 +56,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.671661Z", "start_time": "2020-07-20T10:02:41.651576Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 25, 81, 1089]\n" + ] + } + ], "source": [ "my_list = [1, 5, 9, 33]\n", "my_squares = []\n", @@ -98,14 +106,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.686882Z", "start_time": "2020-07-20T10:02:41.678813Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 25, 81, 1089]\n" + ] + } + ], "source": [ "my_squares2 = [num ** 2 for num in my_list]\n", "print(my_squares2)" @@ -169,14 +185,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.709097Z", "start_time": "2020-07-20T10:02:41.691082Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n" + ] + } + ], "source": [ "# Standard way. \n", "squares_dict = {}\n", @@ -187,14 +211,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.734676Z", "start_time": "2020-07-20T10:02:41.719505Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n" + ] + } + ], "source": [ "# Dictionary Comprehension way. \n", "squares_dict2 = {num: num ** 2 for num in range(1, 6)}\n", @@ -213,7 +245,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.750956Z", @@ -228,14 +260,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.769524Z", "start_time": "2020-07-20T10:02:41.760226Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'cow': 8, 'chicken': 7, 'horse': 5, 'moose': 4}\n" + ] + } + ], "source": [ "animals_dict = {animal: randint(1, 10) for animal in animals_list}\n", "print(animals_dict)" @@ -259,14 +299,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T10:02:41.778626Z", "start_time": "2020-07-20T10:02:41.772327Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 2, 4, 6, 8)\n" + ] + } + ], "source": [ "my_tuple = tuple(num for num in range(10) if num % 2 == 0)\n", "print(my_tuple)" @@ -313,6 +361,95 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 5, 7, 9]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "odds = []\n", + "for num in range(10):\n", + " if num % 2 != 0: \n", + " odds.append(num)\n", + "odds\n", + "\n", + "odds = [num for num in range(1,10) if num % 2 != 0]\n", + "odds" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1: 1, 2: 8, 3: 27, 4: 64, 5: 125}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cubes = {}\n", + "for num in range(1,6):\n", + " cubes[num] = num ** 3\n", + "cubes" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1: 1, 2: 8, 3: 27, 4: 64, 5: 125}" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cubes = {num : num**3 for num in range(1,6)}\n", + "cubes" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (3743952370.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[32], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m cubes = dict(num : num**3 for num in range(1,6))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "cubes = dict(num : num**3 for num in range(1,6))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -337,7 +474,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "toc": { "base_numbering": 1, diff --git a/04_python_practice/01_Python_Basics/Functions/1_Introduction_to_Functions.ipynb b/04_python_practice/01_Python_Basics/Functions/1_Introduction_to_Functions.ipynb index 1839e655..97f5abdd 100644 --- a/04_python_practice/01_Python_Basics/Functions/1_Introduction_to_Functions.ipynb +++ b/04_python_practice/01_Python_Basics/Functions/1_Introduction_to_Functions.ipynb @@ -107,14 +107,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:11:38.576106Z", "start_time": "2020-07-20T09:11:38.562307Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 2, 4, 6, 8]\n" + ] + } + ], "source": [ "evens = []\n", "for element in range(10):\n", @@ -147,7 +155,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:15:46.399540Z", @@ -175,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:16:02.946707Z", @@ -199,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:21:28.680629Z", @@ -226,14 +234,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:21:31.717342Z", "start_time": "2020-07-20T09:21:31.698946Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens()" ] @@ -248,7 +267,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:22:48.392759Z", @@ -267,14 +286,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:22:49.942329Z", "start_time": "2020-07-20T09:22:49.932672Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens()" ] @@ -312,10 +342,119 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_evens(): \n", + " evens = []\n", + " for element in range(10): \n", + " if element % 2 == 0: \n", + " evens.append(element)\n", + " return evens\n", + "\n", + "get_evens()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5- No, the function has no input parameter.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_evens(): \n", + " evens = []\n", + " for element in range(19): \n", + " if element % 2 == 0: \n", + " evens.append(element)\n", + " return evens\n", + "get_evens()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 3, 6, 9, 12, 15, 18]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def multip(): \n", + " mul_three = [mul for mul in range(19) if mul%3 == 0]\n", + " return mul_three\n", + "multip()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5- No, the function has no input parameter.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def multip(): \n", + " mul_three = []\n", + " for element in range(19): \n", + " if element % 3 == 0: \n", + " mul_three.append(element)\n", + " return mul_three\n", + "multip()" + ] } ], "metadata": { @@ -334,7 +473,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "toc": { "base_numbering": 1, diff --git a/04_python_practice/01_Python_Basics/Functions/2_Function_Definitions.ipynb b/04_python_practice/01_Python_Basics/Functions/2_Function_Definitions.ipynb index 3a566348..c181aa21 100644 --- a/04_python_practice/01_Python_Basics/Functions/2_Function_Definitions.ipynb +++ b/04_python_practice/01_Python_Basics/Functions/2_Function_Definitions.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:26:49.271964Z", @@ -62,53 +62,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:27:26.624940Z", "start_time": "2020-07-20T09:27:26.605458Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens(5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:27:27.548548Z", "start_time": "2020-07-20T09:27:27.541263Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8, 10, 12]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens(14)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:27:28.455924Z", "start_time": "2020-07-20T09:27:28.445454Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens(20)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "attachments": {}, "cell_type": "markdown", @@ -119,7 +145,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:28:35.407153Z", @@ -138,56 +164,100 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:28:36.104646Z", "start_time": "2020-07-20T09:28:36.095517Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:28:36.873361Z", "start_time": "2020-07-20T09:28:36.863387Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens(5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:28:37.600905Z", "start_time": "2020-07-20T09:28:37.592055Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8, 10, 12]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens(14)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:28:39.837956Z", "start_time": "2020-07-20T09:28:39.826578Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_evens(20)" ] @@ -210,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:31.208255Z", @@ -229,84 +299,150 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:31.906113Z", "start_time": "2020-07-20T09:30:31.896764Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:32.524560Z", "start_time": "2020-07-20T09:30:32.515367Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:33.057469Z", "start_time": "2020-07-20T09:30:33.048503Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(5, 2)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:35.546057Z", "start_time": "2020-07-20T09:30:35.536691Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(10, 2)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:36.130405Z", "start_time": "2020-07-20T09:30:36.120706Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 3, 6, 9]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(10, 3)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:30:37.929394Z", "start_time": "2020-07-20T09:30:37.920488Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(100, 10)" ] @@ -329,7 +465,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:31:41.488324Z", @@ -348,14 +484,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:31:43.459870Z", "start_time": "2020-07-20T09:31:43.448414Z" } }, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "non-default argument follows default argument (3489222303.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[25], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m def get_multiples(n=5, divisor):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" + ] + } + ], "source": [ "def get_multiples(n=5, divisor): \n", " multiples_lst = []\n", @@ -399,6 +544,37 @@ "2. Call the function with a couple of different arguments. Before you call it, though, write down what you expect it to output. Do the function calls output what you expect? Why or why not?" ] }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_evens(n=6):\n", + " evens = list(num for num in range(n) if num %2 == 0)\n", + " return evens\n", + "\n", + "get_evens(8)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1- yes, because the function has input parameter. " + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -411,6 +587,56 @@ "3. Leaving the function with a default value of `20`, call it with arguments of `5` and `10`. What do you expect the output to be? Run it, and either verify your results or figure out why the output differed from what you expected. " ] }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_evens(n=10):\n", + " evens = list(num for num in range(n) if num %2 == 0)\n", + " return evens\n", + "\n", + "get_evens()\n", + "get_evens(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_evens(n=20):\n", + " evens = list(num for num in range(n) if num %2 == 0)\n", + " return evens\n", + "\n", + "get_evens(5)\n", + "get_evens(10)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -422,8 +648,45 @@ "2. Now, call `get_multiples(100, 10)`. Again, note what you expect this call will output before calling it. Is the output the same as in `1`. Why or why not?" ] }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_multiples(n=100, divisor=20):\n", + " multi_num = list(mul for mul in range(n) if mul % divisor == 0)\n", + " return multi_num\n", + "\n", + "get_multiples()\n", + "get_multiples(100,10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2- Because the input parameter has changed. " + ] + }, { "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { "cell_type": "markdown", "metadata": {}, "source": [ @@ -461,12 +724,90 @@ "2. Verify your answers by typing these into cells, or figure out why you don't see the results you expected. " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1- functions 2, 3, 5, 6 are valid. " + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "non-default argument follows default argument (3695065915.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[49], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m def my_func1(var1='Hello', var2):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" + ] + } + ], + "source": [ + "def my_func1(var1='Hello', var2): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def my_func2(var1, var2='Hello'): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "def my_func3(var1, var2=35): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "non-default argument follows default argument (2217873552.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[53], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m def my_func4(var1=35, var2):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" + ] + } + ], + "source": [ + "def my_func4(var1=35, var2): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "def my_func5(var1=35, var2='Hello'): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "def my_func6(var1, var2): \n", + " pass" + ] } ], "metadata": { diff --git a/04_python_practice/01_Python_Basics/Functions/3_Calling_Functions.ipynb b/04_python_practice/01_Python_Basics/Functions/3_Calling_Functions.ipynb index 242f9598..7b812d14 100644 --- a/04_python_practice/01_Python_Basics/Functions/3_Calling_Functions.ipynb +++ b/04_python_practice/01_Python_Basics/Functions/3_Calling_Functions.ipynb @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:54:35.730820Z", @@ -67,56 +67,98 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:54:35.755520Z", "start_time": "2020-07-20T09:54:35.733785Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(5, 2) # All arguments passed by position." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:54:35.774128Z", "start_time": "2020-07-20T09:54:35.760441Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(n=5, divisor=2) # All arguments passed by keyword." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:54:35.792238Z", "start_time": "2020-07-20T09:54:35.780602Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 3, 6, 9]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "get_multiples(10, divisor=3) # Okay mix of positional and keyword arguments." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-07-20T09:54:35.809623Z", "start_time": "2020-07-20T09:54:35.795527Z" } }, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "positional argument follows keyword argument (3694115901.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[7], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m get_multiples(n=10, 3) # Not okay mix of positional and keyword arguments.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" + ] + } + ], "source": [ "get_multiples(n=10, 3) # Not okay mix of positional and keyword arguments." ] @@ -166,6 +208,132 @@ " \n", "3. Verify your answers by typing these into cells, or figure out why you don't see the results you expected. " ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1.1- passes argument by postion\n", + "1.2- passses argument by keywords\n", + "\n", + "2. function calls 1, 3, 4, 6 are valid. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 13]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_multiples(26, 13)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "positional argument follows keyword argument (3994683612.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[10], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m get_multiples(n=26, 13)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" + ] + } + ], + "source": [ + "get_multiples(n=26, 13)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 13]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_multiples(26, divisor=13)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 13]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_multiples(n=26, divisor=13)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "positional argument follows keyword argument (718035730.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[13], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m get_multiples(n=48, 8)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" + ] + } + ], + "source": [ + " get_multiples(n=48, 8)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 10, 20, 30, 40]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_multiples(50, divisor=10)" + ] } ], "metadata": { @@ -184,7 +352,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.9.6" }, "toc": { "base_numbering": 1, diff --git a/04_python_practice/01_Python_Basics/Functions/4_Functions_Challenge.ipynb b/04_python_practice/01_Python_Basics/Functions/4_Functions_Challenge.ipynb index 50179976..a81041b7 100644 --- a/04_python_practice/01_Python_Basics/Functions/4_Functions_Challenge.ipynb +++ b/04_python_practice/01_Python_Basics/Functions/4_Functions_Challenge.ipynb @@ -79,6 +79,61 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def factorial(n):\n", + " if n>1: \n", + " return factorial(n-1) * n\n", + " else: \n", + " return 1\n", + "\n", + "factorial(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is not a prime number\n" + ] + } + ], + "source": [ + "def prim(n): \n", + " mul = [num for num in range(1, n+1) if n%num == 0]\n", + " if len(mul) > 2:\n", + " return False\n", + " else:\n", + " return True\n", + "\n", + "\n", + "if prim(9):\n", + " print('The number is a prime number')\n", + "else:\n", + " print('The number is not a prime number')\n", + " " + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -107,12 +162,209 @@ ] }, { - "attachments": {}, - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### Extra Challenge\n", - "\n" + "def word_count(str_input):\n", + " return len(str_input.split())\n", + "\n", + "x = 'Today is a rainy cold witer day.'\n", + "word_count(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def word_count(str_input, delim=' '):\n", + " return len(str_input.split(delim))\n", + "\n", + "x = 'Today is a rainy cold winter day.'\n", + "y = \"As&an&example,&let's&say&my &tax &info &is\"\n", + "word_count(y, '&')" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5, 2, 1, 5, 4, 6, 4]" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def character_counter_words(str_input, delim= ' '):\n", + " return [len(itm) for itm in str_input.split(delim)]\n", + "\n", + "character_counter_words(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is not a prime number.\n" + ] + } + ], + "source": [ + "def prim(n): \n", + " mul = [num for num in range(1, n+1) if n%num == 0]\n", + " if len(mul) > 2:\n", + " return False\n", + " else:\n", + " return n\n", + "\n", + "\n", + "if prim(9):\n", + " print('The number is a prime number.')\n", + "else:\n", + " print('The number is not a prime number.')\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 5, 7, 11, 13, 17, 19]\n" + ] + } + ], + "source": [ + "n = 21\n", + "print( [prim(itm) for itm in range(1,n) if prim(itm)] ) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'divisor is zero.'" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def divis(lst, divi):\n", + " if divi != 0:\n", + " result = ['No' if itm % param else 'Yes' for itm in my_lst ] \n", + " return result\n", + " return ('divisor is zero.')\n", + "\n", + "\n", + "divisor =0\n", + "my_lst = [20, 76, 80, 58, 90, 34, 45, 71]\n", + "divis(my_lst, divisor)" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['sure', 'rule', 'share', 'sale']" + ] + }, + "execution_count": 149, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def word_end_char(lst_str, char):\n", + " if lst_str != []: \n", + " return [itm for itm in lst_str if itm.endswith(char)]\n", + " return 'you enterned nothing.'\n", + " \n", + "\n", + "inp_string = ['I', 'am', 'in', 'love', 'with', 'Python']\n", + "inp_string = ['sure','today', 'rule', 'share', 'sale', '']\n", + "char = 'e'\n", + "word_end_char(inp_string,char)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1]" + ] + }, + "execution_count": 159, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def substring(lst_str, word):\n", + " if lst_str != [] and word!=[]: \n", + " return [i for i, itm in enumerate(lst_str) if word in itm]\n", + " return 'your String or substing is nothing'\n", + "\n", + "\n", + "\n", + "inp_string = ['This', 'is', 'an' , 'example']\n", + "my_word = 'is'\n", + "substring(inp_string, my_word)\n" ] }, { @@ -120,6 +372,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### Extra Challenge\n", + "\n", + "\n", "### Extra Challenge\n", "\n", "1. Let's build a calculator for figuring out how much I owe in taxes (and by calculator, I mean function). Write a function that takes in a list of tuples, where each tuple contains two values, as well as an income to compute the taxes on (so your function should accept two arguments). For the tuple list, the first value of a tuple will be an income upper bound, and the second will be a tax rate for all income up to the given income bound. You need to build a calculator that will calculate the tax for all income up to each income bound (if it goes up that high) for the given tax rate. You can assume that the list of tuples will be sorted by the income bound (e.g. the first value in the tuple), such that the lowest income bound will be first, and the highest last (see below for an example). \n", @@ -148,7 +403,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -162,7 +417,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.8" + "version": "3.9.6" }, "toc": { "base_numbering": 1, @@ -205,11 +460,6 @@ "_Feature" ], "window_display": false - }, - "vscode": { - "interpreter": { - "hash": "be7dd07ef673f1c542ac4f7690ce2e5f650b2bc66c3cbf4a3f66e2fce72bd812" - } } }, "nbformat": 4, diff --git a/04_python_practice/01_Python_Basics/requirements.txt b/04_python_practice/01_Python_Basics/requirements.txt index 947b0c8e..b85de8c5 100644 --- a/04_python_practice/01_Python_Basics/requirements.txt +++ b/04_python_practice/01_Python_Basics/requirements.txt @@ -1 +1,3 @@ jupyterlab==3.6.3 + +