|
20 | 20 | "- Algebraic Functions\n",
|
21 | 21 | "- Degrees and Radians\n",
|
22 | 22 | "- Rounding\n",
|
23 |
| - "- Exponents and Logarithms" |
| 23 | + "- Exponents and Logarithms\n", |
| 24 | + "- Sorting" |
24 | 25 | ]
|
25 | 26 | },
|
26 | 27 | {
|
|
549 | 550 | },
|
550 | 551 | {
|
551 | 552 | "cell_type": "code",
|
552 |
| - "execution_count": null, |
553 |
| - "metadata": {}, |
554 |
| - "outputs": [], |
| 553 | + "execution_count": 1, |
| 554 | + "metadata": {}, |
| 555 | + "outputs": [ |
| 556 | + { |
| 557 | + "name": "stdout", |
| 558 | + "output_type": "stream", |
| 559 | + "text": [ |
| 560 | + "Single Value input\n", |
| 561 | + "\t(math) e^3.2 = 24.532530197109352\n", |
| 562 | + "\t(numpy) e^3.2 = 24.532530197109352\n", |
| 563 | + "\n", |
| 564 | + "Multiple Value Input (array/list)\n", |
| 565 | + "\t(numpy) e^[1.2, 2.2, 3.2] = [ 3.32011692 9.0250135 24.5325302 ]\n" |
| 566 | + ] |
| 567 | + } |
| 568 | + ], |
555 | 569 | "source": [
|
556 | 570 | "import math\n",
|
557 | 571 | "import numpy as np\n",
|
|
615 | 629 | "print(f\"\\t\\tlog (np.log) = {np.log(input_list)}\")"
|
616 | 630 | ]
|
617 | 631 | },
|
| 632 | + { |
| 633 | + "cell_type": "markdown", |
| 634 | + "metadata": {}, |
| 635 | + "source": [ |
| 636 | + "## Sorting\n", |
| 637 | + "\n", |
| 638 | + "Python includes functions to organize and sort lists of numbers or strings\n", |
| 639 | + "\n", |
| 640 | + "Functions to sort lists and arrays are part of the standard Python library as well as `numpy`" |
| 641 | + ] |
| 642 | + }, |
| 643 | + { |
| 644 | + "cell_type": "markdown", |
| 645 | + "metadata": {}, |
| 646 | + "source": [ |
| 647 | + "### Sorting in Python and Numpy\n", |
| 648 | + "Python has two similar sorting functions: [`list.sort()` and `sorted()`](https://docs.python.org/3/howto/sorting.html)\n", |
| 649 | + "\n", |
| 650 | + "`list.sort()` reorganizes a numerical or string list in-place, but returns `None`, while `sorted()` creates a new copy of the list with the sorted elements.\n", |
| 651 | + "\n", |
| 652 | + "```\n", |
| 653 | + "lst = [2, 1, 3]\n", |
| 654 | + "lst.sort()\n", |
| 655 | + "print(lst)\n", |
| 656 | + ">> [1, 2, 3]\n", |
| 657 | + "print(lst.sort())\n", |
| 658 | + ">> None\n", |
| 659 | + "\n", |
| 660 | + "lst = [2, 1, 3]\n", |
| 661 | + "new_sorted_list = sorted(lst)\n", |
| 662 | + "print(lst)\n", |
| 663 | + ">> [2, 1, 3]\n", |
| 664 | + "print(new_sorted_list)\n", |
| 665 | + ">> [1, 2, 3]\n", |
| 666 | + "```\n", |
| 667 | + "\n", |
| 668 | + "The `numpy.sort()` function works like the `sorted` function, which creates and returns a sorted array of the original list\n", |
| 669 | + "\n", |
| 670 | + "```\n", |
| 671 | + "import numpy as np\n", |
| 672 | + "lst = [2, 1, 3]\n", |
| 673 | + "new_sorted_list = np.sort(lst)\n", |
| 674 | + "print(lst)\n", |
| 675 | + ">> [2, 1, 3]\n", |
| 676 | + "print(new_sorted_list)\n", |
| 677 | + ">> [1 2 3]\n", |
| 678 | + "```" |
| 679 | + ] |
| 680 | + }, |
| 681 | + { |
| 682 | + "cell_type": "code", |
| 683 | + "execution_count": 2, |
| 684 | + "metadata": {}, |
| 685 | + "outputs": [ |
| 686 | + { |
| 687 | + "name": "stdout", |
| 688 | + "output_type": "stream", |
| 689 | + "text": [ |
| 690 | + "List of Strings\n", |
| 691 | + "\t(standard library) = ['cheese', 'egg', 'mango', 'milkshake', 'taco', 'tea']\n", |
| 692 | + "\t(numpy) = ['cheese' 'egg' 'mango' 'milkshake' 'taco' 'tea']\n", |
| 693 | + "\n", |
| 694 | + "List of Numbers\n", |
| 695 | + "\t(standard library) = [-1.2, 0.2, 3.14, 10, 49, 100]\n", |
| 696 | + "\t(numpy) = [ -1.2 0.2 3.14 10. 49. 100. ]\n" |
| 697 | + ] |
| 698 | + } |
| 699 | + ], |
| 700 | + "source": [ |
| 701 | + "import numpy as np\n", |
| 702 | + "\n", |
| 703 | + "input_values = [\"mango\", \"egg\", \"taco\", \"tea\", \"milkshake\", \"cheese\"]\n", |
| 704 | + "print(\"List of Strings\")\n", |
| 705 | + "print(f\"\\t(standard library) = {sorted(input_values)}\")\n", |
| 706 | + "print(f\"\\t(numpy) = {np.sort(input_values)}\")\n", |
| 707 | + "\n", |
| 708 | + "print(\"\\nList of Numbers\")\n", |
| 709 | + "input_values = [3.14, -1.2, 0.2, 10, 100, 49]\n", |
| 710 | + "print(f\"\\t(standard library) = {sorted(input_values)}\")\n", |
| 711 | + "print(f\"\\t(numpy) = {np.sort(input_values)}\")" |
| 712 | + ] |
| 713 | + }, |
618 | 714 | {
|
619 | 715 | "cell_type": "markdown",
|
620 | 716 | "metadata": {},
|
|
626 | 722 | "- [Built-in Python Functions](https://docs.python.org/3/library/functions.html)\n",
|
627 | 723 | "- [Standard Built-In Python `math` module](https://docs.python.org/3/library/math.html)\n",
|
628 | 724 | "- [Python `cmath` when working with complex mathematics](https://docs.python.org/3/library/cmath.html)\n",
|
629 |
| - "- Additional [mathematical `numpy` functions](https://numpy.org/doc/stable/reference/routines.math.html)" |
| 725 | + "- [Additional mathematical `numpy` functions](https://numpy.org/doc/stable/reference/routines.math.html)\n", |
| 726 | + "- [Python Sorting Techniques](https://docs.python.org/3/howto/sorting.html)" |
630 | 727 | ]
|
631 | 728 | }
|
632 | 729 | ],
|
|
0 commit comments