Programming in Python – an illustrative example

In the previous sections, we discussed variable types and data containers. There are many more aspects of Python programming, such as control flow with if/else statements, loops, and comprehensions; functions; and classes and object-oriented programming. Commonly, Python programs are packaged into modules, which are self-standing scripts that can be run from the command line to perform computing tasks.

Let's introduce some of these concepts in Python with a "module" of our own (you can use the Jupyter Notebook for this):

from math import pow


LB_TO_KG = 0.453592
IN_TO_M = 0.0254


class Patient:
def __init__(self, name, weight_lbs, height_in):
self.name = name
self.weight_lbs = weight_lbs
self.weight_kg = weight_lbs * LB_TO_KG
self.height_in = height_in
self.height_m = height_in * IN_TO_M

def calculate_bmi(self):
return self.weight_kg / pow(self.height_m, 2)

def get_height_m(self):
return self.height_m


if __name__ == '__main__':
test_patients = [
Patient('John Smith', 160, 68),
Patient('Patty Johnson', 180, 73)
]
heights = [patient.get_height_m() for patient in test_patients]
print(
"John's height: ", heights[0], ' ',
"Patty's height: ", heights[1], ' ',
"John's BMI: ", test_patients[0].calculate_bmi(), ' ',
"Patty's BMI: ", test_patients[1].calculate_bmi()
)

When you run this code, you should see the following output:

John's height:  1.7271999999999998 
 Patty's height:  1.8541999999999998 
 John's BMI:  24.327647271211504 
 Patty's BMI:  23.74787410486812

The preceding code is a Python module that prints the height and body mass indices (BMIs) for two mock patients. Let's take a more detailed look at each element of this code:

  • The first line of the code block is an import statement. This allows us to import functions and classes that have been written in other modules that are either distributed with Python, written as open source software, or written by ourselves. A module can simply be thought of as a file that contains Python functions, constants, and/or classes. It has a .py extension. To import a module in its entirety, we can simply use the import word followed by the module name, for example, import math. Notice we used the from keyword as well because we only want to import a specific function, the pow() function. This also saves us from the inconvenience of having to type math.pow() every time we want to raise something to a power.
  • The next two lines contain constants that we will use to perform unit conversions. Constants are usually indicated by capital letters.
  • Next, we define a Patient class that has a constructor and two methods. The constructor takes three argumentsthe name, height, and weightand sets three attributes of the specific Patient instance to equal those three values. It also converts the weight from pounds to kilograms and the height from inches to meters, and stores those values in two extra attributes.
  • The two methods are coded as functions, using the def keyword. calculate_bmi() returns the BMI of the patient, while get_height() simply returns the height in meters.
  • Next, we have a mini if statement. All that this if statement is saying is to run the subsequent code only if it is the main module invoked at the command line. Other if statements may have multiple elif clauses and can also include a final else clause.
  • Next, we create a list of two patients, John Smith and Patty Johnson, with their heights and weights as listed in the code.
  • The following line uses a list comprehension to create a list of heights of the two patients. Comprehensions are very popular in Python programming and can also be performed with dictionaries.
  • Finally, our print statement prints the four numbers as the output (the two heights and the two BMI values).
Further references for the base Python programming language are given at the end of this chapter. You can also check out the online documentation at www.python.org.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset