{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Expressions\n", "\n", "Programming languages are much simpler than human languages. Nonetheless, there are some rules of grammar to learn in any language, and that is where we will begin. In this text, we will use the [Python](https://www.python.org/) programming language. Learning the grammar rules is essential, and the same rules used in the most basic programs are also central to more sophisticated programs.\n", "\n", "Programs are made up of *expressions*, which describe to the computer how to combine pieces of data. For example, a multiplication expression consists of a `*` symbol between two numerical expressions. Expressions, such as `3 * 4`, are *evaluated* by the computer. The value (the result of *evaluation*) of the last expression in each cell, `12` in this case, is displayed below the cell." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The grammar rules of a programming language are rigid. In Python, the `*` symbol cannot appear twice in a row. The computer will not try to interpret an expression that differs from its prescribed expression structures. Instead, it will show a `SyntaxError` error. The *Syntax* of a language is its set of grammar rules, and a `SyntaxError` indicates that an expression structure doesn't match any of the rules of the language." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 3 * * 4\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "3 * * 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Small changes to an expression can change its meaning entirely. Below, the space between the `*`'s has been removed. Because `**` appears between two numerical expressions, the expression is a well-formed *exponentiation* expression (the first number raised to the power of the second: 3 times 3 times 3 times 3). The symbols `*` and `**` are called *operators*, and the values they combine are called *operands*." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "81" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 ** 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Common Operators.** Data science often involves combining numerical values, and the set of operators in a programming language are designed to so that expressions can be used to express any sort of arithmetic. In Python, the following operators are essential.\n", "\n", "| Expression Type | Operator | Example | Value |\n", "|-----------------|----------|------------|-----------|\n", "| Addition | `+` | `2 + 3` | `5` |\n", "| Subtraction | `-` | `2 - 3` | `-1` |\n", "| Multiplication | `*` | `2 * 3` | `6` |\n", "| Division | `/` | `7 / 3` | `2.66667` |\n", "| Remainder | `%` | `7 % 3` | `1` |\n", "| Exponentiation | `**` | `2 ** 0.5` | `1.41421` |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python expressions obey the same familiar rules of *precedence* as in algebra: multiplication and division occur before addition and subtraction. Parentheses can be used to group together smaller expressions within a larger expression." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17.555555555555557" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 * 3 * 4 * 5 / 6 ** 3 + 7 + 8 - 9 + 10" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2017.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 * (3 * 4 * 5 / 6) ** 3 + 7 + 8 - 9 + 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This chapter introduces many types of expressions. Learning to program involves trying out everything you learn in combination, investigating the behavior of the computer. What happens if you divide by zero? What happens if you divide twice in a row? You don't always need to ask an expert (or the Internet); many of these details can be discovered by trying them out yourself. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }