├── 01_Python_Introduction_examples ├── 001 print Hello world!.ipynb ├── 002 add two numbers.ipynb ├── 003 find the square root.ipynb ├── 004 calculate the area of a triangle.ipynb ├── 005 solve quadratic equation.ipynb ├── 006 swap two variables.ipynb ├── 007 generate a random number.ipynb ├── 008 convert kilometers to miles.ipynb ├── 009 convert celsius to fahrenheit.ipynb ├── 010 print output without a newline.ipynb └── README.md ├── 02_Python_Datatypes_examples ├── 001 add two matrices.ipynb ├── 002_transpose a matrix.ipynb ├── 003_multiply_two_matrices.ipynb ├── 004_check_whether_a_string_is_palindrome_or_not.ipynb ├── 005_remove_punctuations_from_a_string.ipynb ├── 006_sort_words_in_alphabetic_order.ipynb ├── 007_illustrate_different_set_operations.ipynb ├── 008_count_the_number_of_each_vowel.ipynb ├── 009_merge_two_dictionaries.ipynb ├── 010_access_index_of_a_list_using_for_loop.ipynb ├── 011_make_a_flattened_list_from_nested_list.ipynb ├── 012_slice_lists.ipynb ├── 013_sort_a_dictionary_by_value.ipynb ├── 014_check_if_a_list_is_empty.ipynb ├── 015_concatenate_two_lists.ipynb ├── 016_check_if_a_key_is_already_present_in_a_dictionary.ipynb ├── 017_split_a_list_into_evenly_sized_chunks.ipynb ├── 018_parse_a_string_to_a_float_or_int.ipynb ├── 019_print_colored_text_to_the_terminal.ipynb ├── 020_convert_string_to_datetime.ipynb ├── 021_get_the_last_element_of_the_list.ipynb ├── 022_get_a_substring_of_a_string.ipynb ├── 023_randomly_select_an_element_from_the_list.ipynb ├── 024_check_if_a_String_is_a_number_float.ipynb ├── 025_count_the_occurrence_of_an_item_in_a_list.ipynb ├── 026_delete_an_element_from_a_dictionary.ipynb ├── 027_create_a_long_multiline_string.ipynb ├── 028_convert_two_lists_into_a_dictionary.ipynb ├── 029_trim_whitespace_from_a_string.ipynb ├── 030_differentiate_between_del_remove_and_pop_on_a_list.ipynb ├── 031_iterate_through_two_lists_in_parallel.ipynb ├── 032_count_the_number_of_digits_present_in_a_number.ipynb ├── 033_check_if_two_strings_are_anagram.ipynb ├── 034_capitalize_the_first_character_of_a_string.ipynb ├── 035_compute_all_the_permutation_of_the_string.ipynb ├── 036_count_the_number_of_occurrence_of_a_character_in_string.ipynb ├── 037_remove_duplicate_element_from_a_list.ipynb ├── Python_Matrices_and_NumPy_Arrays.ipynb ├── README.md └── img │ ├── color.png │ ├── l6_2.png │ ├── matrix1.png │ └── matrix2.png ├── 03_Python_Flow_Control_examples ├── 001_check_if_a_number_is_positive_negative_or_zero.ipynb ├── 002_check_if_a_number_is_odd_or_even.ipynb ├── 003_check_leap_year.ipynb ├── 004_find_the_argest_among_three_numbers.ipynb ├── 005_check_prime_number.ipynb ├── 006_print_all_prime_numbers_in_an_interval.ipynb ├── 007_find_the_factorial_of_a_number.ipynb ├── 008_display_the_multiplication_table.ipynb ├── 009_print_the_fibonacci_sequence.ipynb ├── 010_check_armstrong_number.ipynb ├── 011_find_armstrong_number_in_an_interval.ipynb ├── 012_find_the_sum_of_natural_numbers.ipynb ├── 013_create_pyramid_patterns.ipynb ├── 014_iterate_over_dictionaries_using_for_loop.ipynb ├── 015_reverse_a_number.ipynb ├── 016_compute_the_power_of_a_number.ipynb └── README.md ├── 04_Python_Functions_examples ├── 001_display_powers_of_2_using_anonymous_function.ipynb ├── 002_find_numbers_divisible_by_another_number.ipynb ├── 003_convert_decimal_to_binary_octal_and_hexadecimal.ipynb ├── 004_find_the_ASCII_value_of_a_character.ipynb ├── 005_find_HCF_or_GCD.ipynb ├── 006_find_LCM.ipynb ├── 007_find_the_factors_of_a_number.ipynb ├── 008_make_a_simple_calculator.ipynb ├── 009_shuffle_a_deck_of_cards.ipynb ├── 010_display_calendar.ipynb ├── 011_display_fibonacci_sequence_using_recursion.ipynb ├── 012_find_the_sum_of_natural_numbers_using_recursion.ipynb ├── 013_find_the_factorial_of_number_using_recursion.ipynb ├── 014_convert_decimal_number_to_binary_using_recursive_function.ipynb ├── 015_return_multiple_values_from_a_function.ipynb └── README.md ├── 05_Python_Files_examples ├── 001_merge_mails │ ├── 001_merge_mails.ipynb │ ├── 01_names.txt │ ├── 02_body.txt │ ├── Arthur.txt │ ├── Brandon.txt │ ├── Charles.txt │ ├── David.txt │ └── Ethan.txt ├── 002_find_the_size_of_a_image │ ├── 002_find_the_size_of_a_image.ipynb │ ├── jupyter.jpg │ └── jupyter.png ├── 003_find_the_hash_of_a_file │ ├── 003_find_the_hash_of_a_file.ipynb │ └── track1.mp3 ├── 004_safely_create_a_nested_directory │ ├── 004_safely_create_a_nested_directory.ipynb │ └── img │ │ └── subDir.png ├── 005_catch_multiple_python_exceptions_in_one_line │ └── 005_catch_multiple_python_exceptions_in_one_line.ipynb ├── 006_copy_a_file │ └── 006_copy_a_file.ipynb ├── 007_read_a_file_line_by_line_into_a_list │ ├── 007_read_a_file_line_by_line_into_a_list.ipynb │ ├── vaccine_data.csv │ └── vaccine_data.txt ├── 008_append_to_a_file │ ├── 008_append_to_a_file.ipynb │ └── vaccine_data.txt ├── 009_extract_extension_from_the_file_name │ ├── 009_extract_extension_from_the_file_name.ipynb │ └── vaccine_data.txt ├── 010_get_the_file_name_from_the_file_path │ └── 010_get_the_file_name_from_the_file_path.ipynb ├── 011_get_line_count_of_a_file │ └── 011_get_line_count_of_a_file.ipynb ├── 012_find_all_files_with_txt_extension_present_inside_a_directory │ └── 012_find_all_files_with_txt_extension_present_inside_a_directory.ipynb ├── 013_get_file_creation_and_modification_date │ ├── 013_get_file_creation_and_modification_date.ipynb │ └── Digital_Clock.py ├── 014_get_the_full_path_of_the_current_working_directory │ └── 014_get_the_full_path_of_the_current_working_directory.ipynb ├── 015_check_the_file_size │ ├── 015_check_the_file_size.ipynb │ └── vaccine_data.txt └── README.md ├── 06_Python_Object_Oriented_examples ├── 001 Get the Class Name of an Instance.ipynb ├── 002 Differentiate Between type() and isinstance().ipynb └── README.md ├── 07_Python_Advanced_examples ├── 001 Measure the Elapsed Time in Python.ipynb ├── 002 Represent enum.ipynb ├── 003 Create a Countdown Timer.ipynb ├── 004 Convert Bytes to a String.ipynb └── README.md ├── LICENSE ├── README.md └── img └── dnld_rep.png /01_Python_Introduction_examples/001 print Hello world!.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Print Hello world!\n", 17 | "\n", 18 | "A simple program that displays “Hello, World!”. It's often used to illustrate the syntax of the language.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[How to Get Started With Python?](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)**\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Hello, world!\n" 36 | ] 37 | }, 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "'\\n>>Output/Runtime Test Cases\\n \\nHello, world!\\n'" 42 | ] 43 | }, 44 | "execution_count": 1, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "# Here is source code of the Python Program to print Hello, world!\n", 51 | "# The program output is also shown below.\n", 52 | "\n", 53 | "print('Hello world!')\n", 54 | "\n", 55 | "'''\n", 56 | ">>Output/Runtime Test Cases:\n", 57 | " \n", 58 | "Hello world!\n", 59 | "'''" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "**Explanation:**\n", 67 | " \n", 68 | "In this program, we have used the built-in **`print()`** function to print the string **`Hello, world!`** on our screen.\n", 69 | "\n", 70 | "By the way, a **[string](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String.ipynb)** is a sequence of characters. In Python, strings are enclosed inside single quotes, double quotes, or triple quotes. " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [] 79 | } 80 | ], 81 | "metadata": { 82 | "hide_input": false, 83 | "kernelspec": { 84 | "display_name": "Python 3", 85 | "language": "python", 86 | "name": "python3" 87 | }, 88 | "language_info": { 89 | "codemirror_mode": { 90 | "name": "ipython", 91 | "version": 3 92 | }, 93 | "file_extension": ".py", 94 | "mimetype": "text/x-python", 95 | "name": "python", 96 | "nbconvert_exporter": "python", 97 | "pygments_lexer": "ipython3", 98 | "version": "3.8.8" 99 | }, 100 | "toc": { 101 | "base_numbering": 1, 102 | "nav_menu": {}, 103 | "number_sections": true, 104 | "sideBar": true, 105 | "skip_h1_title": false, 106 | "title_cell": "Table of Contents", 107 | "title_sidebar": "Contents", 108 | "toc_cell": false, 109 | "toc_position": {}, 110 | "toc_section_display": true, 111 | "toc_window_display": false 112 | }, 113 | "varInspector": { 114 | "cols": { 115 | "lenName": 16, 116 | "lenType": 16, 117 | "lenVar": 40 118 | }, 119 | "kernels_config": { 120 | "python": { 121 | "delete_cmd_postfix": "", 122 | "delete_cmd_prefix": "del ", 123 | "library": "var_list.py", 124 | "varRefreshCmd": "print(var_dic_list())" 125 | }, 126 | "r": { 127 | "delete_cmd_postfix": ") ", 128 | "delete_cmd_prefix": "rm(", 129 | "library": "var_list.r", 130 | "varRefreshCmd": "cat(var_dic_list()) " 131 | } 132 | }, 133 | "types_to_exclude": [ 134 | "module", 135 | "function", 136 | "builtin_function_or_method", 137 | "instance", 138 | "_Feature" 139 | ], 140 | "window_display": false 141 | } 142 | }, 143 | "nbformat": 4, 144 | "nbformat_minor": 4 145 | } 146 | -------------------------------------------------------------------------------- /01_Python_Introduction_examples/005 solve quadratic equation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Solve Quadratic Equation\n", 17 | "\n", 18 | "This program computes roots of a quadratic equation when coefficients **`a`**, **`b`** and **`c`** are known.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Data Types](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)**\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**\n", 24 | "* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "The standard form of a quadratic equation is:\n", 32 | "\n", 33 | "```python\n", 34 | "ax**2 + bx + c = 0, where\n", 35 | "a, b and c are real numbers and\n", 36 | "a ≠ 0\n", 37 | "```\n", 38 | "\n", 39 | "The solutions of this quadratic equation is given by:\n", 40 | "\n", 41 | "```python\n", 42 | "(-b ± (b ** 2 - 4 * a * c) ** 0.5) / 2 * a\n", 43 | "```" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 1, 49 | "metadata": { 50 | "ExecuteTime": { 51 | "end_time": "2021-07-23T17:42:33.561284Z", 52 | "start_time": "2021-07-23T17:42:33.499761Z" 53 | } 54 | }, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "The solution are (-3+0j) and (-2+0j)\n" 61 | ] 62 | }, 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "'\\n>>Output/Runtime Test Cases:\\n \\nEnter a: 1\\nEnter b: 5\\nEnter c: 6\\nThe solutions are (-3+0j) and (-2+0j)\\n'" 67 | ] 68 | }, 69 | "execution_count": 1, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "#Example: Solve the quadratic equation ax**2 + bx + c = 0\n", 76 | "\n", 77 | "# import complex math module\n", 78 | "import cmath\n", 79 | "\n", 80 | "a = 1\n", 81 | "b = 5\n", 82 | "c = 6\n", 83 | "\n", 84 | "# calculate the discriminant\n", 85 | "d = (b**2) - (4*a*c)\n", 86 | "\n", 87 | "# find two solutions\n", 88 | "sol1 = (-b-cmath.sqrt(d))/(2*a)\n", 89 | "sol2 = (-b+cmath.sqrt(d))/(2*a)\n", 90 | "\n", 91 | "print('The solution are {0} and {1}'.format(sol1,sol2))\n", 92 | "\n", 93 | "'''\n", 94 | ">>Output/Runtime Test Cases:\n", 95 | " \n", 96 | "Enter a: 1\n", 97 | "Enter b: 5\n", 98 | "Enter c: 6\n", 99 | "The solutions are (-3+0j) and (-2+0j)\n", 100 | "'''" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "**Explanation:**\n", 108 | " \n", 109 | "We have imported the **`cmath`** module to perform complex square root. First, we calculate the discriminant and then find the two solutions of the quadratic equation.\n", 110 | "\n", 111 | "You can change the value of **`a`**, **`b`** and **`c`** in the above program and test this program." 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [] 120 | } 121 | ], 122 | "metadata": { 123 | "hide_input": false, 124 | "kernelspec": { 125 | "display_name": "Python 3", 126 | "language": "python", 127 | "name": "python3" 128 | }, 129 | "language_info": { 130 | "codemirror_mode": { 131 | "name": "ipython", 132 | "version": 3 133 | }, 134 | "file_extension": ".py", 135 | "mimetype": "text/x-python", 136 | "name": "python", 137 | "nbconvert_exporter": "python", 138 | "pygments_lexer": "ipython3", 139 | "version": "3.8.8" 140 | }, 141 | "toc": { 142 | "base_numbering": 1, 143 | "nav_menu": {}, 144 | "number_sections": true, 145 | "sideBar": true, 146 | "skip_h1_title": false, 147 | "title_cell": "Table of Contents", 148 | "title_sidebar": "Contents", 149 | "toc_cell": false, 150 | "toc_position": {}, 151 | "toc_section_display": true, 152 | "toc_window_display": false 153 | }, 154 | "varInspector": { 155 | "cols": { 156 | "lenName": 16, 157 | "lenType": 16, 158 | "lenVar": 40 159 | }, 160 | "kernels_config": { 161 | "python": { 162 | "delete_cmd_postfix": "", 163 | "delete_cmd_prefix": "del ", 164 | "library": "var_list.py", 165 | "varRefreshCmd": "print(var_dic_list())" 166 | }, 167 | "r": { 168 | "delete_cmd_postfix": ") ", 169 | "delete_cmd_prefix": "rm(", 170 | "library": "var_list.r", 171 | "varRefreshCmd": "cat(var_dic_list()) " 172 | } 173 | }, 174 | "types_to_exclude": [ 175 | "module", 176 | "function", 177 | "builtin_function_or_method", 178 | "instance", 179 | "_Feature" 180 | ], 181 | "window_display": false 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 4 186 | } 187 | -------------------------------------------------------------------------------- /01_Python_Introduction_examples/007 generate a random number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Generate a Random Number\n", 17 | "\n", 18 | "In this example, you will learn to generate a random number in Python.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**\n", 24 | "* **[Python Random Module](https://github.com/milaan9/04_Python_Functions/blob/main/008_Python_Function_random_Module.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "To generate random number in Python, **`randint()`** function is used. This function is defined in **[random module](https://docs.python.org/3.6/library/random.html)**." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "metadata": { 38 | "ExecuteTime": { 39 | "end_time": "2021-07-23T17:59:37.868403Z", 40 | "start_time": "2021-07-23T17:59:37.831298Z" 41 | } 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "8\n" 49 | ] 50 | }, 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "'\\n>>Output/Runtime Test Cases:\\n \\n4\\n'" 55 | ] 56 | }, 57 | "execution_count": 1, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "#Example: Generate a random number between 0 and 9\n", 64 | "\n", 65 | "# importing the random module\n", 66 | "import random\n", 67 | "\n", 68 | "print(random.randint(0,9))\n", 69 | "\n", 70 | "'''\n", 71 | ">>Output/Runtime Test Cases:\n", 72 | " \n", 73 | "8\n", 74 | "'''" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | ">**Note:** that we may get different output because this program generates random number in range 0 and 9. The syntax of this function is:\n", 82 | "\n", 83 | "```python\n", 84 | "random.randint(a,b)\n", 85 | "```\n", 86 | "\n", 87 | "This returns a number **`N`** in the inclusive range **`[a,b]`**, meaning **`a <= N <= b`**, where the endpoints are included in the range." 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "hide_input": false, 100 | "kernelspec": { 101 | "display_name": "Python 3", 102 | "language": "python", 103 | "name": "python3" 104 | }, 105 | "language_info": { 106 | "codemirror_mode": { 107 | "name": "ipython", 108 | "version": 3 109 | }, 110 | "file_extension": ".py", 111 | "mimetype": "text/x-python", 112 | "name": "python", 113 | "nbconvert_exporter": "python", 114 | "pygments_lexer": "ipython3", 115 | "version": "3.8.8" 116 | }, 117 | "toc": { 118 | "base_numbering": 1, 119 | "nav_menu": {}, 120 | "number_sections": true, 121 | "sideBar": true, 122 | "skip_h1_title": false, 123 | "title_cell": "Table of Contents", 124 | "title_sidebar": "Contents", 125 | "toc_cell": false, 126 | "toc_position": {}, 127 | "toc_section_display": true, 128 | "toc_window_display": false 129 | }, 130 | "varInspector": { 131 | "cols": { 132 | "lenName": 16, 133 | "lenType": 16, 134 | "lenVar": 40 135 | }, 136 | "kernels_config": { 137 | "python": { 138 | "delete_cmd_postfix": "", 139 | "delete_cmd_prefix": "del ", 140 | "library": "var_list.py", 141 | "varRefreshCmd": "print(var_dic_list())" 142 | }, 143 | "r": { 144 | "delete_cmd_postfix": ") ", 145 | "delete_cmd_prefix": "rm(", 146 | "library": "var_list.r", 147 | "varRefreshCmd": "cat(var_dic_list()) " 148 | } 149 | }, 150 | "types_to_exclude": [ 151 | "module", 152 | "function", 153 | "builtin_function_or_method", 154 | "instance", 155 | "_Feature" 156 | ], 157 | "window_display": false 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 4 162 | } 163 | -------------------------------------------------------------------------------- /01_Python_Introduction_examples/008 convert kilometers to miles.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Convert Kilometers to Miles\n", 17 | "\n", 18 | "In this example, we'll learn to convert kilometers to miles and display it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Data Types](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)**\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**\n", 24 | "* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-07-24T05:51:58.684048Z", 33 | "start_time": "2021-07-24T05:51:56.568823Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "Enter value in kilometers: 2.5\n", 42 | "2.50 kilometers is equal to 1.55 miles\n" 43 | ] 44 | }, 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "'\\n>>Output/Runtime Test Cases:\\n \\nEnter value in kilometers: 2.5\\n3.50 kilometers is equal to 2.17 miles\\n'" 49 | ] 50 | }, 51 | "execution_count": 1, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "#Example: Kilometers to Miles\n", 58 | "\n", 59 | "# Taking kilometers input from the user\n", 60 | "kilometers = float(input(\"Enter value in kilometers: \"))\n", 61 | "\n", 62 | "# conversion factor\n", 63 | "conv_fac = 0.621371\n", 64 | "\n", 65 | "# calculate miles\n", 66 | "miles = kilometers * conv_fac\n", 67 | "print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))\n", 68 | "\n", 69 | "'''\n", 70 | ">>Output/Runtime Test Cases:\n", 71 | " \n", 72 | "Enter value in kilometers: 2.5\n", 73 | "3.50 kilometers is equal to 1.55 miles\n", 74 | "'''" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "**Explanation:** \n", 82 | "\n", 83 | "Here, the user is asked to enter kilometers. This value is stored in the **`kilometers`** variable.\n", 84 | "\n", 85 | "Since 1 kilometer is equal to 0.621371 miles, we can get the equivalent miles by multiplying kilometers with this factor." 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "**Your turn:** Modify the above program to convert miles to kilometers using the following formula and run it.\n", 93 | "```python\n", 94 | "kilometers = miles / conv_fac\n", 95 | "```" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [] 104 | } 105 | ], 106 | "metadata": { 107 | "hide_input": false, 108 | "kernelspec": { 109 | "display_name": "Python 3", 110 | "language": "python", 111 | "name": "python3" 112 | }, 113 | "language_info": { 114 | "codemirror_mode": { 115 | "name": "ipython", 116 | "version": 3 117 | }, 118 | "file_extension": ".py", 119 | "mimetype": "text/x-python", 120 | "name": "python", 121 | "nbconvert_exporter": "python", 122 | "pygments_lexer": "ipython3", 123 | "version": "3.8.8" 124 | }, 125 | "toc": { 126 | "base_numbering": 1, 127 | "nav_menu": {}, 128 | "number_sections": true, 129 | "sideBar": true, 130 | "skip_h1_title": false, 131 | "title_cell": "Table of Contents", 132 | "title_sidebar": "Contents", 133 | "toc_cell": false, 134 | "toc_position": {}, 135 | "toc_section_display": true, 136 | "toc_window_display": false 137 | }, 138 | "varInspector": { 139 | "cols": { 140 | "lenName": 16, 141 | "lenType": 16, 142 | "lenVar": 40 143 | }, 144 | "kernels_config": { 145 | "python": { 146 | "delete_cmd_postfix": "", 147 | "delete_cmd_prefix": "del ", 148 | "library": "var_list.py", 149 | "varRefreshCmd": "print(var_dic_list())" 150 | }, 151 | "r": { 152 | "delete_cmd_postfix": ") ", 153 | "delete_cmd_prefix": "rm(", 154 | "library": "var_list.r", 155 | "varRefreshCmd": "cat(var_dic_list()) " 156 | } 157 | }, 158 | "types_to_exclude": [ 159 | "module", 160 | "function", 161 | "builtin_function_or_method", 162 | "instance", 163 | "_Feature" 164 | ], 165 | "window_display": false 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 4 170 | } 171 | -------------------------------------------------------------------------------- /01_Python_Introduction_examples/009 convert celsius to fahrenheit.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Convert Celsius To Fahrenheit\n", 17 | "\n", 18 | "In this program, you'll learn to convert Celsuis to Fahrenheit and display it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Data Types](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)**\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**\n", 24 | "* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "In the program below, we take a temperature in degree Celsius and convert it into degree Fahrenheit. They are related by the formula:\n", 32 | "```python\n", 33 | "celsius * 1.8 = fahrenheit - 32\n", 34 | "```" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 1, 40 | "metadata": { 41 | "ExecuteTime": { 42 | "end_time": "2021-07-24T05:55:09.109385Z", 43 | "start_time": "2021-07-24T05:55:09.088878Z" 44 | } 45 | }, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "37.5 degree Celsius is equal to 99.5 degree Fahrenheit\n" 52 | ] 53 | }, 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "'\\n>>Output/Runtime Test Cases:\\n \\n37.5 degree Celsius is equal to 99.5 degree Fahrenheit\\n'" 58 | ] 59 | }, 60 | "execution_count": 1, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "#Example: Convert temperature in celsius to fahrenheit\n", 67 | "\n", 68 | "# change this value for a different result\n", 69 | "celsius = 37.5\n", 70 | "\n", 71 | "# calculate fahrenheit\n", 72 | "fahrenheit = (celsius * 1.8) + 32\n", 73 | "print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))\n", 74 | "\n", 75 | "\n", 76 | "'''\n", 77 | ">>Output/Runtime Test Cases:\n", 78 | " \n", 79 | "37.5 degree Celsius is equal to 99.5 degree Fahrenheit\n", 80 | "'''" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "**Your turn:** We encourage you to create a Python program to convert Fahrenheit to Celsius on your own using the following formula:\n", 88 | "\n", 89 | "```python\n", 90 | "celsius = (fahrenheit - 32) / 1.8\n", 91 | "```" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [] 100 | } 101 | ], 102 | "metadata": { 103 | "hide_input": false, 104 | "kernelspec": { 105 | "display_name": "Python 3", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.8.8" 120 | }, 121 | "toc": { 122 | "base_numbering": 1, 123 | "nav_menu": {}, 124 | "number_sections": true, 125 | "sideBar": true, 126 | "skip_h1_title": false, 127 | "title_cell": "Table of Contents", 128 | "title_sidebar": "Contents", 129 | "toc_cell": false, 130 | "toc_position": {}, 131 | "toc_section_display": true, 132 | "toc_window_display": false 133 | }, 134 | "varInspector": { 135 | "cols": { 136 | "lenName": 16, 137 | "lenType": 16, 138 | "lenVar": 40 139 | }, 140 | "kernels_config": { 141 | "python": { 142 | "delete_cmd_postfix": "", 143 | "delete_cmd_prefix": "del ", 144 | "library": "var_list.py", 145 | "varRefreshCmd": "print(var_dic_list())" 146 | }, 147 | "r": { 148 | "delete_cmd_postfix": ") ", 149 | "delete_cmd_prefix": "rm(", 150 | "library": "var_list.r", 151 | "varRefreshCmd": "cat(var_dic_list()) " 152 | } 153 | }, 154 | "types_to_exclude": [ 155 | "module", 156 | "function", 157 | "builtin_function_or_method", 158 | "instance", 159 | "_Feature" 160 | ], 161 | "window_display": false 162 | } 163 | }, 164 | "nbformat": 4, 165 | "nbformat_minor": 4 166 | } 167 | -------------------------------------------------------------------------------- /01_Python_Introduction_examples/010 print output without a newline.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Print Output Without a Newline\n", 17 | "\n", 18 | "In this example, you will learn to print output without a newline.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-07-24T06:05:08.335584Z", 31 | "start_time": "2021-07-24T06:05:08.310198Z" 32 | } 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "Python\n", 40 | "learning is fun.\n", 41 | "\n", 42 | "Python learning is fun.\n" 43 | ] 44 | }, 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "'\\n>>Output/Runtime Test Cases:\\n \\nPython\\nlearning is fun.\\n\\nPython learning is fun.\\n'" 49 | ] 50 | }, 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "#Example: Using `end` keyword print each statement on a new line\n", 58 | "\n", 59 | "print(\"Python\")\n", 60 | "print(\"learning is fun.\")\n", 61 | "\n", 62 | "# new line\n", 63 | "print()\n", 64 | "\n", 65 | "# print both the statements on a single line\n", 66 | "print(\"Python\", end=\" \")\n", 67 | "print(\"learning is fun.\")\n", 68 | "\n", 69 | "'''\n", 70 | ">>Output/Runtime Test Cases:\n", 71 | " \n", 72 | "Python\n", 73 | "learning is fun.\n", 74 | "\n", 75 | "Python learning is fun.\n", 76 | "'''" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "**Your turn:** \n", 84 | "\n", 85 | "Using the end keyword, you can append a string at the end of the print text. In the above example, we have passed a space with end, which adds a space at the end of the line and concatenates the content of the next print statement." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [] 94 | } 95 | ], 96 | "metadata": { 97 | "hide_input": false, 98 | "kernelspec": { 99 | "display_name": "Python 3", 100 | "language": "python", 101 | "name": "python3" 102 | }, 103 | "language_info": { 104 | "codemirror_mode": { 105 | "name": "ipython", 106 | "version": 3 107 | }, 108 | "file_extension": ".py", 109 | "mimetype": "text/x-python", 110 | "name": "python", 111 | "nbconvert_exporter": "python", 112 | "pygments_lexer": "ipython3", 113 | "version": "3.8.8" 114 | }, 115 | "toc": { 116 | "base_numbering": 1, 117 | "nav_menu": {}, 118 | "number_sections": true, 119 | "sideBar": true, 120 | "skip_h1_title": false, 121 | "title_cell": "Table of Contents", 122 | "title_sidebar": "Contents", 123 | "toc_cell": false, 124 | "toc_position": {}, 125 | "toc_section_display": true, 126 | "toc_window_display": false 127 | }, 128 | "varInspector": { 129 | "cols": { 130 | "lenName": 16, 131 | "lenType": 16, 132 | "lenVar": 40 133 | }, 134 | "kernels_config": { 135 | "python": { 136 | "delete_cmd_postfix": "", 137 | "delete_cmd_prefix": "del ", 138 | "library": "var_list.py", 139 | "varRefreshCmd": "print(var_dic_list())" 140 | }, 141 | "r": { 142 | "delete_cmd_postfix": ") ", 143 | "delete_cmd_prefix": "rm(", 144 | "library": "var_list.r", 145 | "varRefreshCmd": "cat(var_dic_list()) " 146 | } 147 | }, 148 | "types_to_exclude": [ 149 | "module", 150 | "function", 151 | "builtin_function_or_method", 152 | "instance", 153 | "_Feature" 154 | ], 155 | "window_display": false 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 4 160 | } 161 | -------------------------------------------------------------------------------- /01_Python_Introduction_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 01 Python Introduction Examples 9 | 10 | In this class, you'll find examples related to python introduction with a detailed explanations. 11 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/005_remove_punctuations_from_a_string.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Remove Punctuations From a String\n", 17 | "\n", 18 | "This program removes all punctuations from a string. We will check each character of the string using for loop. If the character is a punctuation, empty string is assigned to it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python for Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/005_Python_for_Loop.ipynb)**\n", 23 | "* **[Python Strings](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String.ipynb)**\n", 24 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "Sometimes, we may wish to break a sentence into a list of words.\n", 32 | "\n", 33 | "In such cases, we may first want to clean up the string and remove all the punctuation marks. For example." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": { 40 | "ExecuteTime": { 41 | "end_time": "2021-07-30T06:53:39.135824Z", 42 | "start_time": "2021-07-30T06:53:39.104583Z" 43 | }, 44 | "scrolled": true 45 | }, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "Hello how are you Hope doing well\n" 52 | ] 53 | }, 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "'\\n>>Output/Runtime Test Cases:\\n \\nHello how are you Hope doing well\\n'" 58 | ] 59 | }, 60 | "execution_count": 1, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "# Example 1: remove all punctuations from a string\n", 67 | "\n", 68 | "# Define punctuation\n", 69 | "punctuations = '''!()-[]{};:'\"\\,<>./?@#$%^&*_~'''\n", 70 | "\n", 71 | "my_str = \"Hello!!!, how are you? -Hope doing well.\"\n", 72 | "\n", 73 | "# To take input from the user\n", 74 | "# my_str = input(\"Enter a string: \")\n", 75 | "\n", 76 | "# Remove punctuation from the string\n", 77 | "no_punct = \"\"\n", 78 | "for char in my_str:\n", 79 | " if char not in punctuations:\n", 80 | " no_punct = no_punct + char\n", 81 | "\n", 82 | "# Display the unpunctuated string\n", 83 | "print(no_punct)\n", 84 | "\n", 85 | "'''\n", 86 | ">>Output/Runtime Test Cases:\n", 87 | " \n", 88 | "Hello how are you Hope doing well\n", 89 | "'''" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "**Explanation:** \n", 97 | "\n", 98 | "In this program, we first define a string of punctuations. Then, we iterate over the provided string using a **`for`** loop.\n", 99 | "\n", 100 | "In each iteration, we check if the character is a punctuation mark or not using the membership test. We have an empty string to which we add (concatenate) the character if it is not punctuation. Finally, we display the cleaned up string." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [] 109 | } 110 | ], 111 | "metadata": { 112 | "hide_input": false, 113 | "kernelspec": { 114 | "display_name": "Python 3", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.8.8" 129 | }, 130 | "toc": { 131 | "base_numbering": 1, 132 | "nav_menu": {}, 133 | "number_sections": true, 134 | "sideBar": true, 135 | "skip_h1_title": false, 136 | "title_cell": "Table of Contents", 137 | "title_sidebar": "Contents", 138 | "toc_cell": false, 139 | "toc_position": {}, 140 | "toc_section_display": true, 141 | "toc_window_display": false 142 | }, 143 | "varInspector": { 144 | "cols": { 145 | "lenName": 16, 146 | "lenType": 16, 147 | "lenVar": 40 148 | }, 149 | "kernels_config": { 150 | "python": { 151 | "delete_cmd_postfix": "", 152 | "delete_cmd_prefix": "del ", 153 | "library": "var_list.py", 154 | "varRefreshCmd": "print(var_dic_list())" 155 | }, 156 | "r": { 157 | "delete_cmd_postfix": ") ", 158 | "delete_cmd_prefix": "rm(", 159 | "library": "var_list.r", 160 | "varRefreshCmd": "cat(var_dic_list()) " 161 | } 162 | }, 163 | "types_to_exclude": [ 164 | "module", 165 | "function", 166 | "builtin_function_or_method", 167 | "instance", 168 | "_Feature" 169 | ], 170 | "window_display": false 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 4 175 | } 176 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/006_sort_words_in_alphabetic_order.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Sort Words in Alphabetic Order\n", 17 | "\n", 18 | "In this program, you'll learn to sort the words in alphabetic order using for loop and display it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python for Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/005_Python_for_Loop.ipynb)**\n", 23 | "* **[Python Strings](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String.ipynb)**\n", 24 | "* **[String Methods](https://github.com/milaan9/02_Python_Datatypes/tree/main/002_Python_String_Methods)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "In this example, we illustrate how words can be sorted lexicographically (alphabetic order)." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "metadata": { 38 | "ExecuteTime": { 39 | "end_time": "2021-07-30T07:03:46.933961Z", 40 | "start_time": "2021-07-30T07:03:46.904665Z" 41 | }, 42 | "scrolled": true 43 | }, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "The sorted words are:\n", 50 | "brown\n", 51 | "dog\n", 52 | "fox\n", 53 | "jumps\n", 54 | "lazy\n", 55 | "over\n", 56 | "quick\n", 57 | "the\n", 58 | "the\n" 59 | ] 60 | }, 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "'\\n>>Output/Runtime Test Cases:\\n \\nHello how are you Hope doing well\\n'" 65 | ] 66 | }, 67 | "execution_count": 1, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# Example 1: sort alphabetically the words form a string provided by the user\n", 74 | "\n", 75 | "my_str = \"The quick brown fox jumps over the lazy dog\"\n", 76 | "\n", 77 | "# To take input from the user\n", 78 | "#my_str = input(\"Enter a string: \")\n", 79 | "\n", 80 | "# breakdown the string into a list of words\n", 81 | "words = [word.lower() for word in my_str.split()]\n", 82 | "\n", 83 | "# sort the list\n", 84 | "words.sort()\n", 85 | "\n", 86 | "# display the sorted words\n", 87 | "\n", 88 | "print(\"The sorted words are:\")\n", 89 | "for word in words:\n", 90 | " print(word)\n", 91 | "\n", 92 | "'''\n", 93 | ">>Output/Runtime Test Cases:\n", 94 | " \n", 95 | "Hello how are you Hope doing well\n", 96 | "'''" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | ">**Note:** To test the program, change the value of **`my_str`**.\n", 104 | "\n", 105 | "**Explanation:** \n", 106 | "\n", 107 | "In this program, we store the string to be sorted in **`my_str`**. Using the **[split() method](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String_Methods/038_Python_String_split%28%29.ipynb)** the string is converted into a list of words. The **`split()`** method splits the string at whitespaces.\n", 108 | "\n", 109 | "The list of words is then sorted using the **[sort() method](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List_Methods/009_Python_List_sort%28%29.ipynb)**, and all the words are displayed." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [] 118 | } 119 | ], 120 | "metadata": { 121 | "hide_input": false, 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.8.8" 138 | }, 139 | "toc": { 140 | "base_numbering": 1, 141 | "nav_menu": {}, 142 | "number_sections": true, 143 | "sideBar": true, 144 | "skip_h1_title": false, 145 | "title_cell": "Table of Contents", 146 | "title_sidebar": "Contents", 147 | "toc_cell": false, 148 | "toc_position": {}, 149 | "toc_section_display": true, 150 | "toc_window_display": false 151 | }, 152 | "varInspector": { 153 | "cols": { 154 | "lenName": 16, 155 | "lenType": 16, 156 | "lenVar": 40 157 | }, 158 | "kernels_config": { 159 | "python": { 160 | "delete_cmd_postfix": "", 161 | "delete_cmd_prefix": "del ", 162 | "library": "var_list.py", 163 | "varRefreshCmd": "print(var_dic_list())" 164 | }, 165 | "r": { 166 | "delete_cmd_postfix": ") ", 167 | "delete_cmd_prefix": "rm(", 168 | "library": "var_list.r", 169 | "varRefreshCmd": "cat(var_dic_list()) " 170 | } 171 | }, 172 | "types_to_exclude": [ 173 | "module", 174 | "function", 175 | "builtin_function_or_method", 176 | "instance", 177 | "_Feature" 178 | ], 179 | "window_display": false 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 4 184 | } 185 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/007_illustrate_different_set_operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Illustrate Different Set Operations\n", 17 | "\n", 18 | "In this example, we have defined two set variables and we have performed different set operations: union, intersection, difference and symmetric difference.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Sets](https://github.com/milaan9/02_Python_Datatypes/blob/main/006_Python_Sets.ipynb)**\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Python offers a datatype called set whose elements must be unique. It can be used to perform different set operations like union, intersection, difference and symmetric difference." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": { 37 | "ExecuteTime": { 38 | "end_time": "2021-07-30T07:09:05.521304Z", 39 | "start_time": "2021-07-30T07:09:05.492982Z" 40 | }, 41 | "scrolled": true 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Union of M and N is {0, 1, 2, 3, 4, 5, 6, 7, 8, 10}\n", 49 | "Intersection of M and N is {2, 4}\n", 50 | "Difference of M and N is {0, 8, 10, 6}\n", 51 | "Symmetric difference of M and N is {0, 1, 3, 5, 6, 7, 8, 10}\n" 52 | ] 53 | }, 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "'\\n>>Output/Runtime Test Cases:\\n \\nUnion of M and N is {0, 1, 2, 3, 4, 5, 6, 7, 8, 10}\\nIntersection of M and N is {2, 4}\\nDifference of M and N is {0, 8, 10, 6}\\nSymmetric difference of M and N is {0, 1, 3, 5, 6, 7, 8, 10}\\n'" 58 | ] 59 | }, 60 | "execution_count": 1, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "# Example 1: perform different set operations like in mathematics\n", 67 | "\n", 68 | "# define three sets\n", 69 | "M = {0, 2, 4, 6, 8, 10};\n", 70 | "N = {1, 2, 3, 4, 5, 7};\n", 71 | "\n", 72 | "# set union\n", 73 | "print(\"Union of M and N is\",M | N)\n", 74 | "\n", 75 | "# set intersection\n", 76 | "print(\"Intersection of M and N is\",M & N)\n", 77 | "\n", 78 | "# set difference\n", 79 | "print(\"Difference of M and N is\",M - N)\n", 80 | "\n", 81 | "# set symmetric difference\n", 82 | "print(\"Symmetric difference of M and N is\",M ^ N)\n", 83 | "'''\n", 84 | ">>Output/Runtime Test Cases:\n", 85 | " \n", 86 | "Union of M and N is {0, 1, 2, 3, 4, 5, 6, 7, 8, 10}\n", 87 | "Intersection of M and N is {2, 4}\n", 88 | "Difference of M and N is {0, 8, 10, 6}\n", 89 | "Symmetric difference of M and N is {0, 1, 3, 5, 6, 7, 8, 10}\n", 90 | "'''" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "**Explanation:** \n", 98 | "\n", 99 | "In this program, we take two different sets and perform different set operations on them. This can equivalently done by using set methods." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | } 109 | ], 110 | "metadata": { 111 | "hide_input": false, 112 | "kernelspec": { 113 | "display_name": "Python 3", 114 | "language": "python", 115 | "name": "python3" 116 | }, 117 | "language_info": { 118 | "codemirror_mode": { 119 | "name": "ipython", 120 | "version": 3 121 | }, 122 | "file_extension": ".py", 123 | "mimetype": "text/x-python", 124 | "name": "python", 125 | "nbconvert_exporter": "python", 126 | "pygments_lexer": "ipython3", 127 | "version": "3.8.8" 128 | }, 129 | "toc": { 130 | "base_numbering": 1, 131 | "nav_menu": {}, 132 | "number_sections": true, 133 | "sideBar": true, 134 | "skip_h1_title": false, 135 | "title_cell": "Table of Contents", 136 | "title_sidebar": "Contents", 137 | "toc_cell": false, 138 | "toc_position": {}, 139 | "toc_section_display": true, 140 | "toc_window_display": false 141 | }, 142 | "varInspector": { 143 | "cols": { 144 | "lenName": 16, 145 | "lenType": 16, 146 | "lenVar": 40 147 | }, 148 | "kernels_config": { 149 | "python": { 150 | "delete_cmd_postfix": "", 151 | "delete_cmd_prefix": "del ", 152 | "library": "var_list.py", 153 | "varRefreshCmd": "print(var_dic_list())" 154 | }, 155 | "r": { 156 | "delete_cmd_postfix": ") ", 157 | "delete_cmd_prefix": "rm(", 158 | "library": "var_list.r", 159 | "varRefreshCmd": "cat(var_dic_list()) " 160 | } 161 | }, 162 | "types_to_exclude": [ 163 | "module", 164 | "function", 165 | "builtin_function_or_method", 166 | "instance", 167 | "_Feature" 168 | ], 169 | "window_display": false 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 4 174 | } 175 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/016_check_if_a_key_is_already_present_in_a_dictionary.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Check if a Key is Already Present in a Dictionary\n", 17 | "\n", 18 | "In this example, you will learn to check if a key is present in a dictionary.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/005_Python_Dictionary.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-08-02T06:45:59.759995Z", 31 | "start_time": "2021-08-02T06:45:59.729720Z" 32 | } 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "present\n" 40 | ] 41 | }, 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "'\\n>>Output/Runtime Test Cases:\\n \\npresent\\n'" 46 | ] 47 | }, 48 | "execution_count": 1, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "# Example 1: Using in keyword\n", 55 | "\n", 56 | "my_dict = {1: 'a', 2: 'b', 3: 'c'}\n", 57 | "\n", 58 | "if 2 in my_dict:\n", 59 | " print(\"present\")\n", 60 | "\n", 61 | "'''\n", 62 | ">>Output/Runtime Test Cases:\n", 63 | " \n", 64 | "present\n", 65 | "'''" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "**Explanation:** \n", 73 | "\n", 74 | "Using **`if statement`** and **`in`** keyword, you can check if a key is present in a dictionary.\n", 75 | "\n", 76 | "In the above example, 2 is present in the dictionary as a key; therefore, the output is **`present`**.\n", 77 | "\n", 78 | "You can use **`not in`** if you want to check if a key is not present in the dictionary." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [] 87 | } 88 | ], 89 | "metadata": { 90 | "hide_input": false, 91 | "kernelspec": { 92 | "display_name": "Python 3", 93 | "language": "python", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 3 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython3", 106 | "version": "3.8.8" 107 | }, 108 | "toc": { 109 | "base_numbering": 1, 110 | "nav_menu": {}, 111 | "number_sections": true, 112 | "sideBar": true, 113 | "skip_h1_title": false, 114 | "title_cell": "Table of Contents", 115 | "title_sidebar": "Contents", 116 | "toc_cell": false, 117 | "toc_position": {}, 118 | "toc_section_display": true, 119 | "toc_window_display": false 120 | }, 121 | "varInspector": { 122 | "cols": { 123 | "lenName": 16, 124 | "lenType": 16, 125 | "lenVar": 40 126 | }, 127 | "kernels_config": { 128 | "python": { 129 | "delete_cmd_postfix": "", 130 | "delete_cmd_prefix": "del ", 131 | "library": "var_list.py", 132 | "varRefreshCmd": "print(var_dic_list())" 133 | }, 134 | "r": { 135 | "delete_cmd_postfix": ") ", 136 | "delete_cmd_prefix": "rm(", 137 | "library": "var_list.r", 138 | "varRefreshCmd": "cat(var_dic_list()) " 139 | } 140 | }, 141 | "types_to_exclude": [ 142 | "module", 143 | "function", 144 | "builtin_function_or_method", 145 | "instance", 146 | "_Feature" 147 | ], 148 | "window_display": false 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 4 153 | } 154 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/019_print_colored_text_to_the_terminal.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Print Colored Text to the Terminal\n", 17 | "\n", 18 | "In this example, you will learn to print colored text to the terminal.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Strings](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-08-02T10:43:52.161368Z", 31 | "start_time": "2021-08-02T10:43:52.145745Z" 32 | } 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "\u001b[38;2;6;96;243mPython4DataScience\u001b[0m\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "# Example 1: Using ANSI escape sequences\n", 45 | "\n", 46 | "print('\\x1b[38;2;6;96;243m' + 'Python4DataScience' + '\\x1b[0m')" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "**Explanation:** \n", 54 | "\n", 55 | "The working of the above line of code is shown below:\n", 56 | "\n", 57 | "
\n", 58 | "\n", 59 | "
" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Let's understand the escape code **`\\x1b[38;2;5;86;243m`**.\n", 67 | "\n", 68 | "* **`\\x1b`** calls a function. You can also use **`\\033`** for the same purpose.\n", 69 | "* **`38;2;r;g;b`** helps to set RGB color. **`5;86;243`** are the rgb color for blue (the color of the logo of Programiz).\n", 70 | "* **`m`** is the function name. Here, **`m`** means SGR (Select Graphics Rendition) function.\n", 71 | "\n", 72 | "For more information regarding the ANSI escape code, you can refer to **[ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code)**." 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 2, 78 | "metadata": { 79 | "ExecuteTime": { 80 | "end_time": "2021-08-02T10:43:52.344479Z", 81 | "start_time": "2021-08-02T10:43:52.169183Z" 82 | } 83 | }, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "\u001b[31mPython4DataScience\u001b[0m\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "# Example 2: Using python module termcolor\n", 95 | "\n", 96 | "from termcolor import colored\n", 97 | "\n", 98 | "print(colored('Python4DataScience', 'red'))" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | ">**Note:** First you'll need to install **`termcolor`**.\n", 106 | "\n", 107 | "**Explanation:** \n", 108 | "\n", 109 | "Using the module **[termcolor](https://pypi.org/project/termcolor/)**, you can get the desired output. Also, you can set different styles of the text using this module." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [] 118 | } 119 | ], 120 | "metadata": { 121 | "hide_input": false, 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.8.8" 138 | }, 139 | "toc": { 140 | "base_numbering": 1, 141 | "nav_menu": {}, 142 | "number_sections": true, 143 | "sideBar": true, 144 | "skip_h1_title": false, 145 | "title_cell": "Table of Contents", 146 | "title_sidebar": "Contents", 147 | "toc_cell": false, 148 | "toc_position": {}, 149 | "toc_section_display": true, 150 | "toc_window_display": false 151 | }, 152 | "varInspector": { 153 | "cols": { 154 | "lenName": 16, 155 | "lenType": 16, 156 | "lenVar": 40 157 | }, 158 | "kernels_config": { 159 | "python": { 160 | "delete_cmd_postfix": "", 161 | "delete_cmd_prefix": "del ", 162 | "library": "var_list.py", 163 | "varRefreshCmd": "print(var_dic_list())" 164 | }, 165 | "r": { 166 | "delete_cmd_postfix": ") ", 167 | "delete_cmd_prefix": "rm(", 168 | "library": "var_list.r", 169 | "varRefreshCmd": "cat(var_dic_list()) " 170 | } 171 | }, 172 | "types_to_exclude": [ 173 | "module", 174 | "function", 175 | "builtin_function_or_method", 176 | "instance", 177 | "_Feature" 178 | ], 179 | "window_display": false 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 4 184 | } 185 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/021_get_the_last_element_of_the_list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Get the Last Element of the List\n", 17 | "\n", 18 | "In this example, you will learn to get the last element of this list.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-02T11:02:03.993508Z", 33 | "start_time": "2021-08-02T11:02:03.967144Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "f\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "\"\\n>>Output/Runtime Test Cases:\\n \\n\\n2018-08-09 11:11:00\\n\"" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# Example 1: Using negative indexing\n", 57 | "my_list = ['a', 'b', 'c', 'd', 'e', 'f']\n", 58 | "\n", 59 | "# print the last element\n", 60 | "print(my_list[-1])\n", 61 | "\n", 62 | "'''\n", 63 | ">>Output/Runtime Test Cases:\n", 64 | " \n", 65 | "f\n", 66 | "'''" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "**Explanation:** \n", 74 | "\n", 75 | "When you use negative indexing, the counting starts from 1 not 0 as shown in the figure below.\n", 76 | "\n", 77 | "
\n", 78 | "\n", 79 | "
" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "If you want the first 1st element, you can use **`my_list[-10]`**.\n", 87 | "\n", 88 | "If you want to learn more, please go to **[Python list Negative Indexing](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [] 97 | } 98 | ], 99 | "metadata": { 100 | "hide_input": false, 101 | "kernelspec": { 102 | "display_name": "Python 3", 103 | "language": "python", 104 | "name": "python3" 105 | }, 106 | "language_info": { 107 | "codemirror_mode": { 108 | "name": "ipython", 109 | "version": 3 110 | }, 111 | "file_extension": ".py", 112 | "mimetype": "text/x-python", 113 | "name": "python", 114 | "nbconvert_exporter": "python", 115 | "pygments_lexer": "ipython3", 116 | "version": "3.8.8" 117 | }, 118 | "toc": { 119 | "base_numbering": 1, 120 | "nav_menu": {}, 121 | "number_sections": true, 122 | "sideBar": true, 123 | "skip_h1_title": false, 124 | "title_cell": "Table of Contents", 125 | "title_sidebar": "Contents", 126 | "toc_cell": false, 127 | "toc_position": {}, 128 | "toc_section_display": true, 129 | "toc_window_display": false 130 | }, 131 | "varInspector": { 132 | "cols": { 133 | "lenName": 16, 134 | "lenType": 16, 135 | "lenVar": 40 136 | }, 137 | "kernels_config": { 138 | "python": { 139 | "delete_cmd_postfix": "", 140 | "delete_cmd_prefix": "del ", 141 | "library": "var_list.py", 142 | "varRefreshCmd": "print(var_dic_list())" 143 | }, 144 | "r": { 145 | "delete_cmd_postfix": ") ", 146 | "delete_cmd_prefix": "rm(", 147 | "library": "var_list.r", 148 | "varRefreshCmd": "cat(var_dic_list()) " 149 | } 150 | }, 151 | "types_to_exclude": [ 152 | "module", 153 | "function", 154 | "builtin_function_or_method", 155 | "instance", 156 | "_Feature" 157 | ], 158 | "window_display": false 159 | } 160 | }, 161 | "nbformat": 4, 162 | "nbformat_minor": 4 163 | } 164 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/022_get_a_substring_of_a_string.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Get a Substring of a String\n", 17 | "\n", 18 | "In this example, you will learn to get a substring of a string.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Strings](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-02T11:16:36.491371Z", 33 | "start_time": "2021-08-02T11:16:36.460120Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "Python\n", 42 | "is fun.\n", 43 | "Python is fun\n" 44 | ] 45 | }, 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "'\\n>>Output/Runtime Test Cases:\\n \\nPython\\nis fun.\\nPython is fun\\n'" 50 | ] 51 | }, 52 | "execution_count": 1, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "# Example 1: Using String slicing\n", 59 | "\n", 60 | "my_string = \"Python is fun.\"\n", 61 | "\n", 62 | "# prints \"love\"\n", 63 | "print(my_string[0:6])\n", 64 | "\n", 65 | "# prints \"love python.\"\n", 66 | "print(my_string[7:])\n", 67 | "\n", 68 | "# prints \"I love python\"\n", 69 | "print(my_string[:-1])\n", 70 | "\n", 71 | "'''\n", 72 | ">>Output/Runtime Test Cases:\n", 73 | " \n", 74 | "Python\n", 75 | "is fun.\n", 76 | "Python is fun\n", 77 | "'''" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "**Explanation:** \n", 85 | "\n", 86 | "String slicing works similar to list slicing. The working of above code can be understood in the following points.\n", 87 | "\n", 88 | "* **`[0:6]`**\n", 89 | "You need to specify the starting index and the ending index of the substring. In this case, **`Python`** starts at index 0 and ends at index 6.\n", 90 | "* **`[7:]`**\n", 91 | "All the text from index 7 to the end are selected.\n", 92 | "* **`[:-1]`**\n", 93 | "All the text before the last index is selected." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [] 102 | } 103 | ], 104 | "metadata": { 105 | "hide_input": false, 106 | "kernelspec": { 107 | "display_name": "Python 3", 108 | "language": "python", 109 | "name": "python3" 110 | }, 111 | "language_info": { 112 | "codemirror_mode": { 113 | "name": "ipython", 114 | "version": 3 115 | }, 116 | "file_extension": ".py", 117 | "mimetype": "text/x-python", 118 | "name": "python", 119 | "nbconvert_exporter": "python", 120 | "pygments_lexer": "ipython3", 121 | "version": "3.8.8" 122 | }, 123 | "toc": { 124 | "base_numbering": 1, 125 | "nav_menu": {}, 126 | "number_sections": true, 127 | "sideBar": true, 128 | "skip_h1_title": false, 129 | "title_cell": "Table of Contents", 130 | "title_sidebar": "Contents", 131 | "toc_cell": false, 132 | "toc_position": {}, 133 | "toc_section_display": true, 134 | "toc_window_display": false 135 | }, 136 | "varInspector": { 137 | "cols": { 138 | "lenName": 16, 139 | "lenType": 16, 140 | "lenVar": 40 141 | }, 142 | "kernels_config": { 143 | "python": { 144 | "delete_cmd_postfix": "", 145 | "delete_cmd_prefix": "del ", 146 | "library": "var_list.py", 147 | "varRefreshCmd": "print(var_dic_list())" 148 | }, 149 | "r": { 150 | "delete_cmd_postfix": ") ", 151 | "delete_cmd_prefix": "rm(", 152 | "library": "var_list.r", 153 | "varRefreshCmd": "cat(var_dic_list()) " 154 | } 155 | }, 156 | "types_to_exclude": [ 157 | "module", 158 | "function", 159 | "builtin_function_or_method", 160 | "instance", 161 | "_Feature" 162 | ], 163 | "window_display": false 164 | } 165 | }, 166 | "nbformat": 4, 167 | "nbformat_minor": 4 168 | } 169 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/023_randomly_select_an_element_from_the_list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Randomly Select an Element From the List\n", 17 | "\n", 18 | "In this example, you will learn to select a random element from the list.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-02T11:21:30.455996Z", 33 | "start_time": "2021-08-02T11:21:30.428652Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "33\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "'\\n>>Output/Runtime Test Cases:\\n \\n33\\n'" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# Example 1: Using random module\n", 57 | "\n", 58 | "import random\n", 59 | "\n", 60 | "my_list = [1, 'a', 33, 'c', 'd', 66]\n", 61 | "print(random.choice(my_list))\n", 62 | "\n", 63 | "'''\n", 64 | ">>Output/Runtime Test Cases:\n", 65 | " \n", 66 | "33\n", 67 | "'''" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "**Explanation:** \n", 75 | "\n", 76 | "Using **`random`** module, we can generate a random element from a list. As shown in the example above, the list **`my_list`** is passed as a parameter to **`choice()`** method of random module.\n", 77 | "\n", 78 | ">**Note:** The output may vary." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 2, 84 | "metadata": { 85 | "ExecuteTime": { 86 | "end_time": "2021-08-02T11:21:30.611267Z", 87 | "start_time": "2021-08-02T11:21:30.458924Z" 88 | } 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "c\n" 96 | ] 97 | }, 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "'\\n>>Output/Runtime Test Cases:\\n \\nd\\n'" 102 | ] 103 | }, 104 | "execution_count": 2, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# Example 2: Using secrets module\n", 111 | "\n", 112 | "import secrets\n", 113 | "\n", 114 | "my_list = [1, 'a', 33, 'c', 'd', 66]\n", 115 | "print(secrets.choice(my_list))\n", 116 | "'''\n", 117 | ">>Output/Runtime Test Cases:\n", 118 | " \n", 119 | "c\n", 120 | "'''" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "**Explanation:** \n", 128 | "\n", 129 | "Using **`choice()`** method of **`secrets`** module, you can select a random element from the list.\n", 130 | "\n", 131 | "It is cryptographically safer than the **`random`** module." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [] 140 | } 141 | ], 142 | "metadata": { 143 | "hide_input": false, 144 | "kernelspec": { 145 | "display_name": "Python 3", 146 | "language": "python", 147 | "name": "python3" 148 | }, 149 | "language_info": { 150 | "codemirror_mode": { 151 | "name": "ipython", 152 | "version": 3 153 | }, 154 | "file_extension": ".py", 155 | "mimetype": "text/x-python", 156 | "name": "python", 157 | "nbconvert_exporter": "python", 158 | "pygments_lexer": "ipython3", 159 | "version": "3.8.8" 160 | }, 161 | "toc": { 162 | "base_numbering": 1, 163 | "nav_menu": {}, 164 | "number_sections": true, 165 | "sideBar": true, 166 | "skip_h1_title": false, 167 | "title_cell": "Table of Contents", 168 | "title_sidebar": "Contents", 169 | "toc_cell": false, 170 | "toc_position": {}, 171 | "toc_section_display": true, 172 | "toc_window_display": false 173 | }, 174 | "varInspector": { 175 | "cols": { 176 | "lenName": 16, 177 | "lenType": 16, 178 | "lenVar": 40 179 | }, 180 | "kernels_config": { 181 | "python": { 182 | "delete_cmd_postfix": "", 183 | "delete_cmd_prefix": "del ", 184 | "library": "var_list.py", 185 | "varRefreshCmd": "print(var_dic_list())" 186 | }, 187 | "r": { 188 | "delete_cmd_postfix": ") ", 189 | "delete_cmd_prefix": "rm(", 190 | "library": "var_list.r", 191 | "varRefreshCmd": "cat(var_dic_list()) " 192 | } 193 | }, 194 | "types_to_exclude": [ 195 | "module", 196 | "function", 197 | "builtin_function_or_method", 198 | "instance", 199 | "_Feature" 200 | ], 201 | "window_display": false 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 4 206 | } 207 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/024_check_if_a_String_is_a_number_float.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Check If a String Is a Number (Float)\n", 17 | "\n", 18 | "In this example, you will learn to check if a string is a number (float).\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Data Types](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-02T12:10:15.131317Z", 33 | "start_time": "2021-08-02T12:10:15.094208Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "False\n", 42 | "True\n" 43 | ] 44 | }, 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "'\\n>>Output/Runtime Test Cases:\\n \\nFalse\\nTrue\\n'" 49 | ] 50 | }, 51 | "execution_count": 1, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "# Example 1: Using float()\n", 58 | "\n", 59 | "def isfloat(num):\n", 60 | " try:\n", 61 | " float(num)\n", 62 | " return True\n", 63 | " except ValueError:\n", 64 | " return False\n", 65 | "\n", 66 | "print(isfloat('s66'))\n", 67 | "print(isfloat('9.639'))\n", 68 | "\n", 69 | "'''\n", 70 | ">>Output/Runtime Test Cases:\n", 71 | " \n", 72 | "False\n", 73 | "True\n", 74 | "'''" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "**Explanation:** \n", 82 | "\n", 83 | "Here, we have used try except in order to handle the **`ValueError`** if the string is not a float.\n", 84 | "\n", 85 | "* In the function **`isfloat()`**, **`float()`** tries to convert num to float. If it is successful, then the function returns **`True`**.\n", 86 | "* Else, **`ValueError`** is raised and returns **`False`**.\n", 87 | "\n", 88 | "For example, **`'s12'`** is alphanumeric, so it cannot be converted to float and **`False`** is returned; whereas, **`'1.123'`** is a numeric, so it is successfully converted to float." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [] 97 | } 98 | ], 99 | "metadata": { 100 | "hide_input": false, 101 | "kernelspec": { 102 | "display_name": "Python 3", 103 | "language": "python", 104 | "name": "python3" 105 | }, 106 | "language_info": { 107 | "codemirror_mode": { 108 | "name": "ipython", 109 | "version": 3 110 | }, 111 | "file_extension": ".py", 112 | "mimetype": "text/x-python", 113 | "name": "python", 114 | "nbconvert_exporter": "python", 115 | "pygments_lexer": "ipython3", 116 | "version": "3.8.8" 117 | }, 118 | "toc": { 119 | "base_numbering": 1, 120 | "nav_menu": {}, 121 | "number_sections": true, 122 | "sideBar": true, 123 | "skip_h1_title": false, 124 | "title_cell": "Table of Contents", 125 | "title_sidebar": "Contents", 126 | "toc_cell": false, 127 | "toc_position": {}, 128 | "toc_section_display": true, 129 | "toc_window_display": false 130 | }, 131 | "varInspector": { 132 | "cols": { 133 | "lenName": 16, 134 | "lenType": 16, 135 | "lenVar": 40 136 | }, 137 | "kernels_config": { 138 | "python": { 139 | "delete_cmd_postfix": "", 140 | "delete_cmd_prefix": "del ", 141 | "library": "var_list.py", 142 | "varRefreshCmd": "print(var_dic_list())" 143 | }, 144 | "r": { 145 | "delete_cmd_postfix": ") ", 146 | "delete_cmd_prefix": "rm(", 147 | "library": "var_list.r", 148 | "varRefreshCmd": "cat(var_dic_list()) " 149 | } 150 | }, 151 | "types_to_exclude": [ 152 | "module", 153 | "function", 154 | "builtin_function_or_method", 155 | "instance", 156 | "_Feature" 157 | ], 158 | "window_display": false 159 | } 160 | }, 161 | "nbformat": 4, 162 | "nbformat_minor": 4 163 | } 164 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/025_count_the_occurrence_of_an_item_in_a_list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Count the Occurrence of an Item in a List\n", 17 | "\n", 18 | "In this example, you will learn to count the occurrence of an item in a list.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-02T12:13:30.152457Z", 33 | "start_time": "2021-08-02T12:13:30.121210Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "3\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "'\\n>>Output/Runtime Test Cases:\\n \\nFalse\\nTrue\\n'" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# Example 1: Using count() method\n", 57 | "\n", 58 | "freq = ['a', 1, 'a', 9, 6, 3, 'a'].count('a')\n", 59 | "print(freq)\n", 60 | "\n", 61 | "'''\n", 62 | ">>Output/Runtime Test Cases:\n", 63 | " \n", 64 | "3\n", 65 | "'''" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "**Explanation:** \n", 73 | "\n", 74 | "The above code prints the number of occurrences of 'a' in the list.\n", 75 | "\n", 76 | "You can learn more about **`count()`** at **[Python count()](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String_Methods/004_Python_String_count().ipynb)**." 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [] 85 | } 86 | ], 87 | "metadata": { 88 | "hide_input": false, 89 | "kernelspec": { 90 | "display_name": "Python 3", 91 | "language": "python", 92 | "name": "python3" 93 | }, 94 | "language_info": { 95 | "codemirror_mode": { 96 | "name": "ipython", 97 | "version": 3 98 | }, 99 | "file_extension": ".py", 100 | "mimetype": "text/x-python", 101 | "name": "python", 102 | "nbconvert_exporter": "python", 103 | "pygments_lexer": "ipython3", 104 | "version": "3.8.8" 105 | }, 106 | "toc": { 107 | "base_numbering": 1, 108 | "nav_menu": {}, 109 | "number_sections": true, 110 | "sideBar": true, 111 | "skip_h1_title": false, 112 | "title_cell": "Table of Contents", 113 | "title_sidebar": "Contents", 114 | "toc_cell": false, 115 | "toc_position": {}, 116 | "toc_section_display": true, 117 | "toc_window_display": false 118 | }, 119 | "varInspector": { 120 | "cols": { 121 | "lenName": 16, 122 | "lenType": 16, 123 | "lenVar": 40 124 | }, 125 | "kernels_config": { 126 | "python": { 127 | "delete_cmd_postfix": "", 128 | "delete_cmd_prefix": "del ", 129 | "library": "var_list.py", 130 | "varRefreshCmd": "print(var_dic_list())" 131 | }, 132 | "r": { 133 | "delete_cmd_postfix": ") ", 134 | "delete_cmd_prefix": "rm(", 135 | "library": "var_list.r", 136 | "varRefreshCmd": "cat(var_dic_list()) " 137 | } 138 | }, 139 | "types_to_exclude": [ 140 | "module", 141 | "function", 142 | "builtin_function_or_method", 143 | "instance", 144 | "_Feature" 145 | ], 146 | "window_display": false 147 | } 148 | }, 149 | "nbformat": 4, 150 | "nbformat_minor": 4 151 | } 152 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/028_convert_two_lists_into_a_dictionary.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Convert Two Lists Into a Dictionary\n", 17 | "\n", 18 | "In this example, you will learn to convert two lists into a dictionary.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Dictionary](https://github.com/milaan9/02_Python_Datatypes/blob/main/005_Python_Dictionary.ipynb)**\n", 25 | "* **[Python zip()](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/066_Python_zip%28%29.ipynb)**" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": { 32 | "ExecuteTime": { 33 | "end_time": "2021-08-02T17:24:05.067470Z", 34 | "start_time": "2021-08-02T17:24:05.030365Z" 35 | } 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "{1: 'python', 2: 'java', 3: 'c'}\n" 43 | ] 44 | }, 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "'\\n>>Output/Runtime Test Cases:\\n \\nThe only way to\\nlearn to programing is\\nby writing codes.\\n'" 49 | ] 50 | }, 51 | "execution_count": 1, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "# Example 1: Using zip and dict methods\n", 58 | "\n", 59 | "index = [1, 2, 3]\n", 60 | "languages = ['python', 'java', 'c']\n", 61 | "\n", 62 | "dictionary = dict(zip(index, languages))\n", 63 | "print(dictionary)\n", 64 | "\n", 65 | "'''\n", 66 | ">>Output/Runtime Test Cases:\n", 67 | " \n", 68 | "{1: 'python', 2: 'java', 3: 'c'}\n", 69 | "'''" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "**Explanation:** \n", 77 | "\n", 78 | "The **`zip()`** function takes iterables (can be zero or more), aggregates them in a tuple, and returns it. Likewise, **`dict()`** gives the dictionary." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 2, 84 | "metadata": { 85 | "ExecuteTime": { 86 | "end_time": "2021-08-02T17:24:50.923414Z", 87 | "start_time": "2021-08-02T17:24:50.904861Z" 88 | } 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "{1: 'python', 2: 'java', 3: 'c'}\n" 96 | ] 97 | }, 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "'\\n>>Output/Runtime Test Cases:\\n \\nThe only way to \\nlearn to programing is \\nby writing codes.\\n'" 102 | ] 103 | }, 104 | "execution_count": 2, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# Example 2: Using list comprehension\n", 111 | "\n", 112 | "index = [1, 2, 3]\n", 113 | "languages = ['python', 'java', 'c']\n", 114 | "\n", 115 | "dictionary = {k: v for k, v in zip(index, languages)}\n", 116 | "print(dictionary)\n", 117 | "\n", 118 | "'''\n", 119 | ">>Output/Runtime Test Cases:\n", 120 | " \n", 121 | "{1: 'python', 2: 'java', 3: 'c'}\n", 122 | "'''" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [] 131 | } 132 | ], 133 | "metadata": { 134 | "hide_input": false, 135 | "kernelspec": { 136 | "display_name": "Python 3", 137 | "language": "python", 138 | "name": "python3" 139 | }, 140 | "language_info": { 141 | "codemirror_mode": { 142 | "name": "ipython", 143 | "version": 3 144 | }, 145 | "file_extension": ".py", 146 | "mimetype": "text/x-python", 147 | "name": "python", 148 | "nbconvert_exporter": "python", 149 | "pygments_lexer": "ipython3", 150 | "version": "3.8.8" 151 | }, 152 | "toc": { 153 | "base_numbering": 1, 154 | "nav_menu": {}, 155 | "number_sections": true, 156 | "sideBar": true, 157 | "skip_h1_title": false, 158 | "title_cell": "Table of Contents", 159 | "title_sidebar": "Contents", 160 | "toc_cell": false, 161 | "toc_position": {}, 162 | "toc_section_display": true, 163 | "toc_window_display": false 164 | }, 165 | "varInspector": { 166 | "cols": { 167 | "lenName": 16, 168 | "lenType": 16, 169 | "lenVar": 40 170 | }, 171 | "kernels_config": { 172 | "python": { 173 | "delete_cmd_postfix": "", 174 | "delete_cmd_prefix": "del ", 175 | "library": "var_list.py", 176 | "varRefreshCmd": "print(var_dic_list())" 177 | }, 178 | "r": { 179 | "delete_cmd_postfix": ") ", 180 | "delete_cmd_prefix": "rm(", 181 | "library": "var_list.r", 182 | "varRefreshCmd": "cat(var_dic_list()) " 183 | } 184 | }, 185 | "types_to_exclude": [ 186 | "module", 187 | "function", 188 | "builtin_function_or_method", 189 | "instance", 190 | "_Feature" 191 | ], 192 | "window_display": false 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 4 197 | } 198 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/031_iterate_through_two_lists_in_parallel.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Iterate Through Two Lists in Parallel\n", 17 | "\n", 18 | "In this example, you will learn to iterate through two lists in parallel.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**\n", 25 | "* **[Python zip()](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/066_Python_zip().ipynb)**\n", 26 | "* **[Python for Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/005_Python_for_Loop.ipynb)**" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": { 33 | "ExecuteTime": { 34 | "end_time": "2021-08-02T18:04:28.457323Z", 35 | "start_time": "2021-08-02T18:04:28.429979Z" 36 | } 37 | }, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "1 a\n", 44 | "2 b\n", 45 | "3 c\n" 46 | ] 47 | }, 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "'\\n>>Output/Runtime Test Cases:\\n \\nPython4DataScience\\n'" 52 | ] 53 | }, 54 | "execution_count": 1, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "# Example 1: Using zip (Python 3+)\n", 61 | "\n", 62 | "list_1 = [1, 2, 3, 4, 5]\n", 63 | "list_2 = ['a', 'b', 'c']\n", 64 | "\n", 65 | "for i, j in zip(list_1, list_2):\n", 66 | " print(i, j)\n", 67 | "\n", 68 | "'''\n", 69 | ">>Output/Runtime Test Cases:\n", 70 | " \n", 71 | "1 a\n", 72 | "2 b\n", 73 | "3 c\n", 74 | "'''" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "**Explanation:** \n", 82 | "\n", 83 | "The loop runs until the shorter list stops (unless other conditions are passed)." 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [] 92 | } 93 | ], 94 | "metadata": { 95 | "hide_input": false, 96 | "kernelspec": { 97 | "display_name": "Python 3", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.8.8" 112 | }, 113 | "toc": { 114 | "base_numbering": 1, 115 | "nav_menu": {}, 116 | "number_sections": true, 117 | "sideBar": true, 118 | "skip_h1_title": false, 119 | "title_cell": "Table of Contents", 120 | "title_sidebar": "Contents", 121 | "toc_cell": false, 122 | "toc_position": {}, 123 | "toc_section_display": true, 124 | "toc_window_display": false 125 | }, 126 | "varInspector": { 127 | "cols": { 128 | "lenName": 16, 129 | "lenType": 16, 130 | "lenVar": 40 131 | }, 132 | "kernels_config": { 133 | "python": { 134 | "delete_cmd_postfix": "", 135 | "delete_cmd_prefix": "del ", 136 | "library": "var_list.py", 137 | "varRefreshCmd": "print(var_dic_list())" 138 | }, 139 | "r": { 140 | "delete_cmd_postfix": ") ", 141 | "delete_cmd_prefix": "rm(", 142 | "library": "var_list.r", 143 | "varRefreshCmd": "cat(var_dic_list()) " 144 | } 145 | }, 146 | "types_to_exclude": [ 147 | "module", 148 | "function", 149 | "builtin_function_or_method", 150 | "instance", 151 | "_Feature" 152 | ], 153 | "window_display": false 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 4 158 | } 159 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/033_check_if_two_strings_are_anagram.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Check If Two Strings are Anagram\n", 17 | "\n", 18 | "In this example, you will learn to check if two strings are anagram.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Strings](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String.ipynb)**\n", 25 | "\n", 26 | "Two strings are said to be anagram if we can form one string by arranging the characters of another string. For example, Race and Care. Here, we can form Race by arranging the characters of Care." 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": { 33 | "ExecuteTime": { 34 | "end_time": "2021-08-02T18:16:52.137784Z", 35 | "start_time": "2021-08-02T18:16:52.094812Z" 36 | } 37 | }, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "heart and earth are anagram.\n" 44 | ] 45 | }, 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "'\\n>>Output/Runtime Test Cases:\\n \\nheart and earth are anagram.\\n'" 50 | ] 51 | }, 52 | "execution_count": 1, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "# Example 1: check if two strings are anagrams using sorted()\n", 59 | "\n", 60 | "str1 = \"Heart\"\n", 61 | "str2 = \"Earth\"\n", 62 | "\n", 63 | "# convert both the strings into lowercase\n", 64 | "str1 = str1.lower()\n", 65 | "str2 = str2.lower()\n", 66 | "\n", 67 | "# check if length is same\n", 68 | "if(len(str1) == len(str2)):\n", 69 | "\n", 70 | " # sort the strings\n", 71 | " sorted_str1 = sorted(str1)\n", 72 | " sorted_str2 = sorted(str2)\n", 73 | "\n", 74 | " # if sorted char arrays are same\n", 75 | " if(sorted_str1 == sorted_str2):\n", 76 | " print(str1 + \" and \" + str2 + \" are anagram.\")\n", 77 | " else:\n", 78 | " print(str1 + \" and \" + str2 + \" are not anagram.\")\n", 79 | "\n", 80 | "else:\n", 81 | " print(str1 + \" and \" + str2 + \" are not anagram.\")\n", 82 | "\n", 83 | "'''\n", 84 | ">>Output/Runtime Test Cases:\n", 85 | " \n", 86 | "heart and earth are anagram.\n", 87 | "'''" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "**Explanation:** \n", 95 | "\n", 96 | "We first convert the strings to lowercase. It is because Python is case sensitive (i.e. H and h are two different characters in Python).\n", 97 | "\n", 98 | "Here,\n", 99 | "\n", 100 | "* **`lower()`** - converts the characters into lower case\n", 101 | "* **`sorted()`** - sorts both the strings\n", 102 | "\n", 103 | "If sorted arrays are equal, then the strings are anagram." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | } 113 | ], 114 | "metadata": { 115 | "hide_input": false, 116 | "kernelspec": { 117 | "display_name": "Python 3", 118 | "language": "python", 119 | "name": "python3" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 3 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython3", 131 | "version": "3.8.8" 132 | }, 133 | "toc": { 134 | "base_numbering": 1, 135 | "nav_menu": {}, 136 | "number_sections": true, 137 | "sideBar": true, 138 | "skip_h1_title": false, 139 | "title_cell": "Table of Contents", 140 | "title_sidebar": "Contents", 141 | "toc_cell": false, 142 | "toc_position": {}, 143 | "toc_section_display": true, 144 | "toc_window_display": false 145 | }, 146 | "varInspector": { 147 | "cols": { 148 | "lenName": 16, 149 | "lenType": 16, 150 | "lenVar": 40 151 | }, 152 | "kernels_config": { 153 | "python": { 154 | "delete_cmd_postfix": "", 155 | "delete_cmd_prefix": "del ", 156 | "library": "var_list.py", 157 | "varRefreshCmd": "print(var_dic_list())" 158 | }, 159 | "r": { 160 | "delete_cmd_postfix": ") ", 161 | "delete_cmd_prefix": "rm(", 162 | "library": "var_list.r", 163 | "varRefreshCmd": "cat(var_dic_list()) " 164 | } 165 | }, 166 | "types_to_exclude": [ 167 | "module", 168 | "function", 169 | "builtin_function_or_method", 170 | "instance", 171 | "_Feature" 172 | ], 173 | "window_display": false 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 4 178 | } 179 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/037_remove_duplicate_element_from_a_list.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Remove Duplicate Element From a List\n", 17 | "\n", 18 | "In this example, you will learn to remove duplicate elements from a list.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Sets](https://github.com/milaan9/02_Python_Datatypes/blob/main/006_Python_Sets.ipynb)**\n", 25 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": { 32 | "ExecuteTime": { 33 | "end_time": "2021-08-02T19:01:52.676526Z", 34 | "start_time": "2021-08-02T19:01:52.640392Z" 35 | } 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "[1, 3, 6, 9]\n" 43 | ] 44 | }, 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "'\\n>>Output/Runtime Test Cases:\\n \\n[1, 3, 6, 9]\\n'" 49 | ] 50 | }, 51 | "execution_count": 1, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "# Example 1: Using set()\n", 58 | "\n", 59 | "list_1 = [1, 3, 1, 6, 9]\n", 60 | "\n", 61 | "print(list(set(list_1)))\n", 62 | "\n", 63 | "'''\n", 64 | ">>Output/Runtime Test Cases:\n", 65 | " \n", 66 | "[1, 3, 6, 9]\n", 67 | "'''" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "**Explanation:**\n", 75 | "\n", 76 | "In the above example, we first convert the list into a set, then we again convert it into a list. Set cannot have a duplicate item in it, so **`set()`** removes keeps only an instance of the item." 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 2, 82 | "metadata": { 83 | "ExecuteTime": { 84 | "end_time": "2021-08-02T19:01:52.862103Z", 85 | "start_time": "2021-08-02T19:01:52.679460Z" 86 | }, 87 | "scrolled": true 88 | }, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "[1, 2, 3, 5, 6, 7, 8, 9]\n" 95 | ] 96 | }, 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "'\\n>>Output/Runtime Test Cases:\\n \\n[1, 2, 3, 5, 6, 7, 8, 9]\\n'" 101 | ] 102 | }, 103 | "execution_count": 2, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "# Example 2: Remove the items that are duplicated\n", 110 | "\n", 111 | "list_1 = [1, 3, 1, 6, 9]\n", 112 | "list_2 = [7, 5, 2, 8]\n", 113 | "\n", 114 | "print(list(set(list_1) ^ set(list_2) ))\n", 115 | "\n", 116 | "'''\n", 117 | ">>Output/Runtime Test Cases:\n", 118 | " \n", 119 | "[1, 2, 3, 5, 6, 7, 8, 9]\n", 120 | "'''" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "**Explanation:**\n", 128 | "\n", 129 | "In the above example, the items that are present in both the lists are removed." 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [] 138 | } 139 | ], 140 | "metadata": { 141 | "hide_input": false, 142 | "kernelspec": { 143 | "display_name": "Python 3", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.8.8" 158 | }, 159 | "toc": { 160 | "base_numbering": 1, 161 | "nav_menu": {}, 162 | "number_sections": true, 163 | "sideBar": true, 164 | "skip_h1_title": false, 165 | "title_cell": "Table of Contents", 166 | "title_sidebar": "Contents", 167 | "toc_cell": false, 168 | "toc_position": {}, 169 | "toc_section_display": true, 170 | "toc_window_display": false 171 | }, 172 | "varInspector": { 173 | "cols": { 174 | "lenName": 16, 175 | "lenType": 16, 176 | "lenVar": 40 177 | }, 178 | "kernels_config": { 179 | "python": { 180 | "delete_cmd_postfix": "", 181 | "delete_cmd_prefix": "del ", 182 | "library": "var_list.py", 183 | "varRefreshCmd": "print(var_dic_list())" 184 | }, 185 | "r": { 186 | "delete_cmd_postfix": ") ", 187 | "delete_cmd_prefix": "rm(", 188 | "library": "var_list.r", 189 | "varRefreshCmd": "cat(var_dic_list()) " 190 | } 191 | }, 192 | "types_to_exclude": [ 193 | "module", 194 | "function", 195 | "builtin_function_or_method", 196 | "instance", 197 | "_Feature" 198 | ], 199 | "window_display": false 200 | } 201 | }, 202 | "nbformat": 4, 203 | "nbformat_minor": 4 204 | } 205 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 02 Python Datatypes Examples 9 | 10 | In this class, you'll find examples related to python datatypes with a detailed explanations. 11 | -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/img/color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/02_Python_Datatypes_examples/img/color.png -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/img/l6_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/02_Python_Datatypes_examples/img/l6_2.png -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/img/matrix1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/02_Python_Datatypes_examples/img/matrix1.png -------------------------------------------------------------------------------- /02_Python_Datatypes_examples/img/matrix2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/02_Python_Datatypes_examples/img/matrix2.png -------------------------------------------------------------------------------- /03_Python_Flow_Control_examples/002_check_if_a_number_is_odd_or_even.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Check if a Number is Odd or Even\n", 17 | "\n", 18 | "In this example, you will learn to check whether a number entered by the user is even or odd.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**\n", 25 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 26 | "* **[Python if-elif-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/003_Python_if_elif_else_statement%20.ipynb)**" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator **`%`** to compute the remainder. If the remainder is not zero, the number is odd." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": { 40 | "ExecuteTime": { 41 | "end_time": "2021-08-03T07:30:38.694953Z", 42 | "start_time": "2021-08-03T07:30:35.934220Z" 43 | } 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Enter a number: 6\n", 51 | "6 is Even\n" 52 | ] 53 | }, 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "'\\n>>Output/Runtime Test Cases:\\n \\nEnter a number: 9\\nPositive number\\n'" 58 | ] 59 | }, 60 | "execution_count": 1, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "# Example 1: check if the input number is odd or even.\n", 67 | "# A number is even if division by 2 gives a remainder of 0.\n", 68 | "# If the remainder is 1, it is an odd number.\n", 69 | "\n", 70 | "num = int(input(\"Enter a number: \"))\n", 71 | "if (num % 2) == 0:\n", 72 | " print(\"{0} is Even\".format(num))\n", 73 | "else:\n", 74 | " print(\"{0} is Odd\".format(num))\n", 75 | "\n", 76 | "'''\n", 77 | ">>Output/Runtime Test Cases:\n", 78 | " \n", 79 | "Enter a number: 33\n", 80 | "6 is Odd\n", 81 | "'''" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "**Explanation:**\n", 89 | "\n", 90 | "In this program, we ask the user for the input and check if the number is odd or even. Please note that **`{ }`** is a replacement field for **`num`**." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [] 99 | } 100 | ], 101 | "metadata": { 102 | "hide_input": false, 103 | "kernelspec": { 104 | "display_name": "Python 3", 105 | "language": "python", 106 | "name": "python3" 107 | }, 108 | "language_info": { 109 | "codemirror_mode": { 110 | "name": "ipython", 111 | "version": 3 112 | }, 113 | "file_extension": ".py", 114 | "mimetype": "text/x-python", 115 | "name": "python", 116 | "nbconvert_exporter": "python", 117 | "pygments_lexer": "ipython3", 118 | "version": "3.8.8" 119 | }, 120 | "toc": { 121 | "base_numbering": 1, 122 | "nav_menu": {}, 123 | "number_sections": true, 124 | "sideBar": true, 125 | "skip_h1_title": false, 126 | "title_cell": "Table of Contents", 127 | "title_sidebar": "Contents", 128 | "toc_cell": false, 129 | "toc_position": {}, 130 | "toc_section_display": true, 131 | "toc_window_display": false 132 | }, 133 | "varInspector": { 134 | "cols": { 135 | "lenName": 16, 136 | "lenType": 16, 137 | "lenVar": 40 138 | }, 139 | "kernels_config": { 140 | "python": { 141 | "delete_cmd_postfix": "", 142 | "delete_cmd_prefix": "del ", 143 | "library": "var_list.py", 144 | "varRefreshCmd": "print(var_dic_list())" 145 | }, 146 | "r": { 147 | "delete_cmd_postfix": ") ", 148 | "delete_cmd_prefix": "rm(", 149 | "library": "var_list.r", 150 | "varRefreshCmd": "cat(var_dic_list()) " 151 | } 152 | }, 153 | "types_to_exclude": [ 154 | "module", 155 | "function", 156 | "builtin_function_or_method", 157 | "instance", 158 | "_Feature" 159 | ], 160 | "window_display": false 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 4 165 | } 166 | -------------------------------------------------------------------------------- /03_Python_Flow_Control_examples/003_check_leap_year.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Check Leap Year\n", 17 | "\n", 18 | "In this program, you will learn to check whether a year is leap year or not. We will use nested if-else to solve this problem.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**\n", 25 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 26 | "* **[Python Nested if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/004_Python_Nested_if_statement.ipynb)**" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. \n", 34 | "\n", 35 | "For example:\n", 36 | "* 2017 is not a leap year\n", 37 | "* 1900 is a not leap year\n", 38 | "* 2012 is a leap year\n", 39 | "* 2000 is a leap year" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 1, 45 | "metadata": { 46 | "ExecuteTime": { 47 | "end_time": "2021-08-03T07:37:38.797986Z", 48 | "start_time": "2021-08-03T07:37:38.767712Z" 49 | } 50 | }, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "2022 is not a leap year\n" 57 | ] 58 | }, 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "'\\n>>Output/Runtime Test Cases:\\n \\n2022 is not a leap year\\n'" 63 | ] 64 | }, 65 | "execution_count": 1, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "# Example 1: check if year is a leap year or not\n", 72 | "\n", 73 | "year = 2022\n", 74 | "\n", 75 | "# To get year (integer input) from the user\n", 76 | "# year = int(input(\"Enter a year: \"))\n", 77 | "\n", 78 | "if (year % 4) == 0:\n", 79 | " if (year % 100) == 0:\n", 80 | " if (year % 400) == 0:\n", 81 | " print(\"{0} is a leap year\".format(year))\n", 82 | " else:\n", 83 | " print(\"{0} is not a leap year\".format(year))\n", 84 | " else:\n", 85 | " print(\"{0} is a leap year\".format(year))\n", 86 | "else:\n", 87 | " print(\"{0} is not a leap year\".format(year))\n", 88 | " \n", 89 | "'''\n", 90 | ">>Output/Runtime Test Cases:\n", 91 | " \n", 92 | "2022 is not a leap year\n", 93 | "'''" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "**Explanation:**\n", 101 | "\n", 102 | "You can change the value of **`year`** in the source code and run it again to test this program." 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | } 112 | ], 113 | "metadata": { 114 | "hide_input": false, 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.8.8" 131 | }, 132 | "toc": { 133 | "base_numbering": 1, 134 | "nav_menu": {}, 135 | "number_sections": true, 136 | "sideBar": true, 137 | "skip_h1_title": false, 138 | "title_cell": "Table of Contents", 139 | "title_sidebar": "Contents", 140 | "toc_cell": false, 141 | "toc_position": {}, 142 | "toc_section_display": true, 143 | "toc_window_display": false 144 | }, 145 | "varInspector": { 146 | "cols": { 147 | "lenName": 16, 148 | "lenType": 16, 149 | "lenVar": 40 150 | }, 151 | "kernels_config": { 152 | "python": { 153 | "delete_cmd_postfix": "", 154 | "delete_cmd_prefix": "del ", 155 | "library": "var_list.py", 156 | "varRefreshCmd": "print(var_dic_list())" 157 | }, 158 | "r": { 159 | "delete_cmd_postfix": ") ", 160 | "delete_cmd_prefix": "rm(", 161 | "library": "var_list.r", 162 | "varRefreshCmd": "cat(var_dic_list()) " 163 | } 164 | }, 165 | "types_to_exclude": [ 166 | "module", 167 | "function", 168 | "builtin_function_or_method", 169 | "instance", 170 | "_Feature" 171 | ], 172 | "window_display": false 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 4 177 | } 178 | -------------------------------------------------------------------------------- /03_Python_Flow_Control_examples/004_find_the_argest_among_three_numbers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Find the Largest Among Three Numbers\n", 17 | "\n", 18 | "In this program, you'll learn to find the largest among three numbers using if else and display it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following Python programming topics:\n", 21 | "\n", 22 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 23 | "\n", 24 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 25 | "* **[Python if-elif-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/003_Python_if_elif_else_statement%20.ipynb)**\n", 26 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "In the program below, the three numbers are stored in **`num1`**, **`num2`** and **`num3`** respectively. We've used the **`if-elif-else`** ladder to find the largest among the three and display it." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": { 40 | "ExecuteTime": { 41 | "end_time": "2021-08-03T08:03:58.094336Z", 42 | "start_time": "2021-08-03T08:03:58.064064Z" 43 | } 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "The largest number is: 19\n" 51 | ] 52 | }, 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "'\\n>>Output/Runtime Test Cases:\\n \\n2022 is not a leap year\\n'" 57 | ] 58 | }, 59 | "execution_count": 1, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "# Example 1: find the largest number among the three input numbers\n", 66 | "\n", 67 | "# change the values of num1, num2 and num3\n", 68 | "# for a different result\n", 69 | "num1 = 12\n", 70 | "num2 = 19\n", 71 | "num3 = 16\n", 72 | "\n", 73 | "# uncomment following lines to take three numbers from user\n", 74 | "#num1 = float(input(\"Enter first number: \"))\n", 75 | "#num2 = float(input(\"Enter second number: \"))\n", 76 | "#num3 = float(input(\"Enter third number: \"))\n", 77 | "\n", 78 | "if (num1 >= num2) and (num1 >= num3):\n", 79 | " largest = num1\n", 80 | "elif (num2 >= num1) and (num2 >= num3):\n", 81 | " largest = num2\n", 82 | "else:\n", 83 | " largest = num3\n", 84 | "\n", 85 | "print(\"The largest number is:\", largest)\n", 86 | " \n", 87 | "'''\n", 88 | ">>Output/Runtime Test Cases:\n", 89 | " \n", 90 | "The largest number is: 19\n", 91 | "'''" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "**Explanation:**\n", 99 | "\n", 100 | "You can change the value of **`num1`**, **`num2`**, and **`num3`** in the source code and run it again to test this program." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [] 109 | } 110 | ], 111 | "metadata": { 112 | "hide_input": false, 113 | "kernelspec": { 114 | "display_name": "Python 3", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.8.8" 129 | }, 130 | "toc": { 131 | "base_numbering": 1, 132 | "nav_menu": {}, 133 | "number_sections": true, 134 | "sideBar": true, 135 | "skip_h1_title": false, 136 | "title_cell": "Table of Contents", 137 | "title_sidebar": "Contents", 138 | "toc_cell": false, 139 | "toc_position": {}, 140 | "toc_section_display": true, 141 | "toc_window_display": false 142 | }, 143 | "varInspector": { 144 | "cols": { 145 | "lenName": 16, 146 | "lenType": 16, 147 | "lenVar": 40 148 | }, 149 | "kernels_config": { 150 | "python": { 151 | "delete_cmd_postfix": "", 152 | "delete_cmd_prefix": "del ", 153 | "library": "var_list.py", 154 | "varRefreshCmd": "print(var_dic_list())" 155 | }, 156 | "r": { 157 | "delete_cmd_postfix": ") ", 158 | "delete_cmd_prefix": "rm(", 159 | "library": "var_list.r", 160 | "varRefreshCmd": "cat(var_dic_list()) " 161 | } 162 | }, 163 | "types_to_exclude": [ 164 | "module", 165 | "function", 166 | "builtin_function_or_method", 167 | "instance", 168 | "_Feature" 169 | ], 170 | "window_display": false 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 4 175 | } 176 | -------------------------------------------------------------------------------- /03_Python_Flow_Control_examples/008_display_the_multiplication_table.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Display the multiplication Table\n", 17 | "\n", 18 | "This program displays the multiplication table of variable **`num`** (from 1 to 10).\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python for Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/005_Python_for_Loop.ipynb)**\n", 23 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": { 30 | "ExecuteTime": { 31 | "end_time": "2021-08-03T12:37:29.480138Z", 32 | "start_time": "2021-08-03T12:37:29.423497Z" 33 | } 34 | }, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "15 x 1 = 15\n", 41 | "15 x 2 = 30\n", 42 | "15 x 3 = 45\n", 43 | "15 x 4 = 60\n", 44 | "15 x 5 = 75\n", 45 | "15 x 6 = 90\n", 46 | "15 x 7 = 105\n", 47 | "15 x 8 = 120\n", 48 | "15 x 9 = 135\n", 49 | "15 x 10 = 150\n" 50 | ] 51 | }, 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "'\\n>>Output/Runtime Test Cases:\\n \\n15 x 1 = 15\\n15 x 2 = 30\\n15 x 3 = 45\\n15 x 4 = 60\\n15 x 5 = 75\\n15 x 6 = 90\\n15 x 7 = 105\\n15 x 8 = 120\\n15 x 9 = 135\\n15 x 10 = 150\\n'" 56 | ] 57 | }, 58 | "execution_count": 1, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "# Example 1: Multiplication table (from 1 to 10) in Python\n", 65 | "\n", 66 | "num = 15\n", 67 | "\n", 68 | "# To take input from the user\n", 69 | "# num = int(input(\"Display multiplication table of? \"))\n", 70 | "\n", 71 | "# Iterate 10 times from i = 1 to 10\n", 72 | "for i in range(1, 11):\n", 73 | " print(num, 'x', i, '=', num*i)\n", 74 | " \n", 75 | "'''\n", 76 | ">>Output/Runtime Test Cases:\n", 77 | " \n", 78 | "15 x 1 = 15\n", 79 | "15 x 2 = 30\n", 80 | "15 x 3 = 45\n", 81 | "15 x 4 = 60\n", 82 | "15 x 5 = 75\n", 83 | "15 x 6 = 90\n", 84 | "15 x 7 = 105\n", 85 | "15 x 8 = 120\n", 86 | "15 x 9 = 135\n", 87 | "15 x 10 = 150\n", 88 | "'''" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "**Explanation:**\n", 96 | "\n", 97 | "Here, we have used the **`for`** loop along with the **[range() function](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/053_Python_range%28%29.ipynb)** to iterate 10 times. The arguments inside the **`range()`** function are (1, 11). Meaning, greater than or equal to 1 and less than 11.\n", 98 | "\n", 99 | "We have displayed the multiplication table of variable **`num`** (which is 12 in our case). You can change the value of **`num`** in the above program to test for other values." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | } 109 | ], 110 | "metadata": { 111 | "hide_input": false, 112 | "kernelspec": { 113 | "display_name": "Python 3", 114 | "language": "python", 115 | "name": "python3" 116 | }, 117 | "language_info": { 118 | "codemirror_mode": { 119 | "name": "ipython", 120 | "version": 3 121 | }, 122 | "file_extension": ".py", 123 | "mimetype": "text/x-python", 124 | "name": "python", 125 | "nbconvert_exporter": "python", 126 | "pygments_lexer": "ipython3", 127 | "version": "3.8.8" 128 | }, 129 | "toc": { 130 | "base_numbering": 1, 131 | "nav_menu": {}, 132 | "number_sections": true, 133 | "sideBar": true, 134 | "skip_h1_title": false, 135 | "title_cell": "Table of Contents", 136 | "title_sidebar": "Contents", 137 | "toc_cell": false, 138 | "toc_position": {}, 139 | "toc_section_display": true, 140 | "toc_window_display": false 141 | }, 142 | "varInspector": { 143 | "cols": { 144 | "lenName": 16, 145 | "lenType": 16, 146 | "lenVar": 40 147 | }, 148 | "kernels_config": { 149 | "python": { 150 | "delete_cmd_postfix": "", 151 | "delete_cmd_prefix": "del ", 152 | "library": "var_list.py", 153 | "varRefreshCmd": "print(var_dic_list())" 154 | }, 155 | "r": { 156 | "delete_cmd_postfix": ") ", 157 | "delete_cmd_prefix": "rm(", 158 | "library": "var_list.r", 159 | "varRefreshCmd": "cat(var_dic_list()) " 160 | } 161 | }, 162 | "types_to_exclude": [ 163 | "module", 164 | "function", 165 | "builtin_function_or_method", 166 | "instance", 167 | "_Feature" 168 | ], 169 | "window_display": false 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 4 174 | } 175 | -------------------------------------------------------------------------------- /03_Python_Flow_Control_examples/012_find_the_sum_of_natural_numbers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Find the Sum of Natural Numbers\n", 17 | "\n", 18 | "In this program, you'll learn to find the sum of **`n`** natural numbers using while loop and display it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 23 | "* **[Python while Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/006_Python_while_Loop.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "In the program below, we've used an **`if-else`** statement in combination with a **`while`** loop to calculate the sum of natural numbers up to **`num`**." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": { 37 | "ExecuteTime": { 38 | "end_time": "2021-08-03T13:03:12.824151Z", 39 | "start_time": "2021-08-03T13:03:12.788021Z" 40 | } 41 | }, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "The sum is 190\n" 48 | ] 49 | }, 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "'\\n>>Output/Runtime Test Cases:\\n \\nThe sum is 190\\n'" 54 | ] 55 | }, 56 | "execution_count": 1, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "# Example 1: sum of natural numbers up to num\n", 63 | "\n", 64 | "num = 19\n", 65 | "\n", 66 | "if num < 0:\n", 67 | " print(\"Enter a positive number\")\n", 68 | "else:\n", 69 | " sum = 0\n", 70 | " # use while loop to iterate until zero\n", 71 | " while(num > 0):\n", 72 | " sum += num\n", 73 | " num -= 1\n", 74 | " print(\"The sum is\", sum)\n", 75 | " \n", 76 | "'''\n", 77 | ">>Output/Runtime Test Cases:\n", 78 | " \n", 79 | "The sum is 190\n", 80 | "'''" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | ">**Note:** To test the program for a different number, change the value of **`num`**.\n", 88 | "\n", 89 | "**Explanation:**\n", 90 | "\n", 91 | "Initially, the **`sum`** is initialized to 0. And, the number is stored in variable **`num`**.\n", 92 | "\n", 93 | "Then, we used the **`while`** loop to iterate until **`num`** becomes zero. In each iteration of the loop, we have added the **`num`** to **`sum`** and the value of **`num`** is decreased by 1." 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "We could have solved the above problem without using a loop by using the following formula.\n", 101 | "\n", 102 | "```python\n", 103 | "n*(n+1)/2\n", 104 | "```\n", 105 | "\n", 106 | "For example, if **n = 16**, the sum would be **(16*17)/2 = 136**.\n", 107 | "\n", 108 | "**Your turn:** Modify the above program to find the sum of natural numbers using the formula below." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [] 117 | } 118 | ], 119 | "metadata": { 120 | "hide_input": false, 121 | "kernelspec": { 122 | "display_name": "Python 3", 123 | "language": "python", 124 | "name": "python3" 125 | }, 126 | "language_info": { 127 | "codemirror_mode": { 128 | "name": "ipython", 129 | "version": 3 130 | }, 131 | "file_extension": ".py", 132 | "mimetype": "text/x-python", 133 | "name": "python", 134 | "nbconvert_exporter": "python", 135 | "pygments_lexer": "ipython3", 136 | "version": "3.8.8" 137 | }, 138 | "toc": { 139 | "base_numbering": 1, 140 | "nav_menu": {}, 141 | "number_sections": true, 142 | "sideBar": true, 143 | "skip_h1_title": false, 144 | "title_cell": "Table of Contents", 145 | "title_sidebar": "Contents", 146 | "toc_cell": false, 147 | "toc_position": {}, 148 | "toc_section_display": true, 149 | "toc_window_display": false 150 | }, 151 | "varInspector": { 152 | "cols": { 153 | "lenName": 16, 154 | "lenType": 16, 155 | "lenVar": 40 156 | }, 157 | "kernels_config": { 158 | "python": { 159 | "delete_cmd_postfix": "", 160 | "delete_cmd_prefix": "del ", 161 | "library": "var_list.py", 162 | "varRefreshCmd": "print(var_dic_list())" 163 | }, 164 | "r": { 165 | "delete_cmd_postfix": ") ", 166 | "delete_cmd_prefix": "rm(", 167 | "library": "var_list.r", 168 | "varRefreshCmd": "cat(var_dic_list()) " 169 | } 170 | }, 171 | "types_to_exclude": [ 172 | "module", 173 | "function", 174 | "builtin_function_or_method", 175 | "instance", 176 | "_Feature" 177 | ], 178 | "window_display": false 179 | } 180 | }, 181 | "nbformat": 4, 182 | "nbformat_minor": 4 183 | } 184 | -------------------------------------------------------------------------------- /03_Python_Flow_Control_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 02 Python Flow Control Examples 9 | 10 | In this class, you'll find examples related to python decision making and loops with a detailed explanations. 11 | -------------------------------------------------------------------------------- /04_Python_Functions_examples/002_find_numbers_divisible_by_another_number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Find Numbers Divisible by Another Number\n", 17 | "\n", 18 | "In this example, you'll learn to find the numbers divisible by another number and display it.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Anonymous/Lambda Function](https://github.com/milaan9/04_Python_Functions/blob/main/006_Python_Function_Anonymous.ipynb)**\n", 23 | "* **[Python List](https://github.com/milaan9/02_Python_Datatypes/blob/main/003_Python_List.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "In the program below, we have used anonymous (lambda) function inside the **[filter() built-in function](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/020_Python_filter%28%29.ipynb)** to find all the numbers divisible by 13 in the list." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": { 37 | "ExecuteTime": { 38 | "end_time": "2021-08-04T08:07:01.713730Z", 39 | "start_time": "2021-08-04T08:07:01.664901Z" 40 | } 41 | }, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Numbers divisible by 17 are [17, 34, 51, 68, 119, 986]\n" 48 | ] 49 | }, 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "'\\n>>Output/Runtime Test Cases:\\n \\nNumbers divisible by 17 are [17, 34, 51, 68, 119, 986]\\n'" 54 | ] 55 | }, 56 | "execution_count": 1, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "# Example 1: \n", 63 | "\n", 64 | "# Take a list of numbers\n", 65 | "my_list = [17, 34, 51, 43, 68, 72, 119, 327, 243, 986]\n", 66 | "\n", 67 | "# use anonymous function to filter\n", 68 | "result = list(filter(lambda x: (x % 17 == 0), my_list))\n", 69 | "\n", 70 | "# display the result\n", 71 | "print(\"Numbers divisible by 17 are\",result)\n", 72 | " \n", 73 | "'''\n", 74 | ">>Output/Runtime Test Cases:\n", 75 | " \n", 76 | "Numbers divisible by 17 are [17, 34, 51, 68, 119, 986]\n", 77 | "'''" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [] 86 | } 87 | ], 88 | "metadata": { 89 | "hide_input": false, 90 | "kernelspec": { 91 | "display_name": "Python 3", 92 | "language": "python", 93 | "name": "python3" 94 | }, 95 | "language_info": { 96 | "codemirror_mode": { 97 | "name": "ipython", 98 | "version": 3 99 | }, 100 | "file_extension": ".py", 101 | "mimetype": "text/x-python", 102 | "name": "python", 103 | "nbconvert_exporter": "python", 104 | "pygments_lexer": "ipython3", 105 | "version": "3.8.8" 106 | }, 107 | "toc": { 108 | "base_numbering": 1, 109 | "nav_menu": {}, 110 | "number_sections": true, 111 | "sideBar": true, 112 | "skip_h1_title": false, 113 | "title_cell": "Table of Contents", 114 | "title_sidebar": "Contents", 115 | "toc_cell": false, 116 | "toc_position": {}, 117 | "toc_section_display": true, 118 | "toc_window_display": false 119 | }, 120 | "varInspector": { 121 | "cols": { 122 | "lenName": 16, 123 | "lenType": 16, 124 | "lenVar": 40 125 | }, 126 | "kernels_config": { 127 | "python": { 128 | "delete_cmd_postfix": "", 129 | "delete_cmd_prefix": "del ", 130 | "library": "var_list.py", 131 | "varRefreshCmd": "print(var_dic_list())" 132 | }, 133 | "r": { 134 | "delete_cmd_postfix": ") ", 135 | "delete_cmd_prefix": "rm(", 136 | "library": "var_list.r", 137 | "varRefreshCmd": "cat(var_dic_list()) " 138 | } 139 | }, 140 | "types_to_exclude": [ 141 | "module", 142 | "function", 143 | "builtin_function_or_method", 144 | "instance", 145 | "_Feature" 146 | ], 147 | "window_display": false 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 4 152 | } 153 | -------------------------------------------------------------------------------- /04_Python_Functions_examples/007_find_the_factors_of_a_number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Find the Factors of a Number\n", 17 | "\n", 18 | "In this example, you'll learn to find the factors of a number using the **`for`** loop.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 23 | "* **[Python for Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/005_Python_for_Loop.ipynb)**\n", 24 | "* **[Python User-defined Functions](https://github.com/milaan9/04_Python_Functions/blob/main/Python_User_defined_Functions.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-05T14:39:45.830985Z", 33 | "start_time": "2021-08-05T14:39:45.802668Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "The factors of 290 are:\n", 42 | "1\n", 43 | "2\n", 44 | "5\n", 45 | "10\n", 46 | "29\n", 47 | "58\n", 48 | "145\n", 49 | "290\n" 50 | ] 51 | }, 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "'\\n>>Output/Runtime Test Cases:\\n\\nThe factors of 290 are:\\n1\\n2\\n5\\n10\\n29\\n58\\n145\\n290\\n'" 56 | ] 57 | }, 58 | "execution_count": 1, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "# Example 1: find the factors of a number\n", 65 | "\n", 66 | "# This function computes the factor of the argument passed\n", 67 | "def print_factors(x):\n", 68 | " print(\"The factors of\",x,\"are:\")\n", 69 | " for i in range(1, x + 1):\n", 70 | " if x % i == 0:\n", 71 | " print(i)\n", 72 | "\n", 73 | "num = 290\n", 74 | "\n", 75 | "print_factors(num)\n", 76 | " \n", 77 | "'''\n", 78 | ">>Output/Runtime Test Cases:\n", 79 | "\n", 80 | "The factors of 290 are:\n", 81 | "1\n", 82 | "2\n", 83 | "5\n", 84 | "10\n", 85 | "29\n", 86 | "58\n", 87 | "145\n", 88 | "290\n", 89 | "'''" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | ">**Note:** To find the factors of another number, change the value of **`num`**.\n", 97 | "\n", 98 | "In this program, the number whose factor is to be found is stored in **`num`**, which is passed to the **`print_factors()`** function. This value is assigned to the variable **`x`** in **`print_factors()`**.\n", 99 | "\n", 100 | "In the function, we use the for loop to iterate from **`i`** equal to **`x`**. If **`x`** is perfectly divisible by **`i`**, it's a factor of **`x`**." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [] 109 | } 110 | ], 111 | "metadata": { 112 | "hide_input": false, 113 | "kernelspec": { 114 | "display_name": "Python 3", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.8.8" 129 | }, 130 | "toc": { 131 | "base_numbering": 1, 132 | "nav_menu": {}, 133 | "number_sections": true, 134 | "sideBar": true, 135 | "skip_h1_title": false, 136 | "title_cell": "Table of Contents", 137 | "title_sidebar": "Contents", 138 | "toc_cell": false, 139 | "toc_position": {}, 140 | "toc_section_display": true, 141 | "toc_window_display": false 142 | }, 143 | "varInspector": { 144 | "cols": { 145 | "lenName": 16, 146 | "lenType": 16, 147 | "lenVar": 40 148 | }, 149 | "kernels_config": { 150 | "python": { 151 | "delete_cmd_postfix": "", 152 | "delete_cmd_prefix": "del ", 153 | "library": "var_list.py", 154 | "varRefreshCmd": "print(var_dic_list())" 155 | }, 156 | "r": { 157 | "delete_cmd_postfix": ") ", 158 | "delete_cmd_prefix": "rm(", 159 | "library": "var_list.r", 160 | "varRefreshCmd": "cat(var_dic_list()) " 161 | } 162 | }, 163 | "types_to_exclude": [ 164 | "module", 165 | "function", 166 | "builtin_function_or_method", 167 | "instance", 168 | "_Feature" 169 | ], 170 | "window_display": false 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 4 175 | } 176 | -------------------------------------------------------------------------------- /04_Python_Functions_examples/010_display_calendar.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Display Calendar\n", 17 | "\n", 18 | "Python has a built-in function, calendar to work with date related tasks. You will learn to display the calendar of a given date in this example.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Modules](https://github.com/milaan9/04_Python_Functions/blob/main/007_Python_Function_Module.ipynb)**\n", 23 | "* **[Python Programming Built-in Functions](https://github.com/milaan9/04_Python_Functions/tree/main/002_Python_Functions_Built_in)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "In the program below, we import the **`calendar`** module. The built-in function **`month()`** inside the module takes in the year and the month and displays the calendar for that month of the year." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": { 37 | "ExecuteTime": { 38 | "end_time": "2021-08-05T15:27:39.869705Z", 39 | "start_time": "2021-08-05T15:27:39.835528Z" 40 | } 41 | }, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | " January 2022\n", 48 | "Mo Tu We Th Fr Sa Su\n", 49 | " 1 2\n", 50 | " 3 4 5 6 7 8 9\n", 51 | "10 11 12 13 14 15 16\n", 52 | "17 18 19 20 21 22 23\n", 53 | "24 25 26 27 28 29 30\n", 54 | "31\n", 55 | "\n" 56 | ] 57 | }, 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "'\\n>>Output/Runtime Test Cases:\\n\\n January 2022\\nMo Tu We Th Fr Sa Su\\n 1 2\\n 3 4 5 6 7 8 9\\n10 11 12 13 14 15 16\\n17 18 19 20 21 22 23\\n24 25 26 27 28 29 30\\n31\\n'" 62 | ] 63 | }, 64 | "execution_count": 1, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "# Example 1: display calendar of the given month and year\n", 71 | "\n", 72 | "# importing calendar module\n", 73 | "import calendar\n", 74 | "\n", 75 | "yyyy = 2022 # year\n", 76 | "mm = 1 # month #'01' will give error\n", 77 | "\n", 78 | "# To take month and year input from the user\n", 79 | "# yyyy = int(input(\"Enter year: \"))\n", 80 | "# mm = int(input(\"Enter month: \"))\n", 81 | "\n", 82 | "# display the calendar\n", 83 | "print(calendar.month(yyyy, mm))\n", 84 | " \n", 85 | "'''\n", 86 | ">>Output/Runtime Test Cases:\n", 87 | "\n", 88 | " January 2022\n", 89 | "Mo Tu We Th Fr Sa Su\n", 90 | " 1 2\n", 91 | " 3 4 5 6 7 8 9\n", 92 | "10 11 12 13 14 15 16\n", 93 | "17 18 19 20 21 22 23\n", 94 | "24 25 26 27 28 29 30\n", 95 | "31\n", 96 | "'''" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | ">**Note:** You can change the value of variables 'yyyy' and 'mm' and run it to test this program for other dates." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | } 113 | ], 114 | "metadata": { 115 | "hide_input": false, 116 | "kernelspec": { 117 | "display_name": "Python 3", 118 | "language": "python", 119 | "name": "python3" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 3 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython3", 131 | "version": "3.8.8" 132 | }, 133 | "toc": { 134 | "base_numbering": 1, 135 | "nav_menu": {}, 136 | "number_sections": true, 137 | "sideBar": true, 138 | "skip_h1_title": false, 139 | "title_cell": "Table of Contents", 140 | "title_sidebar": "Contents", 141 | "toc_cell": false, 142 | "toc_position": {}, 143 | "toc_section_display": true, 144 | "toc_window_display": false 145 | }, 146 | "varInspector": { 147 | "cols": { 148 | "lenName": 16, 149 | "lenType": 16, 150 | "lenVar": 40 151 | }, 152 | "kernels_config": { 153 | "python": { 154 | "delete_cmd_postfix": "", 155 | "delete_cmd_prefix": "del ", 156 | "library": "var_list.py", 157 | "varRefreshCmd": "print(var_dic_list())" 158 | }, 159 | "r": { 160 | "delete_cmd_postfix": ") ", 161 | "delete_cmd_prefix": "rm(", 162 | "library": "var_list.r", 163 | "varRefreshCmd": "cat(var_dic_list()) " 164 | } 165 | }, 166 | "types_to_exclude": [ 167 | "module", 168 | "function", 169 | "builtin_function_or_method", 170 | "instance", 171 | "_Feature" 172 | ], 173 | "window_display": false 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 4 178 | } 179 | -------------------------------------------------------------------------------- /04_Python_Functions_examples/012_find_the_sum_of_natural_numbers_using_recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Find Sum of Natural Numbers Using Recursion\n", 17 | "\n", 18 | "In this example, you'll learn to find the sum of natural numbers using recursive function.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 23 | "* **[Python Functions](https://github.com/milaan9/04_Python_Functions/blob/main/001_Python_Functions.ipynb)**\n", 24 | "* **[Python Recursion](https://github.com/milaan9/04_Python_Functions/blob/main/005_Python_Function_Recursion.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 3, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-08-05T15:47:09.777136Z", 33 | "start_time": "2021-08-05T15:47:09.758577Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "The sum is: 190\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "'\\n>>Output/Runtime Test Cases:\\n\\nThe sum is: 190\\n'" 48 | ] 49 | }, 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# Example 1: find the sum of natural using recursive function\n", 57 | "\n", 58 | "def recur_sum(n):\n", 59 | " if n <= 1:\n", 60 | " return n\n", 61 | " else:\n", 62 | " return n + recur_sum(n-1)\n", 63 | "\n", 64 | "# change this value for a different result\n", 65 | "num = 19\n", 66 | "\n", 67 | "if num < 0:\n", 68 | " print(\"Enter a positive number\")\n", 69 | "else:\n", 70 | " print(\"The sum is:\",recur_sum(num))\n", 71 | " \n", 72 | "'''\n", 73 | ">>Output/Runtime Test Cases:\n", 74 | "\n", 75 | "The sum is: 190\n", 76 | "'''" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | ">**Note:** To test the program for another number, change the value of **`num`**." 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [] 92 | } 93 | ], 94 | "metadata": { 95 | "hide_input": false, 96 | "kernelspec": { 97 | "display_name": "Python 3", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.8.8" 112 | }, 113 | "toc": { 114 | "base_numbering": 1, 115 | "nav_menu": {}, 116 | "number_sections": true, 117 | "sideBar": true, 118 | "skip_h1_title": false, 119 | "title_cell": "Table of Contents", 120 | "title_sidebar": "Contents", 121 | "toc_cell": false, 122 | "toc_position": {}, 123 | "toc_section_display": true, 124 | "toc_window_display": false 125 | }, 126 | "varInspector": { 127 | "cols": { 128 | "lenName": 16, 129 | "lenType": 16, 130 | "lenVar": 40 131 | }, 132 | "kernels_config": { 133 | "python": { 134 | "delete_cmd_postfix": "", 135 | "delete_cmd_prefix": "del ", 136 | "library": "var_list.py", 137 | "varRefreshCmd": "print(var_dic_list())" 138 | }, 139 | "r": { 140 | "delete_cmd_postfix": ") ", 141 | "delete_cmd_prefix": "rm(", 142 | "library": "var_list.r", 143 | "varRefreshCmd": "cat(var_dic_list()) " 144 | } 145 | }, 146 | "types_to_exclude": [ 147 | "module", 148 | "function", 149 | "builtin_function_or_method", 150 | "instance", 151 | "_Feature" 152 | ], 153 | "window_display": false 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 4 158 | } 159 | -------------------------------------------------------------------------------- /04_Python_Functions_examples/014_convert_decimal_number_to_binary_using_recursive_function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Convert Decimal to Binary Using Recursion\n", 17 | "\n", 18 | "In this example, you will learn to convert decimal number to binary using recursive function.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python if-else Statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", 23 | "* **[Python Functions](https://github.com/milaan9/04_Python_Functions/blob/main/001_Python_Functions.ipynb)**\n", 24 | "* **[Python Recursion](https://github.com/milaan9/04_Python_Functions/blob/main/005_Python_Function_Recursion.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.\n", 32 | "\n", 33 | "
\n", 34 | "\n", 35 | "
" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 1, 41 | "metadata": { 42 | "ExecuteTime": { 43 | "end_time": "2021-08-05T18:45:25.618662Z", 44 | "start_time": "2021-08-05T18:45:25.585458Z" 45 | } 46 | }, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "101111 is a Binary equivalent to Decimal of 47\n" 53 | ] 54 | }, 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "'\\n>>Output/Runtime Test Cases:\\n\\n101111 is Binary equivalent to Decimal of 47\\n'" 59 | ] 60 | }, 61 | "execution_count": 1, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "# Example 1: print binary number using recursion\n", 68 | "\n", 69 | "def convertToBinary(n):\n", 70 | " if n > 1:\n", 71 | " convertToBinary(n//2)\n", 72 | " print(n % 2,end = '')\n", 73 | "\n", 74 | "# decimal number\n", 75 | "dec = 47\n", 76 | "\n", 77 | "convertToBinary(dec)\n", 78 | "print(\" is a Binary equivalent to Decimal of\", dec)\n", 79 | " \n", 80 | "'''\n", 81 | ">>Output/Runtime Test Cases:\n", 82 | "\n", 83 | "101111 is Binary equivalent to Decimal of 47\n", 84 | "'''" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | ">**Note:** You can change the variable **`dec`** in the above program and run it to test out for other values.\n", 92 | "\n", 93 | "This program works only for whole numbers. It doesn't work for real numbers having fractional values such as: **`36.6`**, **`9.63`** and so on. We encourage you to create Python program that converts decimal numbers to binary for all real numbers on your own." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [] 102 | } 103 | ], 104 | "metadata": { 105 | "hide_input": false, 106 | "kernelspec": { 107 | "display_name": "Python 3", 108 | "language": "python", 109 | "name": "python3" 110 | }, 111 | "language_info": { 112 | "codemirror_mode": { 113 | "name": "ipython", 114 | "version": 3 115 | }, 116 | "file_extension": ".py", 117 | "mimetype": "text/x-python", 118 | "name": "python", 119 | "nbconvert_exporter": "python", 120 | "pygments_lexer": "ipython3", 121 | "version": "3.8.8" 122 | }, 123 | "toc": { 124 | "base_numbering": 1, 125 | "nav_menu": {}, 126 | "number_sections": true, 127 | "sideBar": true, 128 | "skip_h1_title": false, 129 | "title_cell": "Table of Contents", 130 | "title_sidebar": "Contents", 131 | "toc_cell": false, 132 | "toc_position": {}, 133 | "toc_section_display": true, 134 | "toc_window_display": false 135 | }, 136 | "varInspector": { 137 | "cols": { 138 | "lenName": 16, 139 | "lenType": 16, 140 | "lenVar": 40 141 | }, 142 | "kernels_config": { 143 | "python": { 144 | "delete_cmd_postfix": "", 145 | "delete_cmd_prefix": "del ", 146 | "library": "var_list.py", 147 | "varRefreshCmd": "print(var_dic_list())" 148 | }, 149 | "r": { 150 | "delete_cmd_postfix": ") ", 151 | "delete_cmd_prefix": "rm(", 152 | "library": "var_list.r", 153 | "varRefreshCmd": "cat(var_dic_list()) " 154 | } 155 | }, 156 | "types_to_exclude": [ 157 | "module", 158 | "function", 159 | "builtin_function_or_method", 160 | "instance", 161 | "_Feature" 162 | ], 163 | "window_display": false 164 | } 165 | }, 166 | "nbformat": 4, 167 | "nbformat_minor": 4 168 | } 169 | -------------------------------------------------------------------------------- /04_Python_Functions_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 02 Python Functions Examples 9 | 10 | In this class, you'll find examples related to python functions with a detailed explanations. 11 | -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/001_merge_mails.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Merge Mails\n", 17 | "\n", 18 | "In this example, you'll learn to merge mails into one.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[String Methods](https://github.com/milaan9/02_Python_Datatypes/tree/main/002_Python_String_Methods)**\n", 23 | "* **[Python File I/O](https://github.com/milaan9/05_Python_Files/blob/main/001_Python_File_Input_Output.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "When we want to send the same invitations to many people, the body of the mail does not change. Only the name (and maybe address) needs to be changed.\n", 31 | "\n", 32 | "Mail merge is a process of doing this. Instead of writing each mail separately, we have a template for body of the mail and a list of names that we merge together to form all the mails." 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 1, 38 | "metadata": { 39 | "ExecuteTime": { 40 | "end_time": "2021-08-05T19:16:31.847338Z", 41 | "start_time": "2021-08-05T19:16:31.824879Z" 42 | } 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "# Example 1: mail merger\n", 47 | "# Names are in the file names.txt\n", 48 | "# Body of the mail is in body.txt\n", 49 | "\n", 50 | "# open names.txt for reading\n", 51 | "with open(\"01_names.txt\", 'r', encoding='utf-8') as names_file:\n", 52 | "\n", 53 | " # open body.txt for reading\n", 54 | " with open(\"02_body.txt\", 'r', encoding='utf-8') as body_file:\n", 55 | "\n", 56 | " # read entire content of the body\n", 57 | " body = body_file.read()\n", 58 | "\n", 59 | " # iterate over names\n", 60 | " for name in names_file:\n", 61 | " mail = \"Dear Mr./Ms. \" + name.strip() + \",\" + \"\\n\\n\" + body\n", 62 | "\n", 63 | " # write the mails to individual files\n", 64 | " with open(name.strip()+\".txt\", 'w', encoding='utf-8') as mail_file:\n", 65 | " mail_file.write(mail)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "**Explanation:**\n", 73 | "\n", 74 | "For this program, we have written all the names in separate lines in the file **`names.txt`**. The body is in the **`body.txt`** file.\n", 75 | "\n", 76 | "We open both the files in reading mode and iterate over each name using a for loop. A new file with the name **`[name].txt`** is created, where name is the name of that person.\n", 77 | "\n", 78 | "We use **`strip()`** method to clean up leading and trailing whitespaces (reading a line from the file also reads the newline '\\n' character). Finally, we write the content of the mail into this file using the **`write()`** method.\n", 79 | "\n", 80 | "Learn more about **[files in Python](https://github.com/milaan9/05_Python_Files)**." 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [] 89 | } 90 | ], 91 | "metadata": { 92 | "hide_input": false, 93 | "kernelspec": { 94 | "display_name": "Python 3", 95 | "language": "python", 96 | "name": "python3" 97 | }, 98 | "language_info": { 99 | "codemirror_mode": { 100 | "name": "ipython", 101 | "version": 3 102 | }, 103 | "file_extension": ".py", 104 | "mimetype": "text/x-python", 105 | "name": "python", 106 | "nbconvert_exporter": "python", 107 | "pygments_lexer": "ipython3", 108 | "version": "3.8.8" 109 | }, 110 | "toc": { 111 | "base_numbering": 1, 112 | "nav_menu": {}, 113 | "number_sections": true, 114 | "sideBar": true, 115 | "skip_h1_title": false, 116 | "title_cell": "Table of Contents", 117 | "title_sidebar": "Contents", 118 | "toc_cell": false, 119 | "toc_position": {}, 120 | "toc_section_display": true, 121 | "toc_window_display": false 122 | }, 123 | "varInspector": { 124 | "cols": { 125 | "lenName": 16, 126 | "lenType": 16, 127 | "lenVar": 40 128 | }, 129 | "kernels_config": { 130 | "python": { 131 | "delete_cmd_postfix": "", 132 | "delete_cmd_prefix": "del ", 133 | "library": "var_list.py", 134 | "varRefreshCmd": "print(var_dic_list())" 135 | }, 136 | "r": { 137 | "delete_cmd_postfix": ") ", 138 | "delete_cmd_prefix": "rm(", 139 | "library": "var_list.r", 140 | "varRefreshCmd": "cat(var_dic_list()) " 141 | } 142 | }, 143 | "types_to_exclude": [ 144 | "module", 145 | "function", 146 | "builtin_function_or_method", 147 | "instance", 148 | "_Feature" 149 | ], 150 | "window_display": false 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 4 155 | } 156 | -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/01_names.txt: -------------------------------------------------------------------------------- 1 | Arthur 2 | Brandon 3 | Charles 4 | David 5 | Ethan -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/02_body.txt: -------------------------------------------------------------------------------- 1 | I hope this email finds you well. 2 | 3 | I am pleased to extend the following offer of employment to you on behalf of Google. You have been selected as the best candidate for the Data Scientist position. 4 | 5 | Congratulations! 6 | We believe that your knowledge, skills and experience would be an ideal fit for our Data Science team. We hope you will enjoy your role and make a significant contribution to the overall success of Google. 7 | 8 | Please take the time to review our offer letter in the attachment. It includes important details about your compensation, benefits and the terms and conditions of your anticipated employment with Google. 9 | 10 | Hope to hear from you soon! 11 | 12 | Google Hiring Team. -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/Arthur.txt: -------------------------------------------------------------------------------- 1 | Dear Mr./Ms. Arthur, 2 | 3 | I hope this email finds you well. 4 | 5 | I am pleased to extend the following offer of employment to you on behalf of Google. You have been selected as the best candidate for the Data Scientist position. 6 | 7 | Congratulations! 8 | We believe that your knowledge, skills and experience would be an ideal fit for our Data Science team. We hope you will enjoy your role and make a significant contribution to the overall success of Google. 9 | 10 | Please take the time to review our offer letter in the attachment. It includes important details about your compensation, benefits and the terms and conditions of your anticipated employment with Google. 11 | 12 | Hope to hear from you soon! 13 | 14 | Google Hiring Team. -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/Brandon.txt: -------------------------------------------------------------------------------- 1 | Dear Mr./Ms. Brandon, 2 | 3 | I hope this email finds you well. 4 | 5 | I am pleased to extend the following offer of employment to you on behalf of Google. You have been selected as the best candidate for the Data Scientist position. 6 | 7 | Congratulations! 8 | We believe that your knowledge, skills and experience would be an ideal fit for our Data Science team. We hope you will enjoy your role and make a significant contribution to the overall success of Google. 9 | 10 | Please take the time to review our offer letter in the attachment. It includes important details about your compensation, benefits and the terms and conditions of your anticipated employment with Google. 11 | 12 | Hope to hear from you soon! 13 | 14 | Google Hiring Team. -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/Charles.txt: -------------------------------------------------------------------------------- 1 | Dear Mr./Ms. Charles, 2 | 3 | I hope this email finds you well. 4 | 5 | I am pleased to extend the following offer of employment to you on behalf of Google. You have been selected as the best candidate for the Data Scientist position. 6 | 7 | Congratulations! 8 | We believe that your knowledge, skills and experience would be an ideal fit for our Data Science team. We hope you will enjoy your role and make a significant contribution to the overall success of Google. 9 | 10 | Please take the time to review our offer letter in the attachment. It includes important details about your compensation, benefits and the terms and conditions of your anticipated employment with Google. 11 | 12 | Hope to hear from you soon! 13 | 14 | Google Hiring Team. -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/David.txt: -------------------------------------------------------------------------------- 1 | Dear Mr./Ms. David, 2 | 3 | I hope this email finds you well. 4 | 5 | I am pleased to extend the following offer of employment to you on behalf of Google. You have been selected as the best candidate for the Data Scientist position. 6 | 7 | Congratulations! 8 | We believe that your knowledge, skills and experience would be an ideal fit for our Data Science team. We hope you will enjoy your role and make a significant contribution to the overall success of Google. 9 | 10 | Please take the time to review our offer letter in the attachment. It includes important details about your compensation, benefits and the terms and conditions of your anticipated employment with Google. 11 | 12 | Hope to hear from you soon! 13 | 14 | Google Hiring Team. -------------------------------------------------------------------------------- /05_Python_Files_examples/001_merge_mails/Ethan.txt: -------------------------------------------------------------------------------- 1 | Dear Mr./Ms. Ethan, 2 | 3 | I hope this email finds you well. 4 | 5 | I am pleased to extend the following offer of employment to you on behalf of Google. You have been selected as the best candidate for the Data Scientist position. 6 | 7 | Congratulations! 8 | We believe that your knowledge, skills and experience would be an ideal fit for our Data Science team. We hope you will enjoy your role and make a significant contribution to the overall success of Google. 9 | 10 | Please take the time to review our offer letter in the attachment. It includes important details about your compensation, benefits and the terms and conditions of your anticipated employment with Google. 11 | 12 | Hope to hear from you soon! 13 | 14 | Google Hiring Team. -------------------------------------------------------------------------------- /05_Python_Files_examples/002_find_the_size_of_a_image/jupyter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/05_Python_Files_examples/002_find_the_size_of_a_image/jupyter.jpg -------------------------------------------------------------------------------- /05_Python_Files_examples/002_find_the_size_of_a_image/jupyter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/05_Python_Files_examples/002_find_the_size_of_a_image/jupyter.png -------------------------------------------------------------------------------- /05_Python_Files_examples/003_find_the_hash_of_a_file/track1.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/05_Python_Files_examples/003_find_the_hash_of_a_file/track1.mp3 -------------------------------------------------------------------------------- /05_Python_Files_examples/004_safely_create_a_nested_directory/img/subDir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/05_Python_Files_examples/004_safely_create_a_nested_directory/img/subDir.png -------------------------------------------------------------------------------- /05_Python_Files_examples/006_copy_a_file/006_copy_a_file.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Copy a File\n", 17 | "\n", 18 | "In this example, you will learn to copy the content of a file to another file using Python.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python File I/O](https://github.com/milaan9/05_Python_Files/blob/main/001_Python_File_Input_Output.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "Multiple exceptions can be caught using a tuple. The errors can be passed through a tuple as shown in example below." 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## Using shutil module" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "# Example 1: \n", 46 | "\n", 47 | "string = input()\n", 48 | "from shutil import copyfile\n", 49 | "copyfile(\"/root/a.txt\", \"/root/b.txt\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": { 55 | "ExecuteTime": { 56 | "end_time": "2021-08-07T06:44:37.674408Z", 57 | "start_time": "2021-08-07T06:44:37.359960Z" 58 | } 59 | }, 60 | "source": [ 61 | "**Explanation:**\n", 62 | "\n", 63 | "The first parameter of **`copyfile()`** is the path of the source file and the second parameter is the path of the destination file. The content of the destination file is replaced with the content of the source file.\n", 64 | "\n", 65 | "There are other methods **`copy()`**, **`copy2()`**, and **`copyfileobj()`** which serve the same purpose with some metadata changes.\n", 66 | "\n", 67 | "| Method | Preserves Permissions | Supports Directory as Destination | Copies Metadata |Supports file object |\n", 68 | "|:------:|:------:|:------:|:------:|:------:|\n", 69 | "|**copy()** | Yes | Yes | No | No |\n", 70 | "|**copyfile()** | No | No | No | No |\n", 71 | "|**copy2()** | Yes | Yes | Yes| No |\n", 72 | "|**copyfileobj()**| No | No | No | Yes|" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [] 81 | } 82 | ], 83 | "metadata": { 84 | "hide_input": false, 85 | "kernelspec": { 86 | "display_name": "Python 3", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.8.8" 101 | }, 102 | "toc": { 103 | "base_numbering": 1, 104 | "nav_menu": {}, 105 | "number_sections": true, 106 | "sideBar": true, 107 | "skip_h1_title": false, 108 | "title_cell": "Table of Contents", 109 | "title_sidebar": "Contents", 110 | "toc_cell": false, 111 | "toc_position": {}, 112 | "toc_section_display": true, 113 | "toc_window_display": false 114 | }, 115 | "varInspector": { 116 | "cols": { 117 | "lenName": 16, 118 | "lenType": 16, 119 | "lenVar": 40 120 | }, 121 | "kernels_config": { 122 | "python": { 123 | "delete_cmd_postfix": "", 124 | "delete_cmd_prefix": "del ", 125 | "library": "var_list.py", 126 | "varRefreshCmd": "print(var_dic_list())" 127 | }, 128 | "r": { 129 | "delete_cmd_postfix": ") ", 130 | "delete_cmd_prefix": "rm(", 131 | "library": "var_list.r", 132 | "varRefreshCmd": "cat(var_dic_list()) " 133 | } 134 | }, 135 | "types_to_exclude": [ 136 | "module", 137 | "function", 138 | "builtin_function_or_method", 139 | "instance", 140 | "_Feature" 141 | ], 142 | "window_display": false 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 4 147 | } 148 | -------------------------------------------------------------------------------- /05_Python_Files_examples/007_read_a_file_line_by_line_into_a_list/vaccine_data.csv: -------------------------------------------------------------------------------- 1 | No.,Vaccine,Developed by,Country,Year,,,,,,,,,,,,,,,,, 2 | 1,Small Pox,Edward Jenner,England,1796,,,,,,,,,,,,,,,,, 3 | 2,Cholera,Louis Pasteur,France,1880,,,,,,,,,,,,,,,,, 4 | 3,Diphtheria and Tetanus,Emil Adolf Von Behring and Shibasaburo Kitasato,Germany/Japan,1891,,,,,,,,,,,,,,,,, 5 | 4,Rubella virus,rubella and CRS,Germany,1740,,,,,,,,,,,,,,,,, 6 | 5,TB Vaccine,Albert Calmette and Camille Guerin,France,1922,,,,,,,,,,,,,,,,, 7 | 6,Polio Vaccine,Jones E. Salk,US,1952,,,,,,,,,,,,,,,,, 8 | 7,Rotavirus,Rotarix by GlaxoSmithKline,N/A,2009,,,,,,,,,,,,,,,,, 9 | 8,Oral Polio Vaccine,Albert Bruce Sabin,US,1955,,,,,,,,,,,,,,,,, 10 | 9,Measles Vaccine,"John F. Enders, Thomas peeble",US,1953,,,,,,,,,,,,,,,,, 11 | 10,Rabies Vaccine,Louis Pasteur,France,1885,,,,,,,,,,,,,,,,, 12 | 11,Typhus Vaccine,Charles Nicolle,France,1909,,,,,,,,,,,,,,,,, 13 | 12,Pneumococcal Pneumonia,William Osler,US,1977,,,,,,,,,,,,,,,,, 14 | 13,Typhoid Fever,Almroth Edward Wright,N/A,1880,,,,,,,,,,,,,,,,, 15 | 14,Meningococcal meningitis,Vieusseux,N/A,1806,,,,,,,,,,,,,,,,, 16 | 15,Tuberculosis,Albert Calmette and Camille Guerin,N/A,1906,,,,,,,,,,,,,,,,, 17 | 16,"Epiglottitis, meningitis, pneumonia",David Smith,N/A,N/A,,,,,,,,,,,,,,,,, 18 | 17,Q Fever,H.R. Cox and Gordon Davis,USA,1937,,,,,,,,,,,,,,,,, 19 | 18,Whooping Cough,Jules Bordet and Octave Gengou,France,100<,,,,,,,,,,,,,,,,, 20 | 19,Anthrax,Louis Pasteur,France,1881,,,,,,,,,,,,,,,,, 21 | 20,Leprosy,Guy Henry Faget,Sweden,1873,,,,,,,,,,,,,,,,, 22 | 21,Hepatitis A,Steven M. Feinstone,US,1973,,,,,,,,,,,,,,,,, 23 | 22,Hepatitis B,Dr. Blumberg won,US,1969,,,,,,,,,,,,,,,,, 24 | 23,Hepatitis E,N/A,Inidia,1978,,,,,,,,,,,,,,,,, 25 | 24,"Cervical cancer, Genital warts, anogenital cancers",Ian Frazer,US,2006,,,,,,,,,,,,,,,,, 26 | 25,Influenza,Jonas Salk & Thomas Francis,US,1933,,,,,,,,,,,,,,,,,Country 27 | 26,Mumps,Maurice Hilleman,N/A,1969,,,,,,,,,,,,,,,,, 28 | 27,Poliomyelitis,Dr. Jonas Salk,US,1952,,,,,,,,,,,,,,,,, 29 | 28,"Chicken pox, Shingles",Michiaki Takahashi,N/A,1972,,,,,,,,,,,,,,,,, 30 | 29,Yellow Fever,Max Theiler,USA,1930,,,,,,,,,,,,,,,,, 31 | -------------------------------------------------------------------------------- /05_Python_Files_examples/007_read_a_file_line_by_line_into_a_list/vaccine_data.txt: -------------------------------------------------------------------------------- 1 | Small-Pox Edward-Jenner England-1796 2 | Cholera Louis-Pasteur France-1880 3 | Polio Jones-Salk US-1952 4 | 5 | -------------------------------------------------------------------------------- /05_Python_Files_examples/008_append_to_a_file/008_append_to_a_file.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Append to a File\n", 17 | "\n", 18 | "In this example, you will learn to append to a file.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python File I/O](https://github.com/milaan9/05_Python_Files/blob/main/001_Python_File_Input_Output.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "Let the content of the file **`vaccine_data.txt`** be:\n", 30 | "\n", 31 | "```\n", 32 | "Small-Pox Edward-Jenner England-1796\n", 33 | "Cholera Louis-Pasteur France-1880\n", 34 | "Polio Jones-Salk US-1952\n", 35 | "```" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "## Open file in append mode and write to it" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 1, 48 | "metadata": { 49 | "ExecuteTime": { 50 | "end_time": "2021-08-07T08:52:54.438131Z", 51 | "start_time": "2021-08-07T08:52:54.419576Z" 52 | } 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "# Example 1: \n", 57 | "\n", 58 | "with open(\"vaccine_data.txt\", \"a\") as f:\n", 59 | " f.write(\"new_text\") " 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "The content of the file after appending a text to it is:\n", 67 | "\n", 68 | "```\n", 69 | "Small-Pox Edward-Jenner England-1796\n", 70 | "Cholera Louis-Pasteur France-1880\n", 71 | "Polio Jones-Salk US-1952new_text\n", 72 | "```" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": { 78 | "ExecuteTime": { 79 | "end_time": "2021-08-07T06:44:37.674408Z", 80 | "start_time": "2021-08-07T06:44:37.359960Z" 81 | } 82 | }, 83 | "source": [ 84 | "**Explanation:**\n", 85 | "\n", 86 | "Open the file in append **`'a'`** mode, and write to it using **`write()`** method. Inside **`write()`** method, a string **`\"new_text\"`** is passed. This text is seen on the file as shown above." 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": { 92 | "ExecuteTime": { 93 | "end_time": "2021-08-07T06:44:37.674408Z", 94 | "start_time": "2021-08-07T06:44:37.359960Z" 95 | } 96 | }, 97 | "source": [ 98 | "If you want to learn more about different types of file opening modes, please refer to **[Python file I/O](https://github.com/milaan9/05_Python_Files/blob/main/001_Python_File_Input_Output.ipynb)**." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [] 107 | } 108 | ], 109 | "metadata": { 110 | "hide_input": false, 111 | "kernelspec": { 112 | "display_name": "Python 3", 113 | "language": "python", 114 | "name": "python3" 115 | }, 116 | "language_info": { 117 | "codemirror_mode": { 118 | "name": "ipython", 119 | "version": 3 120 | }, 121 | "file_extension": ".py", 122 | "mimetype": "text/x-python", 123 | "name": "python", 124 | "nbconvert_exporter": "python", 125 | "pygments_lexer": "ipython3", 126 | "version": "3.8.8" 127 | }, 128 | "toc": { 129 | "base_numbering": 1, 130 | "nav_menu": {}, 131 | "number_sections": true, 132 | "sideBar": true, 133 | "skip_h1_title": false, 134 | "title_cell": "Table of Contents", 135 | "title_sidebar": "Contents", 136 | "toc_cell": false, 137 | "toc_position": {}, 138 | "toc_section_display": true, 139 | "toc_window_display": false 140 | }, 141 | "varInspector": { 142 | "cols": { 143 | "lenName": 16, 144 | "lenType": 16, 145 | "lenVar": 40 146 | }, 147 | "kernels_config": { 148 | "python": { 149 | "delete_cmd_postfix": "", 150 | "delete_cmd_prefix": "del ", 151 | "library": "var_list.py", 152 | "varRefreshCmd": "print(var_dic_list())" 153 | }, 154 | "r": { 155 | "delete_cmd_postfix": ") ", 156 | "delete_cmd_prefix": "rm(", 157 | "library": "var_list.r", 158 | "varRefreshCmd": "cat(var_dic_list()) " 159 | } 160 | }, 161 | "types_to_exclude": [ 162 | "module", 163 | "function", 164 | "builtin_function_or_method", 165 | "instance", 166 | "_Feature" 167 | ], 168 | "window_display": false 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 4 173 | } 174 | -------------------------------------------------------------------------------- /05_Python_Files_examples/008_append_to_a_file/vaccine_data.txt: -------------------------------------------------------------------------------- 1 | Small-Pox Edward-Jenner England-1796 2 | Cholera Louis-Pasteur France-1880 3 | Polio Jones-Salk US-1952new_text -------------------------------------------------------------------------------- /05_Python_Files_examples/009_extract_extension_from_the_file_name/vaccine_data.txt: -------------------------------------------------------------------------------- 1 | Small-Pox Edward-Jenner England-1796 2 | Cholera Louis-Pasteur France-1880 3 | Polio Jones-Salk US-1952new_text -------------------------------------------------------------------------------- /05_Python_Files_examples/013_get_file_creation_and_modification_date/Digital_Clock.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Python Program to Create Digital Clock 3 | by Dr. Milaan Parmar 4 | ''' 5 | # Import necessary modules! 6 | from tkinter import * 7 | from tkinter.ttk import * 8 | 9 | # Import time 10 | from time import strftime 11 | 12 | # Create UI for our digital clock. 13 | root = Tk() 14 | 15 | # Set the title of our clock using title method. 16 | root.title("Digital clock") 17 | 18 | # Define a clock function to get the time 19 | # Use strftime method to get the time and store it inside a string and name the string as tick 20 | def clock(): 21 | tick = strftime("%H:%M:%S %p") 22 | 23 | # Set the label using config method. 24 | label.config(text =tick) 25 | 26 | # Call our clock function and use the after method to do the same 27 | label.after(1000, clock) 28 | 29 | # Use label method to store our title. 30 | label = Label(root, font = ("segoe", 60), foreground = "yellow", background = "black") 31 | 32 | # Use pack method to pack our label 33 | # Define the alignment of the label using the anchor method. 34 | label.pack(anchor= "center") 35 | 36 | # Call our clock function and at the end we will call it mainloop 37 | clock() 38 | mainloop() -------------------------------------------------------------------------------- /05_Python_Files_examples/014_get_the_full_path_of_the_current_working_directory/014_get_the_full_path_of_the_current_working_directory.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this **Python Examples** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Get the Full Path of the Current Working Directory\n", 17 | "\n", 18 | "In this example, you will learn to get the full path of the current working directory.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python File I/O](https://github.com/milaan9/05_Python_Files/blob/main/001_Python_File_Input_Output.ipynb)**\n", 23 | "* **[Python Directory and Files Management](https://github.com/milaan9/05_Python_Files/blob/main/002_Python_File_Directory.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "# Example 1: Using pathlib module\n", 33 | "\n", 34 | "import pathlib\n", 35 | "\n", 36 | "# path of the given file\n", 37 | "print(pathlib.Path(\"my_file.txt\").parent.absolute())\n", 38 | "\n", 39 | "# current working directory\n", 40 | "print(pathlib.Path().absolute())\n", 41 | "\n", 42 | "'''\n", 43 | ">>Output/Runtime Test Cases:\n", 44 | "\n", 45 | "/Users/username\n", 46 | "/Users/username\n", 47 | "'''" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "ExecuteTime": { 54 | "end_time": "2021-08-07T06:44:37.674408Z", 55 | "start_time": "2021-08-07T06:44:37.359960Z" 56 | } 57 | }, 58 | "source": [ 59 | "**Explanation:**\n", 60 | "\n", 61 | "Using the **`pathlib`** module, you can get the current working directory.\n", 62 | "\n", 63 | "* Pass the file's name in **`Path()`** method.\n", 64 | "* **`parent`** gives the logical parent of the path and **`absolute()`** gives the absolute path of the file.\n", 65 | "* **`pathlib.Path().absolute()`** gives the current working directory." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# Example 2: Using os module\n", 75 | "\n", 76 | "import os\n", 77 | "\n", 78 | "# path of the given file\n", 79 | "print(os.path.dirname(os.path.abspath(\"my_file.txt\")))\n", 80 | "\n", 81 | "# current working directory\n", 82 | "print(os.path.abspath(os.getcwd()))\n", 83 | "\n", 84 | "'''\n", 85 | ">>Output/Runtime Test Cases:\n", 86 | "\n", 87 | "/Users/username\n", 88 | "/Users/username\n", 89 | "'''" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": { 95 | "ExecuteTime": { 96 | "end_time": "2021-08-07T06:44:37.674408Z", 97 | "start_time": "2021-08-07T06:44:37.359960Z" 98 | } 99 | }, 100 | "source": [ 101 | "**Explanation:**\n", 102 | "\n", 103 | "You can do the same thing with the **`os`** module.\n", 104 | "\n", 105 | "* Use **`abspath()`** method to get an absolute path.\n", 106 | "* **`getcwd()`** gives the current working directory." 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "hide_input": false, 119 | "kernelspec": { 120 | "display_name": "Python 3", 121 | "language": "python", 122 | "name": "python3" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 3 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython3", 134 | "version": "3.8.8" 135 | }, 136 | "toc": { 137 | "base_numbering": 1, 138 | "nav_menu": {}, 139 | "number_sections": true, 140 | "sideBar": true, 141 | "skip_h1_title": false, 142 | "title_cell": "Table of Contents", 143 | "title_sidebar": "Contents", 144 | "toc_cell": false, 145 | "toc_position": {}, 146 | "toc_section_display": true, 147 | "toc_window_display": false 148 | }, 149 | "varInspector": { 150 | "cols": { 151 | "lenName": 16, 152 | "lenType": 16, 153 | "lenVar": 40 154 | }, 155 | "kernels_config": { 156 | "python": { 157 | "delete_cmd_postfix": "", 158 | "delete_cmd_prefix": "del ", 159 | "library": "var_list.py", 160 | "varRefreshCmd": "print(var_dic_list())" 161 | }, 162 | "r": { 163 | "delete_cmd_postfix": ") ", 164 | "delete_cmd_prefix": "rm(", 165 | "library": "var_list.r", 166 | "varRefreshCmd": "cat(var_dic_list()) " 167 | } 168 | }, 169 | "types_to_exclude": [ 170 | "module", 171 | "function", 172 | "builtin_function_or_method", 173 | "instance", 174 | "_Feature" 175 | ], 176 | "window_display": false 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 4 181 | } 182 | -------------------------------------------------------------------------------- /05_Python_Files_examples/015_check_the_file_size/vaccine_data.txt: -------------------------------------------------------------------------------- 1 | Small-Pox Edward-Jenner England-1796 2 | Cholera Louis-Pasteur France-1880 3 | Polio Jones-Salk US-1952new_text -------------------------------------------------------------------------------- /05_Python_Files_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 02 Python Files Examples 9 | 10 | In this class, you'll find examples related to python files with a detailed explanations. 11 | -------------------------------------------------------------------------------- /06_Python_Object_Oriented_examples/001 Get the Class Name of an Instance.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Get the Class Name of an Instance\n", 17 | "\n", 18 | "In this example, you will learn to get the class name of an instance.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Object Oriented Programming](https://github.com/milaan9/06_Python_Object_Class/blob/main/001_Python_OOPs_Concepts.ipynb)**\n", 23 | "* **[Python Objects and Classes](https://github.com/milaan9/06_Python_Object_Class/blob/main/002_Python_Classes_and_Objects.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": { 30 | "ExecuteTime": { 31 | "end_time": "2021-07-02T12:15:12.649468Z", 32 | "start_time": "2021-07-02T12:15:12.624078Z" 33 | } 34 | }, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "Vehicle\n" 41 | ] 42 | }, 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "'\\n>>Expected output:\\n \\nVehicle\\n'" 47 | ] 48 | }, 49 | "execution_count": 1, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "# Example 1: Using __class__.__name__\n", 56 | "\n", 57 | "class Vehicle:\n", 58 | " def name(self, name):\n", 59 | " return name\n", 60 | " \n", 61 | "v = Vehicle()\n", 62 | "print(v.__class__.__name__)\n", 63 | "\n", 64 | "'''\n", 65 | ">>Expected output:\n", 66 | " \n", 67 | "Vehicle\n", 68 | "'''" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "The execution time depends on the system." 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 2, 81 | "metadata": { 82 | "ExecuteTime": { 83 | "end_time": "2021-07-02T12:15:12.789113Z", 84 | "start_time": "2021-07-02T12:15:12.652399Z" 85 | } 86 | }, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "Vehicle\n" 93 | ] 94 | }, 95 | { 96 | "data": { 97 | "text/plain": [ 98 | "'\\n>>Expected output:\\n \\nVehicle\\n'" 99 | ] 100 | }, 101 | "execution_count": 2, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "# Example 2: Using type() and __name__ attribute\n", 108 | "\n", 109 | "class Vehicle:\n", 110 | " def name(self, name):\n", 111 | " return name\n", 112 | "\n", 113 | "v = Vehicle()\n", 114 | "print(type(v).__name__)\n", 115 | "\n", 116 | "\n", 117 | "'''\n", 118 | ">>Expected output:\n", 119 | " \n", 120 | "Vehicle\n", 121 | "'''" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "Using attribute **`__name__`** with **`type()`**, you can get the class name of an instance/object." 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | } 138 | ], 139 | "metadata": { 140 | "hide_input": false, 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.8.8" 157 | }, 158 | "toc": { 159 | "base_numbering": 1, 160 | "nav_menu": {}, 161 | "number_sections": true, 162 | "sideBar": true, 163 | "skip_h1_title": false, 164 | "title_cell": "Table of Contents", 165 | "title_sidebar": "Contents", 166 | "toc_cell": false, 167 | "toc_position": {}, 168 | "toc_section_display": true, 169 | "toc_window_display": false 170 | }, 171 | "varInspector": { 172 | "cols": { 173 | "lenName": 16, 174 | "lenType": 16, 175 | "lenVar": 40 176 | }, 177 | "kernels_config": { 178 | "python": { 179 | "delete_cmd_postfix": "", 180 | "delete_cmd_prefix": "del ", 181 | "library": "var_list.py", 182 | "varRefreshCmd": "print(var_dic_list())" 183 | }, 184 | "r": { 185 | "delete_cmd_postfix": ") ", 186 | "delete_cmd_prefix": "rm(", 187 | "library": "var_list.r", 188 | "varRefreshCmd": "cat(var_dic_list()) " 189 | } 190 | }, 191 | "types_to_exclude": [ 192 | "module", 193 | "function", 194 | "builtin_function_or_method", 195 | "instance", 196 | "_Feature" 197 | ], 198 | "window_display": false 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 4 203 | } 204 | -------------------------------------------------------------------------------- /06_Python_Object_Oriented_examples/002 Differentiate Between type() and isinstance().ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Differentiate Between `type()` and `isinstance()`\n", 17 | "\n", 18 | "In this example, you will learn to differentiate between **`type()`** and **`isinstance()`**.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Object Oriented Programming](https://github.com/milaan9/06_Python_Object_Class/blob/main/001_Python_OOPs_Concepts.ipynb)**\n", 23 | "* **[Python Objects and Classes](https://github.com/milaan9/06_Python_Object_Class/blob/main/002_Python_Classes_and_Objects.ipynb)**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": { 30 | "ExecuteTime": { 31 | "end_time": "2021-07-02T12:53:51.842225Z", 32 | "start_time": "2021-07-02T12:53:51.804628Z" 33 | } 34 | }, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "True\n", 41 | "False\n", 42 | "True\n", 43 | "True\n" 44 | ] 45 | }, 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "'\\n>>Expected output:\\n \\nTrue\\nFalse\\nTrue\\nTrue\\n'" 50 | ] 51 | }, 52 | "execution_count": 1, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "# Difference between type() and instance()\n", 59 | "# Let's understand the difference between type() and instance() with the example code below.\n", 60 | "\n", 61 | "class Polygon:\n", 62 | " def sides_no(self):\n", 63 | " pass\n", 64 | "\n", 65 | "class Triangle(Polygon):\n", 66 | " def area(self):\n", 67 | " pass\n", 68 | "\n", 69 | "obj_polygon = Polygon()\n", 70 | "obj_triangle = Triangle()\n", 71 | "\n", 72 | "print(type(obj_triangle) == Triangle) \t# true\n", 73 | "print(type(obj_triangle) == Polygon) \t# false\n", 74 | "\n", 75 | "print(isinstance(obj_polygon, Polygon)) \t# true\n", 76 | "print(isinstance(obj_triangle, Polygon))\t# true\n", 77 | "\n", 78 | "'''\n", 79 | ">>Expected output:\n", 80 | " \n", 81 | "True\n", 82 | "False\n", 83 | "True\n", 84 | "True\n", 85 | "'''" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "**Explanation:**\n", 93 | "\n", 94 | "In the above example, we see that **`type()`** cannot distinguish whether an instance of a class is somehow related to the base class. In our case, although **`obj_triangle`** is an instance of child class **`Triangle`**, it is inherited from the base class **`Polygon`**. If you want to relate the object of a child class with the base class, you can achieve this with **`instance()`**." 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [] 103 | } 104 | ], 105 | "metadata": { 106 | "hide_input": false, 107 | "kernelspec": { 108 | "display_name": "Python 3", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.8.8" 123 | }, 124 | "toc": { 125 | "base_numbering": 1, 126 | "nav_menu": {}, 127 | "number_sections": true, 128 | "sideBar": true, 129 | "skip_h1_title": false, 130 | "title_cell": "Table of Contents", 131 | "title_sidebar": "Contents", 132 | "toc_cell": false, 133 | "toc_position": {}, 134 | "toc_section_display": true, 135 | "toc_window_display": false 136 | }, 137 | "varInspector": { 138 | "cols": { 139 | "lenName": 16, 140 | "lenType": 16, 141 | "lenVar": 40 142 | }, 143 | "kernels_config": { 144 | "python": { 145 | "delete_cmd_postfix": "", 146 | "delete_cmd_prefix": "del ", 147 | "library": "var_list.py", 148 | "varRefreshCmd": "print(var_dic_list())" 149 | }, 150 | "r": { 151 | "delete_cmd_postfix": ") ", 152 | "delete_cmd_prefix": "rm(", 153 | "library": "var_list.r", 154 | "varRefreshCmd": "cat(var_dic_list()) " 155 | } 156 | }, 157 | "types_to_exclude": [ 158 | "module", 159 | "function", 160 | "builtin_function_or_method", 161 | "instance", 162 | "_Feature" 163 | ], 164 | "window_display": false 165 | } 166 | }, 167 | "nbformat": 4, 168 | "nbformat_minor": 4 169 | } 170 | -------------------------------------------------------------------------------- /06_Python_Object_Oriented_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 06 Python Object Oriented examples 9 | 10 | In this class, you'll find examples related to python object oriented with a detailed explanations. 11 | -------------------------------------------------------------------------------- /07_Python_Advanced_examples/001 Measure the Elapsed Time in Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples/tree/main/07_Python_Advanced_examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Measure the Elapsed Time in Python\n", 17 | "\n", 18 | "In this example, you will learn to measure the elapsed time.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python time Module](https://github.com/milaan9/08_Python_Date_Time_Module/blob/main/007_Python_time_Module.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 3, 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-07-02T06:58:59.045097Z", 31 | "start_time": "2021-07-02T06:58:59.034355Z" 32 | } 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "52.9\n", 40 | "0.0\n" 41 | ] 42 | }, 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "'\\n>>Expected output:\\n \\n52.9\\n0.0\\n'" 47 | ] 48 | }, 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "# Example 1: Using time module\n", 56 | "\n", 57 | "import time\n", 58 | "\n", 59 | "start = time.time()\n", 60 | "\n", 61 | "print(23*2.3)\n", 62 | "\n", 63 | "end = time.time()\n", 64 | "print(end - start)\n", 65 | "\n", 66 | "'''\n", 67 | ">>Expected output:\n", 68 | " \n", 69 | "52.9\n", 70 | "0.0\n", 71 | "'''" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "The execution time depends on the system." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": { 85 | "ExecuteTime": { 86 | "end_time": "2021-07-02T06:59:36.437432Z", 87 | "start_time": "2021-07-02T06:59:36.418880Z" 88 | } 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "52.9\n", 96 | "0.00035429999996949846\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "# Example 2: Using timeit module\n", 102 | "\n", 103 | "from timeit import default_timer as timer\n", 104 | "\n", 105 | "start = timer()\n", 106 | "\n", 107 | "print(23*2.3)\n", 108 | "\n", 109 | "end = timer()\n", 110 | "print(end - start)\n", 111 | "\n", 112 | "\n", 113 | "'''\n", 114 | ">>Expected output:\n", 115 | " \n", 116 | "52.9\n", 117 | "0.00035429999996949846\n", 118 | "'''" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "**`timeit`** provides the most accurate results. " 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [] 134 | } 135 | ], 136 | "metadata": { 137 | "hide_input": false, 138 | "kernelspec": { 139 | "display_name": "Python 3", 140 | "language": "python", 141 | "name": "python3" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 3 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython3", 153 | "version": "3.8.8" 154 | }, 155 | "toc": { 156 | "base_numbering": 1, 157 | "nav_menu": {}, 158 | "number_sections": true, 159 | "sideBar": true, 160 | "skip_h1_title": false, 161 | "title_cell": "Table of Contents", 162 | "title_sidebar": "Contents", 163 | "toc_cell": false, 164 | "toc_position": {}, 165 | "toc_section_display": true, 166 | "toc_window_display": false 167 | }, 168 | "varInspector": { 169 | "cols": { 170 | "lenName": 16, 171 | "lenType": 16, 172 | "lenVar": 40 173 | }, 174 | "kernels_config": { 175 | "python": { 176 | "delete_cmd_postfix": "", 177 | "delete_cmd_prefix": "del ", 178 | "library": "var_list.py", 179 | "varRefreshCmd": "print(var_dic_list())" 180 | }, 181 | "r": { 182 | "delete_cmd_postfix": ") ", 183 | "delete_cmd_prefix": "rm(", 184 | "library": "var_list.r", 185 | "varRefreshCmd": "cat(var_dic_list()) " 186 | } 187 | }, 188 | "types_to_exclude": [ 189 | "module", 190 | "function", 191 | "builtin_function_or_method", 192 | "instance", 193 | "_Feature" 194 | ], 195 | "window_display": false 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 4 200 | } 201 | -------------------------------------------------------------------------------- /07_Python_Advanced_examples/002 Represent enum.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples/tree/main/07_Python_Advanced_examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Represent enum\n", 17 | "\n", 18 | "In this example, you will learn to represent enum.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Object Oriented Programming](https://github.com/milaan9/06_Python_Object_Class/blob/main/001_Python_OOPs_Concepts.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-07-02T07:02:44.292185Z", 31 | "start_time": "2021-07-02T07:02:44.258984Z" 32 | } 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "Day.MONDAY\n", 40 | "MONDAY\n", 41 | "1\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "'\\n>>Expected output:\\n \\n52.9\\n0.0\\n'" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# Example 1: Using enum module\n", 57 | "\n", 58 | "from enum import Enum\n", 59 | "\n", 60 | "class Day(Enum):\n", 61 | " MONDAY = 1\n", 62 | " TUESDAY = 2\n", 63 | " WEDNESDAY = 3\n", 64 | "\n", 65 | "# print the enum member\n", 66 | "print(Day.MONDAY)\n", 67 | "\n", 68 | "# get the name of the enum member\n", 69 | "print(Day.MONDAY.name)\n", 70 | "\n", 71 | "# get the value of the enum member\n", 72 | "print(Day.MONDAY.value)\n", 73 | "\n", 74 | "'''\n", 75 | ">>Expected output:\n", 76 | " \n", 77 | "Day.MONDAY\n", 78 | "MONDAY\n", 79 | "1\n", 80 | "'''" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "You can refer to the official documentation of **[enum](https://docs.python.org/3/library/enum.html)** for more information." 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "hide_input": false, 100 | "kernelspec": { 101 | "display_name": "Python 3", 102 | "language": "python", 103 | "name": "python3" 104 | }, 105 | "language_info": { 106 | "codemirror_mode": { 107 | "name": "ipython", 108 | "version": 3 109 | }, 110 | "file_extension": ".py", 111 | "mimetype": "text/x-python", 112 | "name": "python", 113 | "nbconvert_exporter": "python", 114 | "pygments_lexer": "ipython3", 115 | "version": "3.8.8" 116 | }, 117 | "toc": { 118 | "base_numbering": 1, 119 | "nav_menu": {}, 120 | "number_sections": true, 121 | "sideBar": true, 122 | "skip_h1_title": false, 123 | "title_cell": "Table of Contents", 124 | "title_sidebar": "Contents", 125 | "toc_cell": false, 126 | "toc_position": {}, 127 | "toc_section_display": true, 128 | "toc_window_display": false 129 | }, 130 | "varInspector": { 131 | "cols": { 132 | "lenName": 16, 133 | "lenType": 16, 134 | "lenVar": 40 135 | }, 136 | "kernels_config": { 137 | "python": { 138 | "delete_cmd_postfix": "", 139 | "delete_cmd_prefix": "del ", 140 | "library": "var_list.py", 141 | "varRefreshCmd": "print(var_dic_list())" 142 | }, 143 | "r": { 144 | "delete_cmd_postfix": ") ", 145 | "delete_cmd_prefix": "rm(", 146 | "library": "var_list.r", 147 | "varRefreshCmd": "cat(var_dic_list()) " 148 | } 149 | }, 150 | "types_to_exclude": [ 151 | "module", 152 | "function", 153 | "builtin_function_or_method", 154 | "instance", 155 | "_Feature" 156 | ], 157 | "window_display": false 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 4 162 | } 163 | -------------------------------------------------------------------------------- /07_Python_Advanced_examples/003 Create a Countdown Timer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples/tree/main/07_Python_Advanced_examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Create a Countdown Timer\n", 17 | "\n", 18 | "In this example, you will learn to create a countdown timer.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python while Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/006_Python_while_Loop.ipynb)**\n", 23 | "* **[Python divmod()](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/017_Python_divmod%28%29.ipynb)**\n", 24 | "* **[Python time Module](https://github.com/milaan9/08_Python_Date_Time_Module/blob/main/007_Python_time_Module.ipynb)**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2021-07-02T07:10:36.277472Z", 33 | "start_time": "2021-07-02T07:10:31.178383Z" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "stop1\n" 42 | ] 43 | }, 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "'\\n>>Expected output:\\n \\n00:05\\n00:04\\n00:03\\n00:02\\n00:01\\nstop1\\n'" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "# Example 1: Countdown time in Python\n", 57 | "\n", 58 | "import time\n", 59 | "\n", 60 | "def countdown(time_sec):\n", 61 | " while time_sec:\n", 62 | " mins, secs = divmod(time_sec, 60)\n", 63 | " timeformat = '{:02d}:{:02d}'.format(mins, secs)\n", 64 | " print(timeformat, end='\\r')\n", 65 | " time.sleep(1)\n", 66 | " time_sec -= 1\n", 67 | "\n", 68 | " print(\"stop\")\n", 69 | "\n", 70 | "countdown(5)\n", 71 | "\n", 72 | "'''\n", 73 | ">>Expected output:\n", 74 | " \n", 75 | "00:05\n", 76 | "00:04\n", 77 | "00:03\n", 78 | "00:02\n", 79 | "00:01\n", 80 | "stop1\n", 81 | "'''" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "**Explanation:**\n", 89 | "\n", 90 | "The **`divmod()`** method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.\n", 91 | "\n", 92 | "**`end='\\r'`** overwrites the output for each iteration. The value of **`time_sec`** is decremented at the end of each iteration." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [] 101 | } 102 | ], 103 | "metadata": { 104 | "hide_input": false, 105 | "kernelspec": { 106 | "display_name": "Python 3", 107 | "language": "python", 108 | "name": "python3" 109 | }, 110 | "language_info": { 111 | "codemirror_mode": { 112 | "name": "ipython", 113 | "version": 3 114 | }, 115 | "file_extension": ".py", 116 | "mimetype": "text/x-python", 117 | "name": "python", 118 | "nbconvert_exporter": "python", 119 | "pygments_lexer": "ipython3", 120 | "version": "3.8.8" 121 | }, 122 | "toc": { 123 | "base_numbering": 1, 124 | "nav_menu": {}, 125 | "number_sections": true, 126 | "sideBar": true, 127 | "skip_h1_title": false, 128 | "title_cell": "Table of Contents", 129 | "title_sidebar": "Contents", 130 | "toc_cell": false, 131 | "toc_position": {}, 132 | "toc_section_display": true, 133 | "toc_window_display": false 134 | }, 135 | "varInspector": { 136 | "cols": { 137 | "lenName": 16, 138 | "lenType": 16, 139 | "lenVar": 40 140 | }, 141 | "kernels_config": { 142 | "python": { 143 | "delete_cmd_postfix": "", 144 | "delete_cmd_prefix": "del ", 145 | "library": "var_list.py", 146 | "varRefreshCmd": "print(var_dic_list())" 147 | }, 148 | "r": { 149 | "delete_cmd_postfix": ") ", 150 | "delete_cmd_prefix": "rm(", 151 | "library": "var_list.r", 152 | "varRefreshCmd": "cat(var_dic_list()) " 153 | } 154 | }, 155 | "types_to_exclude": [ 156 | "module", 157 | "function", 158 | "builtin_function_or_method", 159 | "instance", 160 | "_Feature" 161 | ], 162 | "window_display": false 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 4 167 | } 168 | -------------------------------------------------------------------------------- /07_Python_Advanced_examples/004 Convert Bytes to a String.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples/tree/main/07_Python_Advanced_examples)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Program to Convert Bytes to a String\n", 17 | "\n", 18 | "In this example, you will learn to convert bytes to a string.\n", 19 | "\n", 20 | "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", 21 | "\n", 22 | "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-07-02T07:12:28.136657Z", 31 | "start_time": "2021-07-02T07:12:28.104434Z" 32 | } 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "Easy ✅\n" 40 | ] 41 | }, 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "'\\n>>Expected output:\\n \\n00:05\\n00:04\\n00:03\\n00:02\\n00:01\\nstop1\\n'" 46 | ] 47 | }, 48 | "execution_count": 1, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "# Example 1: Using decode()\n", 55 | "\n", 56 | "print(b'Easy \\xE2\\x9C\\x85'.decode(\"utf-8\"))\n", 57 | "\n", 58 | "\n", 59 | "'''\n", 60 | ">>Expected output:\n", 61 | " \n", 62 | "Easy ✅\n", 63 | "'''" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Using **`decode()`**, you can convert bytes into string. Here, we have used utf-8 for decoding.\n", 71 | "\n", 72 | "You can learn about encoding on **[Python String encode()](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String_Methods/007_Python_String_encode%28%29.ipynb)**." 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [] 81 | } 82 | ], 83 | "metadata": { 84 | "hide_input": false, 85 | "kernelspec": { 86 | "display_name": "Python 3", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.8.8" 101 | }, 102 | "toc": { 103 | "base_numbering": 1, 104 | "nav_menu": {}, 105 | "number_sections": true, 106 | "sideBar": true, 107 | "skip_h1_title": false, 108 | "title_cell": "Table of Contents", 109 | "title_sidebar": "Contents", 110 | "toc_cell": false, 111 | "toc_position": {}, 112 | "toc_section_display": true, 113 | "toc_window_display": false 114 | }, 115 | "varInspector": { 116 | "cols": { 117 | "lenName": 16, 118 | "lenType": 16, 119 | "lenVar": 40 120 | }, 121 | "kernels_config": { 122 | "python": { 123 | "delete_cmd_postfix": "", 124 | "delete_cmd_prefix": "del ", 125 | "library": "var_list.py", 126 | "varRefreshCmd": "print(var_dic_list())" 127 | }, 128 | "r": { 129 | "delete_cmd_postfix": ") ", 130 | "delete_cmd_prefix": "rm(", 131 | "library": "var_list.r", 132 | "varRefreshCmd": "cat(var_dic_list()) " 133 | } 134 | }, 135 | "types_to_exclude": [ 136 | "module", 137 | "function", 138 | "builtin_function_or_method", 139 | "instance", 140 | "_Feature" 141 | ], 142 | "window_display": false 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 4 147 | } 148 | -------------------------------------------------------------------------------- /07_Python_Advanced_examples/README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 |

5 | 6 | 7 | 8 | # 07 Python Advanced Examples 9 | 10 | In this class, you'll find examples related to python advanced topics with a detailed explanations. 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 milaan9 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /img/dnld_rep.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/90_Python_Examples/5b2e3d65596cda09e62208e93bd7b48515b65940/img/dnld_rep.png --------------------------------------------------------------------------------