{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0b1011100111101000010101101001001000101000000001100100000110110100\n" ] } ], "source": [ "# Example 1\n", "from random import randint\n", "random_bits = 0\n", "for i in range(64):\n", " if randint(0, 1):\n", " random_bits |= 1 << i\n", "print(bin(random_bits))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vanilla is delicious\n", "chocolate is delicious\n", "pecan is delicious\n", "strawberry is delicious\n" ] } ], "source": [ "# Example 2\n", "flavor_list = ['vanilla', 'chocolate', 'pecan', 'strawberry']\n", "for flavor in flavor_list:\n", " print('%s is delicious' % flavor)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### When you need index, you can do this. UGLY!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: vanilla\n", "2: chocolate\n", "3: pecan\n", "4: strawberry\n" ] } ], "source": [ "# Example 3\n", "for i in range(len(flavor_list)):\n", " flavor = flavor_list[i]\n", " print('%d: %s' % (i + 1, flavor))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### enumerate is much better. GOOD!" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: vanilla\n", "2: chocolate\n", "3: pecan\n", "4: strawberry\n" ] } ], "source": [ "# Example 4\n", "for i, flavor in enumerate(flavor_list):\n", " print('%d: %s' % (i + 1, flavor))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set the begin\tcounting\t(1\tin\tthis\tcase)! WONDERFUL!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: vanilla\n", "2: chocolate\n", "3: pecan\n", "4: strawberry\n" ] } ], "source": [ "# Example 5\n", "for i, flavor in enumerate(flavor_list, 1):\n", " print('%d: %s' % (i, flavor))" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }