{ "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": [ "## 8.2.1 Presentation Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### It's important to format output to make it easier to read and understand. You can use Python's **f-strings** for formatting data. The result from formatting is another string.\n", "#### If there is a **format specifier**, it is separated from the value by a colon. The specifier can have an optional field width. For floating-point and decimal values only, the optional width can be followed a decimal point followed by the desired number of decimal places. The last part of the specifier is an optional **presentation type** such as `f` for float, `d` for integer, `c` for character, and `s` for string." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f'{17.489:.2f}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f'{10:d}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Characters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f'{65:c} {97:c}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Presentation type `s` is the default. If there are no presentation types, Python converts the value to a string." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f'{\"hello\":s} {7}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Floating-Point and Decimal Values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from decimal import Decimal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f'{Decimal(\"10000000000000000000000000.0\"):.3f}'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f'{Decimal(\"10000000000000000000000000.0\"):.3e}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Labeled output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use `=` to label the value with the text of the value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pi = 3.1415926\n", "\n", "print(f'{pi = }')\n", "print(f'{pi = :.2f}')\n", "print(f'{2*pi + 3 = :.2f}')" ] }, { "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 }