[{"id":"/sicpy/1.1.1","tag":"TITLE","body":"1.1.1  Expressions"},{"tag":"TEXT","id":"#p1","child":[{"body":"\n    One easy way to get started at programming is to examine some typical\n    interactions with an interpreter for the\n    Python language.\n    You type \n    a ","tag":"#text"},{"tag":"EM","child":[{"body":"statement","tag":"#text"}]},{"body":",\n    and pass it to the interpreter, which then\n    ","tag":"#text"},{"tag":"EM","child":[{"body":"evaluates","tag":"#text"}]},{"body":" that\n    \n\tstatement.\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p2","child":[{"body":"\n\t  In this edition, the mouse click on \n\t  Python statements with dark background is \n\t  programmed in such a way that a Python interpreter is\n\t  displayed, which can ","tag":"#text"},{"tag":"EM","child":[{"body":"evaluate","tag":"#text"}]},{"body":" the\n\t  statement and display the values it prints.\n\t","tag":"#text"}]},{"tag":"TEXT","id":"#p3","child":[{"body":"\n        One kind of statement you might type is an \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"expression","tag":"#text"}]},{"body":".\n        \n\tOne kind of primitive expression is a number.\n      \n    (More precisely, the expression that you type consists of the numerals that\n    represent the number in base 10.)\n    \n      If you ask our script to evaluate the expression statement\n      ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":1,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgLADgGxA","body":"486 "},{"body":"\n      by clicking it, it will respond by displaying a Python interpreter\n      with the option\n      to evaluate the statement by pressing a \"","tag":"#text"},{"body":"Run","tag":"#text"},{"body":"\" button.\n      Click on the primitive expression statement, and what happens is—nothing!\n      To see the result of evaluating ","tag":"#text"},{"tag":"PYTHONINLINE","body":"486"},{"body":",\n      we need to apply the function ","tag":"#text"},{"tag":"PYTHONINLINE","body":"print"},{"body":"\n      to the expression, using the usual mathematical notation of function application\n      ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":2,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBQAsAHAGwCUQA","body":"print(486) "},{"body":"\n      resulting in the display of","tag":"#text"},{"tag":"FOOTNOTE_REF","id":"#footnote-link-1","body":"1","href":"/sicpy/1.1.1#footnote-1"},{"tag":"SNIPPET","latex":false,"id":3,"eval":false,"output":"486"},{"body":"\n      in a separate part in the browser window.\n    ","tag":"#text"}]},{"tag":"TEXT","id":"#p4","child":[{"body":"\n        Expressions representing numbers may be combined with\n\toperators\n        (such \n        \n        as ","tag":"#text"},{"tag":"PYTHONINLINE","body":"+"},{"body":"\n        or ","tag":"#text"},{"tag":"PYTHONINLINE","body":"*"},{"body":") to form a\n        \n        compound expression that represents the\n        application of a corresponding primitive\n        function to those numbers.  For example,\n\t\n\t  evaluate\n          any of the following expression statements\n\t  by clicking on it:\n\t","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":4,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBSIDMA7CANQjkAsAnAJRA","body":"print(137 + 349) ","output":"486"},{"tag":"SNIPPET","latex":false,"id":5,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBSIAMlIAtCAMx0AsAlEA","body":"print(1000 - 334) ","output":"666"},{"tag":"SNIPPET","latex":false,"id":6,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBQCsIAVCAJw0CUQA","body":"print(5 * 99) ","output":"495"},{"tag":"SNIPPET","latex":false,"id":7,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBSIAMIA9CACwCUQA","body":"print(10 / 4) ","output":"2.5"},{"tag":"SNIPPET","latex":false,"id":8,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBQBMyA7CANRIAMAlEA","body":"print(2.7 + 10) ","output":"12.7"}]},{"tag":"TEXT","id":"#p5","child":[{"body":"\n        Expressions such as ","tag":"#text"},{"tag":"PYTHONINLINE","body":"137 + 349"},{"body":", which contain other expressions\n        as components, are called ","tag":"#text"},{"tag":"EM","child":[{"body":"combinations","tag":"#text"}]},{"body":".\n        \n        Combinations that are formed by an \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"operator","tag":"#text"}]},{"body":" symbol in the middle, and\n        ","tag":"#text"},{"tag":"EM","child":[{"body":"operand","tag":"#text"}]},{"body":" expressions to the left and right of it, \n        are called \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"operator combinations","tag":"#text"}]},{"body":".\n\t\n        The value of an operator combination is\n        obtained by applying the function specified by the operator to the\n        arguments that are the values of the operands.\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p6","child":[{"body":"\n        The convention of placing the operator between the operands is\n        known as \n        ","tag":"#text"},{"tag":"EM","child":[{"body":"infix notation","tag":"#text"}]},{"body":". It follows the mathematical notation that\n\tyou are most likely familiar with from school and everyday life.\n        As in mathematics, operator combinations can be ","tag":"#text"},{"tag":"EM","child":[{"body":"nested","tag":"#text"}]},{"body":", that\n\tis, they can have operands that\n        \n\tthemselves are operator combinations:\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":9,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgDgJwJYB2ALgBQkDMIAVCAKwCUIA1CCYgAwgC0IAbPfSA","body":"print((3 * 5) + (10 - 6)) ","output":"19"},{"body":"\n        As usual,\n\t\n\tparentheses are used to group operator combinations in order\n\tto avoid ambiguities. Python also follows the usual conventions\n\twhen parentheses are omitted: multiplication and division bind more\n\tstrongly than addition and subtraction. For example,\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":10,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgzCAVCAVhAGokAGEAehACYg","body":"3 * 5 + 10 / 2 "},{"body":"\n        stands for \n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":11,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgFAZhAKhAFYBKEAahC0QAYQB6EAJhKA","body":"(3 * 5) + (10 / 2) "},{"body":"\n        We say that ","tag":"#text"},{"tag":"PYTHONINLINE","body":"*"},{"body":" and\n\t","tag":"#text"},{"tag":"PYTHONINLINE","body":"/"},{"body":" have\n        ","tag":"#text"},{"tag":"EM","child":[{"body":"higher precedence","tag":"#text"}]},{"body":"\n\tthan ","tag":"#text"},{"tag":"PYTHONINLINE","body":"+"},{"body":" and\n\t","tag":"#text"},{"tag":"PYTHONINLINE","body":"-"},{"body":". Sequences of additions and\n\tsubtractions are read from left to right, as are sequences of\n\tmultiplications and divisions. Thus,\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":12,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOxIC0ICsIA9CAEwgBUIALCANQgDMQA","body":"1 - 5 / 2 * 4 + 3 "},{"body":"\n        stands for \n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":13,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgFIkBaELLAVhAHoQAmAShACoQAWa2gahAGYg","body":"(1 - ((5 / 2) * 4)) + 3 "},{"body":"\n        We say that the operators\n        ","tag":"#text"},{"tag":"PYTHONINLINE","body":"+"},{"body":",\n        ","tag":"#text"},{"tag":"PYTHONINLINE","body":"-"},{"body":",\n        ","tag":"#text"},{"tag":"PYTHONINLINE","body":"*"},{"body":" and\n        ","tag":"#text"},{"tag":"PYTHONINLINE","body":"/"},{"body":" are \n\t","tag":"#text"},{"tag":"EM","child":[{"body":"left-associative","tag":"#text"}]},{"body":".\n      ","tag":"#text"}]},{"tag":"TEXT","id":"#p7","child":[{"body":"\n        There is no limit (in principle) to the depth of such nesting and to the\n\toverall complexity of the expressions that the Python interpreter\n\tcan evaluate. It is we humans who might get confused by still relatively\n        simple expressions such as\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":14,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgzCAVCACgCY8QAWEAakJ2oFYBKBqwgxABhAFoQB2Z6gDYGQA","body":"3 * (2 * 4 + (3 + 5)) + ((10 - 7) + 6) "},{"body":"\n        which the interpreter would readily evaluate to be 57. We can help\n        ourselves by writing such an expression in the form\n        ","tag":"#text"},{"tag":"SNIPPET","latex":false,"id":15,"eval":true,"prependLength":0,"program":"chap=1&prgrm=MQAgygkgwgCgniAjAOhYgUOgFAZhAKhCwCYCQAWEAaiLxoFYBKR9a1rLRABhAFoQA7I2ogAbMyA","body":"(3 * (2 * 4 + (3 + 5))\n +\n ((10 - 7) + 6)) "},{"body":"\n        to visually separate the major components of the expression.","tag":"#text"},{"tag":"FOOTNOTE_REF","id":"#footnote-link-2","body":"2","href":"/sicpy/1.1.1#footnote-2"}]},{"tag":"TEXT","id":"#p8","child":[{"body":"\n    Even with complex expressions, the interpreter always operates in the\n    same basic cycle: It reads\n    a statement typed by the user,\n    evaluates the\n    statement,\n    and prints the result of any applications of ","tag":"#text"},{"tag":"PYTHONINLINE","body":"print"},{"body":".\n    This mode of operation is often expressed by saying\n    that the interpreter runs in a\n    ","tag":"#text"},{"tag":"EM","child":[{"body":"read-evaluate-print loop","tag":"#text"}]},{"body":".\n      \n\tObserve, however, that it is necessary to explicitly instruct the\n\tinterpreter to print the value of the\n\texpression.\n      ","tag":"#text"}]},{"tag":"DISPLAYFOOTNOTE","id":"#footnote-1","count":1,"href":"/sicpy/1.1.1#footnote-link-1","child":[{"body":"Throughout this book,\n      we distinguish\n      \n      between the input typed by\n      the user and any text printed by the interpreter by showing the\n      latter in slanted characters.","tag":"#text"}]},{"tag":"DISPLAYFOOTNOTE","id":"#footnote-2","count":2,"href":"/sicpy/1.1.1#footnote-link-2","child":[{"body":"\n\tThe additional parentheses are necessary,\n\tif we want to spread an expression over multiple lines and there\n\tare no surrounding parentheses already.\n\t","tag":"#text"}]}]