2.2: Variables

Python for Everybody · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

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

    2.2: Variables

    One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.

    An assignment statement creates new variables and gives them values:

    >>> message = 'And now for something completely different'
    >>> n = 17
    >>> pi = 3.1415926535897931

    This example makes three assignments. The first assigns a string to a new variable named message; the second assigns the integer 17 to n; the third assigns the (approximate) value of π to pi.

    To display the value of a variable, you can use a print statement:

    >>> print(n)
    17
    >>> print(pi)
    3.141592653589793

    The type of a variable is the type of the value it refers to.

    >>> type(message)
    <class 'str'>
    >>> type(n)
    <class 'int'>
    >>> type(pi)
    <class 'float'>

    This page titled 2.2: Variables was last modified on Tue, 01 Dec 2020 03:50:48 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform.