|
6 | 6 | "source": [ |
7 | 7 | "## Data Structures\n", |
8 | 8 | "\n", |
9 | | - "A \"Data Structure\" is just a fancy way of saying, \"I put some information in a container.\" Conveniently, it makes me seem more professional and fancy. Python has a few types of Data Structures, but the most common ones are lists, tuples, and dictionaries. These structures let you store and organize data in different ways, making it easier to work with.\n", |
| 9 | + "A \"Data Structure\" is just a fancy way of saying, \"I put some information in a container.\" Conveniently, it makes me seem more professional and fancy. Python has a few types of Data Structures, but the most common ones are lists, tuples, and dictionaries. These structures let you store and organize data in different ways, making it easier to work with. Sometimes, Data Structures are called iterables (because you can loop through them), but not all iterables are Data Structures. For example, strings are also iterables, but they are not Data Structures. You can think of Data Structures as containers that hold data, like a backpack or Batman's belt.\n", |
10 | 10 | "\n", |
11 | 11 | "### Lists\n", |
12 | 12 | "Lists are ordered collections of items that can be changed (mutable). You can think of them as a shopping list where you can add, remove, or change items. Lists are defined using square brackets `[]`, and you can access items using their index (starting from 0), like this: `list[n]`. Each item in a list is called an \"element.\" You can have as many elements as you can fit in your array without running out of memory. By the way, this is called a \"Stack Overflow\" error, and it happens when you try to use more memory than your computer has available in the RAM. It's like trying to fit two gallons of milk into a one-gallon milk jug, it just doesn't work.\n", |
|
326 | 326 | "print(\"Number of times 'John' appears:\", students.count(\"John\")) # how many times john is in the list" |
327 | 327 | ], |
328 | 328 | "id": "1f326905b82a434b" |
| 329 | + }, |
| 330 | + { |
| 331 | + "metadata": {}, |
| 332 | + "cell_type": "markdown", |
| 333 | + "source": [ |
| 334 | + "### list slicing\n", |
| 335 | + "\n", |
| 336 | + "list slicing is not what happens when i bring my shopping list to the kitchen and cut it up into pieces. List slicing is when you take little slices or portions of a list, and create a new list out of them. You can slice a list using the syntax `list[start:end:step]`, where `start` is the index to start from, `end` is the index to stop at (not inclusive), and `step` is the number of indices to skip. If you leave out any of these parameters, Python will use default values: `start=0`, `end=len(list)`, and `step=1`. run the following code block for an example:" |
| 337 | + ], |
| 338 | + "id": "8166b0d9385c6c56" |
| 339 | + }, |
| 340 | + { |
| 341 | + "metadata": { |
| 342 | + "ExecuteTime": { |
| 343 | + "end_time": "2025-08-20T14:46:43.215891Z", |
| 344 | + "start_time": "2025-08-20T14:46:43.210656Z" |
| 345 | + } |
| 346 | + }, |
| 347 | + "cell_type": "code", |
| 348 | + "source": [ |
| 349 | + "my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", |
| 350 | + "print(my_list[2:5]) # (elements at indices 2, 3, and 4)\n", |
| 351 | + "print(my_list[:5]) # (elements from the start to index 4)\n", |
| 352 | + "print(my_list[5:]) # (elements from index 5 to the end)\n", |
| 353 | + "print(my_list[::2]) # (every second element)" |
| 354 | + ], |
| 355 | + "id": "cc45dbab94ec0955", |
| 356 | + "outputs": [ |
| 357 | + { |
| 358 | + "name": "stdout", |
| 359 | + "output_type": "stream", |
| 360 | + "text": [ |
| 361 | + "[3, 4, 5]\n", |
| 362 | + "[1, 2, 3, 4, 5]\n", |
| 363 | + "[6, 7, 8, 9, 10]\n", |
| 364 | + "[1, 3, 5, 7, 9]\n" |
| 365 | + ] |
| 366 | + } |
| 367 | + ], |
| 368 | + "execution_count": 3 |
| 369 | + }, |
| 370 | + { |
| 371 | + "metadata": {}, |
| 372 | + "cell_type": "markdown", |
| 373 | + "source": "funnily enough, you can also slice strings like this! lets say you wanted to get the first 5 characters of a string, so you could do `string[:5]`. You can still use negative indices like this, so let's try writing some code that gets the _last five_ characters of a string:", |
| 374 | + "id": "6ad9f3564906017d" |
| 375 | + }, |
| 376 | + { |
| 377 | + "metadata": {}, |
| 378 | + "cell_type": "code", |
| 379 | + "outputs": [], |
| 380 | + "execution_count": null, |
| 381 | + "source": [ |
| 382 | + "my_string = \"Hello, world!\"\n", |
| 383 | + "print(my_string[:5])\n", |
| 384 | + "# now print out the last five characters of the string\n", |
| 385 | + "# your code here!" |
| 386 | + ], |
| 387 | + "id": "522a670b3425c220" |
| 388 | + }, |
| 389 | + { |
| 390 | + "metadata": {}, |
| 391 | + "cell_type": "markdown", |
| 392 | + "source": "## pratical exercise : TO WRITE", |
| 393 | + "id": "95449c903c6b4dc1" |
329 | 394 | } |
330 | 395 | ], |
331 | 396 | "metadata": { |
|
0 commit comments