Skip to main content
Engineering LibreTexts

5.2: Procedure – Output Window

  • Page ID
    26443
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Open IDLE by selecting Python from the Start menu and then choosing the option to open IDLE (Python GUI). Do not open the command line. Alternately, open the IDLE (Python GUI) icon on the desktop. A simple text window known as the Python shell will open. It should have a white background with a text message at the top and immediately below that, a cursor prompt

    >>> 
    

    The shell serves two functions. First, it can serve as a sort of scratch pad to try snippets of code (shown in the steps below). Second, it can serve as the text output window for larger programs. Do not try to use this window for the creation of complete programs that you wish to save. We shall use this window to create a few variables and perform some basic manipulations on them. Type the following and then hit the Enter key:

    a=5
    

    The >>> should reappear. This command defines a variable called a and copies the integer value 5 into it. In similar manner, type in the following commands:

    b=13
    x=5.0
    y=13.0
    m=”Mary”
    n=”Nancy”
    

    It is very important that the “.0” portions be included. This is how integers and floats are distinguished: floats always have a decimal point, integers don’t. Also, it is possible to define the strings using the apostrophe versus the quote . This can be handy if you need to have a string that includes a quote or apostrophe within it; merely define the string with the other character. In any case, the computer’s memory now looks something like this:

    name

    value

    a

    5

    b

    13

    x

    5.0

    y

    13.0

    m

    Mary

    n

    Nancy

    Table \(\PageIndex{1}\)

    The trick now, of course, is to access these values, manipulate them, and see the results. An important command for this process is the print command. print will print what follows it, either variables or expressions, on to the output window. Note that like all built-in commands and functions in Python, this command is all lower case. Capitalizing it will generate an error. Also, note that commands will be color coded orange-red.

    At the prompt, type the following:

    print( a )
    

    The output should be the integer 5

    Now type:

    print( a, x, m, n )
    

    In this case, the following sequence should result:

    5 5.0 Mary Nancy
    

    Continue with the following expression:

    print( a + b )
    

    This results in the value 18. This line retrieves the values of a and b from memory, adds them together, and prints the result on the output window. Neither a nor b are altered in the process. Alternately, we could have created a brand new variable and printed it. The result will be the same. Enter the following two lines to verify this:

    c = a + b
    print( c )
    

    The only difference is that this version adds a new “slot” called c to the memory map above. It is worth noting that once a variable is created, its value may be recomputed over and over if desired. For example, type the following:

    c = 20 + a
    print( c )
    

    The result should be 25. The first line computes a new value which then overwrites the prior value of 18.

    Besides addition, the other main math operators are , * (multiplication), / (division), ** (exponents, which can also be performed using the function pow(x,y) for xy), % (modulo) and // (floor divide). Parentheses () may be used to force the execution of some operations before others. Parentheses have the highest precedence and are followed by multiplication, division, addition and subtraction. That is, the expression a=b+c*d will multiply c by d before b is added. To force the addition first, use parentheses: a=(b+c)*d Remember, think of the equal sign as “gets” as in “a gets the value computed by…”. It is an assignment, not a true mathematical relation. That is, if at some point in the future the value of b was to change, a will not automatically be altered to reflect that change. This allows you to do the following:

    c = c + 1
    

    Type this in. What do you think the result will be?

    The line above may appear a little odd. After all, how can something equal itself plus one? Remember, this is an assignment, not a mathematical relation. What it says is, “Retrieve the current value of c, add one to it, and store the result back in c (overwriting the original value). Print out the value of c. You should be get 26 (the prior value of 25 plus one).

    Continuing with the other math operators, type:

    print( y/x )
    

    The result should be 2.6. Now try the following:

    print( b/a )
    

    The result is also 2.6 even though both variables are integers (an integer, of course, can’t contain a fractional portion). In essence, Python promotes the variables to floats in order to maintain precision, producing a floating point answer. Now try:

    print( b/x )
    

    In this case the answer is again 2.6. This is because in a mixed calculation between a float and an integer, the integer is again promoted to a float in the calculation in order to maintain the precision of the floating point variable. You can force a variable to be promoted (or demoted) by using the float() and int() functions.

    Now try:

    print( b%a )
    

    The result should be 3. The modulo operator produces the remainder of the divide, that is, a goes into b two whole times with 3 left over. Finally, we have floor divide:

    print( 18.2//4.1 )
    

    The result should be 4.0. You can think of floor divide as like integer divide but for floats. That is, 4.1 goes into 18.2 4.0 times. Further, the “left over” is 1.8, which you can verify with the command print( 18.2%4.1 ).

    What do you expect the results to be from the following?

    print( y//x )
    print( y%x )
    

    Type in the above lines and see if you were correct.

    At times it is useful to limit the number of digits that are printed. By default, Python uses up to 18 digits if required. Try this:

    print( x/y )
    

    The result is 0.38461538461538464. To limit this to fewer digits, the round() function may be used. The first argument is the value of expression to be rounded and the second is the number of digits after the decimal point. Now enter this:

    print( round(x/y,3) )
    

    The result should be 0.385 (rounding to the third digit).

    There are also limited operations on strings. The + and * operators when used with strings perform concatenation and iteration. That is, combining strings and repeating strings. Type:

    print( m, n )
    

    You should see:

    Mary Nancy
    

    Now try:

    print( m+n )
    

    The result should be:

    MaryNancy
    

    Note the lack of a separating space. Now type:

    print( m*3 )
    

    The result should be:

    MaryMaryMary
    

    The string is repeated three times. Note that it doesn’t make sense to ask for things like m*2.6 or m-n. We can’t have a fractional copy of something and what would it mean to subtract “Nancy” from “Mary”? These sorts of statements will generate errors.


    This page titled 5.2: Procedure – Output Window is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?