{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.8 `for` Statement \n", "\n", "#### The `for` statement is the other type of looping statement. Use it whenever you want to repeatedly execute a suite, once for each item in a sequence of items.\n", "\n", "#### For example, a string is a sequence of characters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for character in 'Programming':\n", " print(character, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Function `print`’s `sep` Keyword Argument \n", "\n", "#### The default separation character is a single blank. We can change that with the `sep` keyword argument." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(10, 20, 30)\n", "print(10, 20, 30, sep=', ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.8.1 Iterables, Lists, and Iterators\n", "\n", "#### In a `for` statement, what's to the right of the keyword `in` must be an **iterable** -- an object that contains a sequence of items that the `for` statement can take in order one at a time. The string `'Programming'` is an iterable since it consists of a sequence of characters, and the above `for` statement processed each character in order one at a time. We say that the `for` loop **iterates** over the string.\n", "\n", "#### In the following, `[2, -3, 0, 17, 9]` is a Python **list** object. It consists of a sequence of values separated by values and enclosed in square brackets. A list is an iterable, and the `for` loop iterates over the list to compute its sum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total = 0\n", "\n", "for number in [2, -3, 0, 17, 9]:\n", " total = total + number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Behind each sequence (such as a string or a list) is a hidden **iterator**.\n", "- The iterator knows how to get each item of the sequence in order one at a time, from the first to the last.\n", "- The iterator keeps track of which item it last got so that it can get the next item when it's asked to do so. \n", "\n", "#### A `for` loop repeately asks the iterator for the items until there no more items. The `for` loop executes its suite after getting each item." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.8.2 Built-In `range` Function and Generators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can use Python's `range()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for counter in range(10):\n", " print(counter, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note that by default, the range starts with 0 and goes by 1 up to, but **not** including, the argument value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE:** Use the range function and a `for` loop to calculate and print the sum of the integers 1 through 1 million." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum = 0\n", "\n", "for n in range(1_000_001):\n", " sum = sum + n\n", " \n", "print(sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unnamed control variable\n", "#### Sometimes you just want to count how many times to loop. You can use `_` as a dummy unnamed control variable." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n", "Hello, world!\n", "Hello, world!\n" ] } ], "source": [ "for _ in range(0, 3):\n", " print('Hello, world!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2024 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }