|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "f189c3fd", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Python: Everything\n", |
| 9 | + "- 1) Lists: Introduction\n", |
| 10 | + "<br>----------------------------------------------\n", |
| 11 | + "<br> By Hamed Shah-Hosseini\n", |
| 12 | + "<br> https://github.com/ostad-ai/Python-Everything\n", |
| 13 | + "<br> https://www.pinterest.com/HamedShahHosseini/programming-languages/python" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "id": "78beeaf8", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "To define a list:" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 20, |
| 27 | + "id": "e2fa4e97", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [ |
| 30 | + { |
| 31 | + "name": "stdout", |
| 32 | + "output_type": "stream", |
| 33 | + "text": [ |
| 34 | + "[1, 2, 3, 'hello', 'world']\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "mylist=[1,2,3,'hello','world']\n", |
| 40 | + "print(mylist)" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "id": "71985d4c", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "To change items of a list:" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 19, |
| 54 | + "id": "0ddc46e1", |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "[27, 2, 3, 'hello', 'world']\n" |
| 62 | + ] |
| 63 | + } |
| 64 | + ], |
| 65 | + "source": [ |
| 66 | + "mylist=[1,2,3,'hello','world']\n", |
| 67 | + "mylist[0]=27\n", |
| 68 | + "print(mylist)" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "id": "574d3a0b", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "To iterate over elements of a list:" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 30, |
| 82 | + "id": "b02995f7", |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [ |
| 85 | + { |
| 86 | + "name": "stdout", |
| 87 | + "output_type": "stream", |
| 88 | + "text": [ |
| 89 | + "1, 2, 3, hello, world, " |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "mylist=[1,2,3,'hello','world']\n", |
| 95 | + "for item in mylist:\n", |
| 96 | + " print(item,end=', ')" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "id": "b2062ed3", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "To iterate over elements of a list using indexes:" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 29, |
| 110 | + "id": "b808a005", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [ |
| 113 | + { |
| 114 | + "name": "stdout", |
| 115 | + "output_type": "stream", |
| 116 | + "text": [ |
| 117 | + "1, 2, 3, hello, world, " |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "mylist=[1,2,3,'hello','world']\n", |
| 123 | + "for i in range(len(mylist)):\n", |
| 124 | + " print(mylist[i],end=', ') " |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "id": "07db6c2b", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "Example: Let's separate numbers from non-numbers" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": 48, |
| 138 | + "id": "1a74d1f2", |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [ |
| 141 | + { |
| 142 | + "name": "stdout", |
| 143 | + "output_type": "stream", |
| 144 | + "text": [ |
| 145 | + "Numbers: [1, 21, 3.7, (4+5.6j)]\n", |
| 146 | + "Not-numbers: ['hello', 'world']\n" |
| 147 | + ] |
| 148 | + } |
| 149 | + ], |
| 150 | + "source": [ |
| 151 | + "mylist=[1,21,3.7, 4+5.6j,'hello','world']\n", |
| 152 | + "numbers=[]\n", |
| 153 | + "not_numbers=[]\n", |
| 154 | + "for item in mylist:\n", |
| 155 | + " tp=type(item)\n", |
| 156 | + " if tp==int or tp==float or tp==complex:\n", |
| 157 | + " numbers.append(item)\n", |
| 158 | + " else:\n", |
| 159 | + " not_numbers.append(item)\n", |
| 160 | + "print('Numbers:',numbers)\n", |
| 161 | + "print('Not-numbers:',not_numbers)" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "id": "9dd1d861", |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [], |
| 170 | + "source": [] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": "Python 3 (ipykernel)", |
| 176 | + "language": "python", |
| 177 | + "name": "python3" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "codemirror_mode": { |
| 181 | + "name": "ipython", |
| 182 | + "version": 3 |
| 183 | + }, |
| 184 | + "file_extension": ".py", |
| 185 | + "mimetype": "text/x-python", |
| 186 | + "name": "python", |
| 187 | + "nbconvert_exporter": "python", |
| 188 | + "pygments_lexer": "ipython3", |
| 189 | + "version": "3.8.10" |
| 190 | + } |
| 191 | + }, |
| 192 | + "nbformat": 4, |
| 193 | + "nbformat_minor": 5 |
| 194 | +} |
0 commit comments