From f3c27d07c41b14926e85e2bc374b644762fa3082 Mon Sep 17 00:00:00 2001 From: mehul <40045788+wiredtoserve@users.noreply.github.com> Date: Mon, 2 Jul 2018 16:21:45 +1000 Subject: [PATCH] Adding generator file --- Python/07.Generators.ipynb | 201 +++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 Python/07.Generators.ipynb diff --git a/Python/07.Generators.ipynb b/Python/07.Generators.ipynb new file mode 100644 index 0000000..66fc586 --- /dev/null +++ b/Python/07.Generators.ipynb @@ -0,0 +1,201 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def create_cubes(n):\n", + " result = []\n", + " for x in range(n):\n", + " result.append(x**3)\n", + " return result " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_cubes(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def create_roots(n):\n", + " for x in range(n):\n", + " yield(x**(1/2))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_roots(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.0\n", + "1.0\n", + "1.4142135623730951\n", + "1.7320508075688772\n", + "2.0\n", + "2.23606797749979\n", + "2.449489742783178\n", + "2.6457513110645907\n", + "2.8284271247461903\n", + "3.0\n" + ] + } + ], + "source": [ + "for x in create_roots(10):\n", + " print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(create_roots(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "s ='hello'" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "s_iter = iter(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(s_iter)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'e'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "next(s_iter)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}