September 3, 2019 Basic Python: Nuts and Bolts, II
In our last post, we took a break from the data science and statistics libraries to focus on core Python. We looked at a program which converted Celsius temperatures to Fahrenheit. The input() function was looked at as we discussed taking keyboard input from the user. Next, we gave a high-level overview of the while loop, which executes if the condition holds true; otherwise, the loop returns false and ends the program. We want to discuss loops and look at decision statements in Python.
As a review, let us look at the temperature conversion program:
response = "Yes" while response == "Yes" or response == "yes" or response == "YES" or response == "Y" or response == "y": fahrenheit = float(input("Enter temperature in Fahrenheit: ")) celsius = 5.0/9.0 * (fahrenheit - 32.0) N = format(celsius, '.2f') print("The equivalent in Celsius is", n) response = input("Do you want to continue?")
The conversion program is placed in a while loop because we want the user to determine if they want to continue the program. To facilitate this decision, we prompt the user to input different versions of the word “yes” from the keyboard. If the user types in the correct character or string value for “yes, ” the program continues. However, if the user types either “no” or types “yes” in a different character combination, the loop terminates and the program ends. Therefore, as long as the condition holds true, the loop continues. The loop ends when the loop holds false. The while loop is one example of a looping statement in Python. Now, we will look at decision statements with another sample program.
Decision statements are structures used to make decisions within a program. The program enters a structure and determines whether or not to execute the statement or not. The following program illustrates this:
#Welcome user print("Welcome to the conversion program.") print("I can take your Celsius temperature as input") print("I can then tell you what you should wear") print("Let's get started, shall we?") #Convert temperature celsius = float(input("Enter temperature in celsius:")) fahrenheit = ((9/5) * celsius) + 32 #Give fashion advise based on temperature if fahrenheit > 70: print("Your temperature is: ", fahrenheit) print("Wear shorts, it's hot out there today!") elif fahrenheit < 70: print("Your temperature is: ", fahrenheit) print("Wear long pants, it's a blizzard outside!") else: print("ERROR!, Out of range") input("Press <Enter> to continue.")
This program takes a temperature and determines if you should bundle up or take off layers of clothing. First, the program makes the conversions listed in our simple conversion program from our last post. This time, we don’t output the temperature. Instead, we test it against an arbitrary value, which is 70 degrees Fahrenheit. If the temperature is greater than 70, we tell the user to wear their summer clothes. But, if the temperature is less than 70, the user must bundle up. We have a concluding decision which outputs an error message if the user types a Celsius temperature and its Fahrenheit conversion is out of range. What you see is an example of the if…elif…else structure. This is a compound structure going through a list of decisions before exiting out of the program. This is a structure you must use in Python if you give the user a choice within a console-based menu. Python does not have a case statement as part of the language. We use dictionaries to implement choices, thus we have to use if…elif…else to implement choices without a dictionary. Lists and dictionaries are covered in our next blog post. For that post, we will make a small return to NumPy and SciPy. The easiest way to understand a decision statement is to create a flowchart to lay out the decisions for your program.
In this post, we reviewed the temperature conversion program and focused on the while loop. Then, we looked at if…elif…else and looked at another program determining attire based on weather conditions. As mentioned in the previous paragraph, we discuss lists and dictionaries in our next post. The final post in our series will do a simple, extended statistical analysis based on a data set. In that post, we explain advanced syntax and hypothesis testing.