Skip to main content
Engineering LibreTexts

7.1: Introduction

  • Page ID
    26428
  • \( \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}}\)

    Our prior programs could be classified as simple linear or straight-line programs. The program flow was fairly straight forward: Give the user directions, ask the user for data, perform a few calculations based on those data and then print out appropriate results. The next level up in sophistication is the concept of branching. That is, the execution path through the code can vary depending on certain conditions. You might think of this as the program making decisions to do one thing or another. The fundamental conditional operation is the if statement. It looks something like this:

    if conditional expression:
         resulting action
    

    The conditional expression is some manner of test, for example to see if one variable is larger than another. The tests include = = (same as), != (not same as), >, <, >= and <=. The logical directives and and or are also available. The resulting action is any legal block of Python code. It may be a single line or a multitude of lines. So, if the conditional expression is true, the resulting action is performed. If the expression is not true, the action is skipped. In either case, program execution picks up at the next line after the resulting action block. It is extremely important to note that the resulting action block must be indented. All lines of the block must be indented by the same amount. This is how Python recognizes that it is a single block of code.

    As an example, suppose we’d like to test to see if variable A is larger than variable B. If it is, we’d like to print out the message: “It’s bigger”. After this, we want to print out the message “Done”, whether or not A was larger.

    if A > B:
         print( “It’s bigger” )
    
    print( “Done” )
    

    Because the second print statement is not indented, it is not part of the block, therefore it is always executed. If the second print statement had been indented instead, then “Done” would only be printed if A was larger than B. A common beginner’s syntax error is to forget the colon at the end of the if statement.

    For another example, consider that you have a floating point variable named T that represents a computed time in hours. Instead of printing this out as hours with a fraction, you prefer to present it as hours and minutes. A floor divide can be used to obtain the whole hours:

    h = T // 1.0
    

    Similarly, a modulo can be used to obtain the fractional portion, which when multiplied by 60.0 will yield the minutes:

    m = (T % 1.0) * 60.0
    

    So, you could print out the result as follows:

    print( “The time is”, h, “hours and”, m, “minutes” )
    

    Of course, what it the minutes portion works out to zero? Reading something like “The time is 5 hours and 0 minutes” looks a little strange. We’d prefer to leave off the “and 0 minutes” portion. This can be achieved with a simple set of if tests:

    if m != 0.0:
         print( “The time is”, h, “hours and”, m, “minutes” )
    
    if m == 0.0:
         print( “The time is”, h, “hours” )
    

    This sort of “one-or-the-other” construct is fairly common. To make life a little simpler, we can use the else clause:

    if m != 0.0:
         print( “The time is”, h, “hours and”, m, “minutes” )
    else:
         print( “The time is”, h, “hours” )
    

    If m is non-zero, the full print statement is used, otherwise (else) the simplified version is used. Enter the completed program below and try it with several different values, some whole numbers, others not, and inspect the results:

    T = float(input(“Please enter a time value: ”))
    
    h = T // 1.0
    m = (T % 1.0) * 60.0
    
    if m != 0.0:
         print( “The time is”, h, “hours and”, m, “minutes” )
    else:
         print( “The time is”, h, “hours” )
    
    print( “Done!” )
    

    If you look carefully, you might note that under certain circumstances the printout may still be less than satisfactory (hint: what about seconds?). How might this issue be fixed?


    This page titled 7.1: Introduction is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?