├── bonus.ipynb ├── challange.ipynb ├── exercises.ipynb ├── main.ipynb └── readme.md /bonus.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Bonus\n", 8 | "\n", 9 | "Create a program that would receive a two dimensional matrix as one row at a time and print their accumulative summation after each row, and the final accumulative summation" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "3\n", 22 | "6\n", 23 | "9\n", 24 | "18\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "matrix = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]\n", 30 | "total_sum = 0\n", 31 | "\n", 32 | "for row in matrix:\n", 33 | " current_sum = sum(row)\n", 34 | " print(current_sum)\n", 35 | " total_sum = total_sum + current_sum\n", 36 | "\n", 37 | "print(total_sum)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [] 46 | } 47 | ], 48 | "metadata": { 49 | "kernelspec": { 50 | "display_name": "Python 3", 51 | "language": "python", 52 | "name": "python3" 53 | }, 54 | "language_info": { 55 | "codemirror_mode": { 56 | "name": "ipython", 57 | "version": 3 58 | }, 59 | "file_extension": ".py", 60 | "mimetype": "text/x-python", 61 | "name": "python", 62 | "nbconvert_exporter": "python", 63 | "pygments_lexer": "ipython3", 64 | "version": "3.13.0" 65 | } 66 | }, 67 | "nbformat": 4, 68 | "nbformat_minor": 2 69 | } 70 | -------------------------------------------------------------------------------- /challange.ipynb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KareimGazer/session-2-python/4ed093b68032ca60d4fdb0f067eaaa820cdb5ee6/challange.ipynb -------------------------------------------------------------------------------- /exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Create 2 types of python loops" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "numbers = [1, 2, 3, 4, 5]\n", 24 | "\n", 25 | "for number in numbers:\n", 26 | " print(number)\n", 27 | "\n", 28 | "for x in range(5):\n", 29 | " for y in range(5):\n", 30 | " print(x, y)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "### Create 1 incremental value" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "total = 0" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "### Check on the change for the value of the variable in the two different loops and conclude the differences\n" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "numbers = [1, 2, 3, 4, 5]\n", 63 | "\n", 64 | "total = 0\n", 65 | "for number in numbers:\n", 66 | " total = total + 1\n", 67 | " print(number)\n", 68 | "\n", 69 | "print(total)\n", 70 | "\n", 71 | "total = 0\n", 72 | "for x in range(5):\n", 73 | " for y in range(5):\n", 74 | " total = total + 1\n", 75 | " print(x, y)\n", 76 | "\n", 77 | "print(total)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "### Create python lists" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "numbers = [1, 2, 3, 4, 5]" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### Loop over the list while printing the list’s elements " 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "for number in numbers:\n", 110 | " print(number)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "### Apply changes to the list’s elements and print them once again" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "for i in range(len(numbers)):\n", 127 | " numbers[i] = numbers[i] * 2\n", 128 | "\n", 129 | "print(numbers)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "### Duplicate a Syntax error and fix it " 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "### Add condition to print only even numbers inside the loops" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "numbers = [1, 2, 3, 4, 5]\n", 160 | "\n", 161 | "for number in numbers:\n", 162 | " if number % 2 == 0:\n", 163 | " print(number)" 164 | ] 165 | } 166 | ], 167 | "metadata": { 168 | "kernelspec": { 169 | "display_name": "Python 3", 170 | "language": "python", 171 | "name": "python3" 172 | }, 173 | "language_info": { 174 | "name": "python", 175 | "version": "3.12.1" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 2 180 | } 181 | -------------------------------------------------------------------------------- /main.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lists, Loops, and errors\n", 8 | "\n", 9 | "## Lists\n", 10 | "lists are a way to store many values in the same variable" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "my_list = [10, 20, 30, 40, 50] # list of numbers of int type\n", 20 | "print(my_list)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "lists are some kind of python objects, they got methods that we can use to operate on them" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "# add the number 60 to the end of the list\n", 37 | "my_list.append(60)\n", 38 | "print(my_list)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# remove last element\n", 48 | "last_element = my_list.pop()\n", 49 | "print(last_element)\n", 50 | "print(my_list)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "# find and remove the number 30 from the list\n", 60 | "my_list.remove(30)\n", 61 | "print(my_list)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "print(my_list)\n", 71 | "my_list.insert(2, 30)\n", 72 | "print(my_list)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "lists have length that indicates how many elements are in the array" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "my_list_length = len(my_list)\n", 89 | "print(my_list_length)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "lists are indexed starting from `0` up to the `length of the list - 1`\n", 97 | "this means that the first element in the list in number 0 and the last element is number `len(my_list) - 1`" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# so to select the first element\n", 107 | "first_element = my_list[0]\n", 108 | "print(first_element)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "# to select the last element\n", 118 | "index_of_last_element = len(my_list) - 1\n", 119 | "last_element = my_list[index_of_last_element]\n", 120 | "print(last_element)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "there is also another way which is to count from the end of the list using negative numbers" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "# for example to get the first element in the array\n", 137 | "last_element = my_list[-1]\n", 138 | "second_to_last_element = my_list[-2] # and so on\n", 139 | "print(last_element, second_to_last_element)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "### range indexing\n", 147 | "\n", 148 | "we can choose a subset of our list\n", 149 | "this will give us a smaller list" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "smaller_list = my_list[1:4]\n", 159 | "print(smaller_list)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# we can change the values inside our list using indexing\n", 169 | "my_list[0] = 5\n", 170 | "print(my_list)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [ 179 | "# we can combine (concatenate) lists using + operator\n", 180 | "combined_list = my_list + smaller_list\n", 181 | "print(combined_list)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "## Loops\n", 189 | "\n", 190 | "- we use loops to loop over the values of our list\n", 191 | "- we store each value of our list in a variable and run the code inside the loop\n", 192 | "- the variable number will take the value of each item in the list\n", 193 | "- for each value in the list assign it to the variable number and run the code inside the loop" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "for number in [1, 2, 3, 4]:\n", 203 | " print(number)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "# we can assign the list to a variable\n", 213 | "numbers = [1, 2, 3, 4]\n", 214 | "for number in numbers:\n", 215 | " print(number * 2)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [ 224 | "# we can use a for loop to help us instruct our turtle to draw a rectangle\n", 225 | "# instead of writing code for the four sides of rectangle\n", 226 | "import turtle\n", 227 | "amy = turtle.Turtle()\n", 228 | "for side in [1, 2, 3, 4]:\n", 229 | " amy.forward(100)\n", 230 | " amy.left(90)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [ 239 | "# we can generate a list of numbers that we can loop over\n", 240 | "# the range is a generator that generates lists of numbers for us that we can loop over\n", 241 | "for number in range(1, 5): # [1, 2, 3, 4], 5 not included\n", 242 | " print(number)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": null, 248 | "metadata": {}, 249 | "outputs": [], 250 | "source": [ 251 | "# we can use a step of two to get a list of all even numbers from 0 to 10\n", 252 | "for number in range(0, 11, 2): # [0, 2, 4, 6, 8, 10], 11 not included\n", 253 | " print(number)" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "there is a concise syntax for range to do this and generate our list which is called a list comprehension" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "numbers = [number for number in range(0, 11, 2)] # a list of all even numbers from 0 to 10\n", 270 | "print(numbers)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "# we can also generate a list of all odd numbers from 0 to 10\n", 280 | "numbers = [number for number in range(1, 11, 2)] # a list of all odd numbers from 0 to 10\n", 281 | "print(numbers)" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "### Matrix\n", 289 | "\n", 290 | "a matrix is a list of lists (two dimensional list)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": null, 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [ 299 | "lst = [1, \"hello\", True]\n", 300 | "matrix = [lst, [], []]\n", 301 | "matrix" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [ 310 | "matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]\n", 311 | "print(matrix)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "for row in matrix:\n", 321 | " print(row)" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "### Nested for loops\n", 329 | "\n", 330 | "for loops can be treaded as commands to be injected (nested) into other for loops" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "numbers = [1, 2, 3, 4]\n", 340 | "\n", 341 | "for x in numbers:\n", 342 | " for y in numbers:\n", 343 | " print(x, y)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "Indentation can be tricky.\n", 351 | "One place that errors can sneak into your code is in the indentation of loops. If you find that some lines of code are running more times (or fewer) than you expect, take a careful look at how your loops are indented." 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 1, 357 | "metadata": {}, 358 | "outputs": [], 359 | "source": [ 360 | "for a in [1, 2, 3]:\n", 361 | " # code here will run 3 times.\n", 362 | " for b in [4, 5, 6]:\n", 363 | " # code in here will run 9 times\n", 364 | " pass\n", 365 | " # but code here will run only 3 times!" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 2, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "30\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "# what will be the total sum?\n", 383 | "total_sum = 0\n", 384 | "for x in [1, 1, 1]:\n", 385 | " total_sum = total_sum + x\n", 386 | "\n", 387 | " for y in [2, 2, 2, 2]:\n", 388 | " total_sum = total_sum + y\n", 389 | " \n", 390 | " total_sum = total_sum + x\n", 391 | "\n", 392 | "print(total_sum)" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "## Errors\n" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "### Syntax Errors\n", 407 | "\n", 408 | "Syntax errors are like spelling errors. This is when you write something that doesn't make sense in the grammar of the language. In Python, if you write a `)` with no opening `(` before it, that's a syntax error. If you leave the `:` off of a for loop, that's also a syntax error.\n", 409 | "Syntax errors are like spelling errors. When you're learning a new language, you'll probably make a lot of them. But once you're used to the language, you'll make very few." 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 3, 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "ename": "SyntaxError", 419 | "evalue": "unmatched ')' (32552717.py, line 1)", 420 | "output_type": "error", 421 | "traceback": [ 422 | "\u001b[1;36m Cell \u001b[1;32mIn[3], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m print)(\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unmatched ')'\n" 423 | ] 424 | } 425 | ], 426 | "source": [ 427 | "print)(" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "### Usage Errors\n", 435 | "\n", 436 | "This is when you ask the computer to do something that doesn't make sense. For instance, `alison.forward(\"orange\")` is a usage error. It's completely grammatical, but the forward method expects a number, a distance in pixels, not a string. The turtle can't go forward by \"orange\" pixels.\n", 437 | "\n", 438 | "Python's `NameError`, `ZeroDivisionError`, and `TypeError` messages usually mean an error of this sort." 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "### Logic errors are like asking for the wrong thing\n", 446 | "\n", 447 | "Finally, the third kind of errors are logic errors. The program runs fine — it doesn't crash — but it doesn't do what you wanted it to do, because what you wrote isn't what you meant. These are what people mean when they say, \"The computer doesn't do what you want it to; it only does what you tell it to do.\"" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": null, 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [ 456 | "# sum all numbers from 1 to 10 - locgical error: a numer is missing\n", 457 | "\n", 458 | "total_sum = 0\n", 459 | "for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]:\n", 460 | " total_sum = total_sum + x\n", 461 | "print(total_sum)" 462 | ] 463 | } 464 | ], 465 | "metadata": { 466 | "kernelspec": { 467 | "display_name": "Python 3", 468 | "language": "python", 469 | "name": "python3" 470 | }, 471 | "language_info": { 472 | "codemirror_mode": { 473 | "name": "ipython", 474 | "version": 3 475 | }, 476 | "file_extension": ".py", 477 | "mimetype": "text/x-python", 478 | "name": "python", 479 | "nbconvert_exporter": "python", 480 | "pygments_lexer": "ipython3", 481 | "version": "3.13.0" 482 | } 483 | }, 484 | "nbformat": 4, 485 | "nbformat_minor": 2 486 | } 487 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Session-2 Python 2 | 3 | ## Content 4 | 5 | - Lists 6 | - methods 7 | - indexing 8 | - concatenation 9 | - nested lists 10 | - list comprehension 11 | - slicing 12 | - range 13 | - Loops 14 | - for loop 15 | - nested loops 16 | - *while loop* 17 | - *break and continue* 18 | - pass 19 | - *else* 20 | - Errors 21 | - syntax errors 22 | - usage errors 23 | - logic errors 24 | - *exception handling* --------------------------------------------------------------------------------