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

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5.15 Other Sequence Processing Functions " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Alphabetical vs. Numerical Ordering of Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Red' < 'orange'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This may not be what we expected, because alphabetically, `'Red'` comes **after** `'orange'`. It happened because upper-case 'R' comes before lower-case 'o'. Python is actually doing a numerical comparison using the **ordinal value** of the letters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ord('R')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ord('o')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### If we want Python to simulate comparing alphabetically, we must either shift all the letters to lower case or to upper case before doing a comparison using the string methods `lower()` or `upper()`, respectively." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colors = ['Red', 'orange', 'Yellow', 'green', 'Blue']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_red = 'Red'\n", "color_orange = 'orange'\n", "\n", "color_red < color_orange" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_red.lower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_orange.lower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_red.lower() < color_orange.lower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_red.upper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_orange.upper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_red.upper() < color_orange.upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterating Backwards Through a Sequence" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]\n", "reversed_numbers = [n**2 for n in reversed(numbers)]\n", "\n", "reversed_numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combining Iterables into Tuples of Corresponding Elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppose you need to iterate over multiple iterables in parallel. Built-in function `zip()` combines any number of equal-length iterables such as lists. It returns an iterable containing tuples, and each tuple contains the corresponding elements from the original tuples." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = ['Bob', 'Sue', 'Amanda']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grade_point_averages = [3.5, 4.0, 3.75] " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Name GPA')\n", "\n", "for name, gpa in zip(names, grade_point_averages):\n", " print(f'{name:6}{gpa:5.1f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `for` loop unpacks each tuple created by the call to `zip()` into variables `name` and `gpa`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE:** A student has three exam scores, 85, 92, and 99. The weights applied to the exams are 0.25, 0.25, and 0.50, respectively. Calculate the student's weighted total score." ] }, { "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 2023 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 }