|
15 | 15 | "metadata": {}, |
16 | 16 | "source": [ |
17 | 17 | "Few built-in Modules are:\n", |
18 | | - "1. math\n", |
19 | | - "2. cmath\n", |
20 | | - "3. sys\n", |
| 18 | + "1. \\_\\_builtins\\_\\_\n", |
| 19 | + "0. \\_\\_file\\_\\_\n", |
| 20 | + "2. math\n", |
| 21 | + "3. cmath\n", |
| 22 | + "2. sys\n", |
21 | 23 | "4. os\n", |
22 | 24 | "5. itertools\n", |
23 | 25 | "6. string" |
|
48 | 50 | "<br>\n", |
49 | 51 | "prime = 2 <br>\n", |
50 | 52 | "prime_square = 4 <br>\n", |
| 53 | + "\\_\\_primeval\\_\\_ = 3<br>\n", |
51 | 54 | "<br>\n", |
52 | 55 | "if \\_\\_name\\_\\_ == '\\_\\_main\\_\\_': <br>\n", |
53 | 56 | " t = time()<br>\n", |
54 | 57 | " for i in prime_gen(10 ** 2):<br>\n", |
55 | 58 | " print(i)<br>\n", |
56 | | - " print(time()-t)" |
| 59 | + " print(time()-t)\n", |
| 60 | + "\n", |
| 61 | + "Contents:\n", |
| 62 | + "1. prime_gen - function\n", |
| 63 | + "2. prime - varaiable (constant when imported)\n", |
| 64 | + "3. prime_square - variable (constant when imported)\n", |
| 65 | + "4. \\_\\_primeval\\_\\_ - private value" |
57 | 66 | ] |
58 | 67 | }, |
59 | 68 | { |
|
120 | 129 | "cell_type": "markdown", |
121 | 130 | "metadata": {}, |
122 | 131 | "source": [ |
123 | | - "## Importing all the functions from the module:\n", |
| 132 | + "## Importing all the objects from the module:\n", |
124 | 133 | "To import all the functions or objects, use * .\n", |
125 | 134 | "\n", |
126 | 135 | "Syntax: `from module_name import *`" |
127 | 136 | ] |
128 | 137 | }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": 1, |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "name": "stdout", |
| 145 | + "output_type": "stream", |
| 146 | + "text": [ |
| 147 | + "2 3 5 7 11 13 17 19 " |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "from prime_gen import *\n", |
| 153 | + "\n", |
| 154 | + "for i in prime_gen.prime_gen(20):\n", |
| 155 | + " print(i, end=' ')\n" |
| 156 | + ] |
| 157 | + }, |
129 | 158 | { |
130 | 159 | "cell_type": "markdown", |
131 | 160 | "metadata": {}, |
132 | 161 | "source": [ |
133 | | - "## as Keyword:\n", |
| 162 | + "## **as** Keyword:\n", |
134 | 163 | "*as* Keyword is used to have a custom name for imported module or function.\n", |
135 | 164 | "\n", |
136 | 165 | "Syntax: \n", |
137 | 166 | "`import moule_name as custom_name` \n", |
138 | 167 | "`from module_name import function_name as custom_name`" |
139 | 168 | ] |
140 | 169 | }, |
| 170 | + { |
| 171 | + "cell_type": "code", |
| 172 | + "execution_count": 3, |
| 173 | + "metadata": {}, |
| 174 | + "outputs": [ |
| 175 | + { |
| 176 | + "name": "stdout", |
| 177 | + "output_type": "stream", |
| 178 | + "text": [ |
| 179 | + "2 3 5 7 11 13 17 19 " |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "import prime_gen as pg\n", |
| 185 | + "\n", |
| 186 | + "for i in pg.prime_gen(20):\n", |
| 187 | + " print(i, end=' ')" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": 4, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [ |
| 195 | + { |
| 196 | + "name": "stdout", |
| 197 | + "output_type": "stream", |
| 198 | + "text": [ |
| 199 | + "2 3 5 7 11 13 17 19 " |
| 200 | + ] |
| 201 | + } |
| 202 | + ], |
| 203 | + "source": [ |
| 204 | + "from prime_gen import prime_gen as pgf\n", |
| 205 | + "\n", |
| 206 | + "for i in pgf(20):\n", |
| 207 | + " print(i, end=' ')" |
| 208 | + ] |
| 209 | + }, |
141 | 210 | { |
142 | 211 | "cell_type": "markdown", |
143 | 212 | "metadata": {}, |
144 | 213 | "source": [ |
145 | 214 | "## \\_\\_name\\_\\_ method:\n", |
146 | | - "It returns the name of the module.\n", |
| 215 | + "It returns the name of the module without the extension. If called for main executing program (main module), it returns '\\_\\_main\\_\\_'\n", |
147 | 216 | "\n", |
148 | 217 | "Syntax: `module_name.__name__`" |
149 | 218 | ] |
150 | 219 | }, |
151 | 220 | { |
152 | 221 | "cell_type": "code", |
153 | | - "execution_count": null, |
| 222 | + "execution_count": 5, |
| 223 | + "metadata": {}, |
| 224 | + "outputs": [ |
| 225 | + { |
| 226 | + "name": "stdout", |
| 227 | + "output_type": "stream", |
| 228 | + "text": [ |
| 229 | + "prime_gen\n", |
| 230 | + "__main__\n" |
| 231 | + ] |
| 232 | + } |
| 233 | + ], |
| 234 | + "source": [ |
| 235 | + "import prime_gen\n", |
| 236 | + "\n", |
| 237 | + "print(prime_gen.__name__)\n", |
| 238 | + "print(__name__)" |
| 239 | + ] |
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "markdown", |
| 243 | + "metadata": {}, |
| 244 | + "source": [ |
| 245 | + "## dir method:\n" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "code", |
| 250 | + "execution_count": 7, |
| 251 | + "metadata": {}, |
| 252 | + "outputs": [ |
| 253 | + { |
| 254 | + "name": "stdout", |
| 255 | + "output_type": "stream", |
| 256 | + "text": [ |
| 257 | + "['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'accumulate', 'acos', 'acosh', 'altzone', 'asctime', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'chain', 'comb', 'combinations', 'combinations_with_replacement', 'compress', 'copysign', 'cos', 'cosh', 'count', 'ctime', 'cycle', 'daylight', 'degrees', 'dist', 'dropwhile', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'filterfalse', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'get_clock_info', 'gmtime', 'groupby', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'islice', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'localtime', 'log', 'log10', 'log1p', 'log2', 'mktime', 'modf', 'monotonic', 'monotonic_ns', 'nan', 'perf_counter', 'perf_counter_ns', 'perm', 'permutations', 'pi', 'pow', 'prime', 'prime_gen', 'prime_square', 'process_time', 'process_time_ns', 'prod', 'product', 'radians', 'remainder', 'repeat', 'sin', 'sinh', 'sleep', 'sqrt', 'starmap', 'strftime', 'strptime', 'struct_time', 'takewhile', 'tan', 'tanh', 'tau', 'tee', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'trunc', 'tzname', 'zip_longest']\n", |
| 258 | + "\n", |
| 259 | + "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_i5', '_i6', '_i7', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'i', 'pg', 'pgf', 'prime_gen', 'quit']\n" |
| 260 | + ] |
| 261 | + } |
| 262 | + ], |
| 263 | + "source": [ |
| 264 | + "import prime_gen\n", |
| 265 | + "\n", |
| 266 | + "print(dir(prime_gen))\n", |
| 267 | + "print()\n", |
| 268 | + "print(dir())" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "markdown", |
154 | 273 | "metadata": {}, |
155 | | - "outputs": [], |
156 | | - "source": [] |
| 274 | + "source": [ |
| 275 | + "## namespace:\n" |
| 276 | + ] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "markdown", |
| 280 | + "metadata": {}, |
| 281 | + "source": [ |
| 282 | + "## Global, Local and Built-in Namespaces" |
| 283 | + ] |
| 284 | + }, |
| 285 | + { |
| 286 | + "cell_type": "markdown", |
| 287 | + "metadata": {}, |
| 288 | + "source": [ |
| 289 | + "## Builtins:\n", |
| 290 | + "\n", |
| 291 | + "\\_\\_builins\\_\\_ module contains the list of methods and variables that are standard definitions in Python." |
| 292 | + ] |
| 293 | + }, |
| 294 | + { |
| 295 | + "cell_type": "code", |
| 296 | + "execution_count": 8, |
| 297 | + "metadata": {}, |
| 298 | + "outputs": [ |
| 299 | + { |
| 300 | + "name": "stdout", |
| 301 | + "output_type": "stream", |
| 302 | + "text": [ |
| 303 | + "ArithmeticError\n", |
| 304 | + "AssertionError\n", |
| 305 | + "AttributeError\n", |
| 306 | + "BaseException\n", |
| 307 | + "BlockingIOError\n", |
| 308 | + "BrokenPipeError\n", |
| 309 | + "BufferError\n", |
| 310 | + "BytesWarning\n", |
| 311 | + "ChildProcessError\n", |
| 312 | + "ConnectionAbortedError\n", |
| 313 | + "ConnectionError\n", |
| 314 | + "ConnectionRefusedError\n", |
| 315 | + "ConnectionResetError\n", |
| 316 | + "DeprecationWarning\n", |
| 317 | + "EOFError\n", |
| 318 | + "Ellipsis\n", |
| 319 | + "EnvironmentError\n", |
| 320 | + "Exception\n", |
| 321 | + "False\n", |
| 322 | + "FileExistsError\n", |
| 323 | + "FileNotFoundError\n", |
| 324 | + "FloatingPointError\n", |
| 325 | + "FutureWarning\n", |
| 326 | + "GeneratorExit\n", |
| 327 | + "IOError\n", |
| 328 | + "ImportError\n", |
| 329 | + "ImportWarning\n", |
| 330 | + "IndentationError\n", |
| 331 | + "IndexError\n", |
| 332 | + "InterruptedError\n", |
| 333 | + "IsADirectoryError\n", |
| 334 | + "KeyError\n", |
| 335 | + "KeyboardInterrupt\n", |
| 336 | + "LookupError\n", |
| 337 | + "MemoryError\n", |
| 338 | + "ModuleNotFoundError\n", |
| 339 | + "NameError\n", |
| 340 | + "None\n", |
| 341 | + "NotADirectoryError\n", |
| 342 | + "NotImplemented\n", |
| 343 | + "NotImplementedError\n", |
| 344 | + "OSError\n", |
| 345 | + "OverflowError\n", |
| 346 | + "PendingDeprecationWarning\n", |
| 347 | + "PermissionError\n", |
| 348 | + "ProcessLookupError\n", |
| 349 | + "RecursionError\n", |
| 350 | + "ReferenceError\n", |
| 351 | + "ResourceWarning\n", |
| 352 | + "RuntimeError\n", |
| 353 | + "RuntimeWarning\n", |
| 354 | + "StopAsyncIteration\n", |
| 355 | + "StopIteration\n", |
| 356 | + "SyntaxError\n", |
| 357 | + "SyntaxWarning\n", |
| 358 | + "SystemError\n", |
| 359 | + "SystemExit\n", |
| 360 | + "TabError\n", |
| 361 | + "TimeoutError\n", |
| 362 | + "True\n", |
| 363 | + "TypeError\n", |
| 364 | + "UnboundLocalError\n", |
| 365 | + "UnicodeDecodeError\n", |
| 366 | + "UnicodeEncodeError\n", |
| 367 | + "UnicodeError\n", |
| 368 | + "UnicodeTranslateError\n", |
| 369 | + "UnicodeWarning\n", |
| 370 | + "UserWarning\n", |
| 371 | + "ValueError\n", |
| 372 | + "Warning\n", |
| 373 | + "WindowsError\n", |
| 374 | + "ZeroDivisionError\n", |
| 375 | + "__IPYTHON__\n", |
| 376 | + "__build_class__\n", |
| 377 | + "__debug__\n", |
| 378 | + "__doc__\n", |
| 379 | + "__import__\n", |
| 380 | + "__loader__\n", |
| 381 | + "__name__\n", |
| 382 | + "__package__\n", |
| 383 | + "__spec__\n", |
| 384 | + "abs\n", |
| 385 | + "all\n", |
| 386 | + "any\n", |
| 387 | + "ascii\n", |
| 388 | + "bin\n", |
| 389 | + "bool\n", |
| 390 | + "breakpoint\n", |
| 391 | + "bytearray\n", |
| 392 | + "bytes\n", |
| 393 | + "callable\n", |
| 394 | + "chr\n", |
| 395 | + "classmethod\n", |
| 396 | + "compile\n", |
| 397 | + "complex\n", |
| 398 | + "copyright\n", |
| 399 | + "credits\n", |
| 400 | + "delattr\n", |
| 401 | + "dict\n", |
| 402 | + "dir\n", |
| 403 | + "display\n", |
| 404 | + "divmod\n", |
| 405 | + "enumerate\n", |
| 406 | + "eval\n", |
| 407 | + "exec\n", |
| 408 | + "filter\n", |
| 409 | + "float\n", |
| 410 | + "format\n", |
| 411 | + "frozenset\n", |
| 412 | + "get_ipython\n", |
| 413 | + "getattr\n", |
| 414 | + "globals\n", |
| 415 | + "hasattr\n", |
| 416 | + "hash\n", |
| 417 | + "help\n", |
| 418 | + "hex\n", |
| 419 | + "id\n", |
| 420 | + "input\n", |
| 421 | + "int\n", |
| 422 | + "isinstance\n", |
| 423 | + "issubclass\n", |
| 424 | + "iter\n", |
| 425 | + "len\n", |
| 426 | + "license\n", |
| 427 | + "list\n", |
| 428 | + "locals\n", |
| 429 | + "map\n", |
| 430 | + "max\n", |
| 431 | + "memoryview\n", |
| 432 | + "min\n", |
| 433 | + "next\n", |
| 434 | + "object\n", |
| 435 | + "oct\n", |
| 436 | + "open\n", |
| 437 | + "ord\n", |
| 438 | + "pow\n", |
| 439 | + "print\n", |
| 440 | + "property\n", |
| 441 | + "range\n", |
| 442 | + "repr\n", |
| 443 | + "reversed\n", |
| 444 | + "round\n", |
| 445 | + "set\n", |
| 446 | + "setattr\n", |
| 447 | + "slice\n", |
| 448 | + "sorted\n", |
| 449 | + "staticmethod\n", |
| 450 | + "str\n", |
| 451 | + "sum\n", |
| 452 | + "super\n", |
| 453 | + "tuple\n", |
| 454 | + "type\n", |
| 455 | + "vars\n", |
| 456 | + "zip\n" |
| 457 | + ] |
| 458 | + } |
| 459 | + ], |
| 460 | + "source": [ |
| 461 | + "builtin = dir(__builtins__)\n", |
| 462 | + "for i in builtin:\n", |
| 463 | + " print(i)" |
| 464 | + ] |
| 465 | + }, |
| 466 | + { |
| 467 | + "cell_type": "markdown", |
| 468 | + "metadata": {}, |
| 469 | + "source": [ |
| 470 | + "## Private in modules:" |
| 471 | + ] |
157 | 472 | } |
158 | 473 | ], |
159 | 474 | "metadata": { |
|
0 commit comments