2.3: Expressions and statements

Think Python 2ed. · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    2.3: Expressions and statements

    An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions:

    >>> 42
    42
    >>> n
    17
    >>> n + 25
    42
    

    When you type an expression at the prompt, the interpreter evaluates it, which means that it finds the value of the expression. In this example, n has the value 17 and n + 25 has the value 42.

    A statement is a unit of code that has an effect, like creating a variable or displaying a value.

    >>> n = 17
    >>> print(n)
    

    The first line is an assignment statement that gives a value to n. The second line is a print statement that displays the value of n.

    When you type a statement, the interpreter executes it, which means that it does whatever the statement says. In general, statements don’t have values.


    This page titled 2.3: Expressions and statements was last modified on Tue, 01 Dec 2020 03:30:50 GMT and is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform.