Python Basics
  • 15 Oct 2021
  • 13 Minutes to read
  • Dark
    Light

Python Basics

  • Dark
    Light

Article summary

In order to run compound queries, an understanding of the Python programming language is required. Without understanding Python, you will be unable to write reports or even understand reports that have been previously written. This document supplies a brief overview of some of those basic constructs. IMAT reports require Python 3.6. Search the Internet for some Python tutorials, such as Codecademy, to gain a better understanding of the Python language. The Python website may be helpful after some tutorials are completed.

Formatting and Helpful Hints

Correct formatting of each script is important. In fact, the formatting is so important that if done incorrectly an error will occur and the script will either not run or not run correctly. Below are some formatting guidelines and helpful hints for getting the desired output:

  • Every item that is opened with quotation marks, parentheses, brackets, etc. must be closed with that same symbol.
  • Python is case sensitive and should be consistently typed the same each time when entering field names or assigning values, lists names, set names, etc.
  • A colon at the end of a line must appear if using a statement that requires indenting. Below is a list of some common statements that require at least one line of indenting:
    • if
    • else
    • elif
    • for
    • while
    • def
    • do
  • Indentation must be consistent within blocks of code. If tabs are used in the block of code, tabs should always be used; if spaces (Python recommends four) are used, the same amount of spaces should always be used. The script area indents automatically, provided a colon is entered at the end of the first line of the blocked code.
  • Ranges use commas to define the first and last number in the range, or enter one number to include all numbers previous to that number. Use parentheses around the range:
    range(5,15)
  • Similarly, slices (used with lists and dictionaries) provide for a limited number of items to iterate through. Use brackets and colons instead of parentheses and commas.
    list_name[5:8]
  • Most methods, e.g. str() or len(), can be used with strings as well as other types of data.
  • The backslash, \, indicates a special character that should be treated literally and not as part of the Python code.
  • Double backslashes, \\, will allow a backslash to appear in the output.
  • Double percent signs, %%, will cause a percent sign to appear in the output.
  • Each character in a string is assigned a number called an index. Indexes are also used in lists, sets and dictionaries. Counting begins with 0 instead of 1.
    p y t h o n
    0 1 2 3 4 5

Vocabulary and Operators

Variable: changeable information provided by the user

Both the name and the value provided are supplied by the user, the information can include single items of information, lists, sets, numbers, strings, etc. The equal sign is used to assign a value to the variable. Variables should begin with a lowercase letter; capital letters or underscores can be used to denote new words within the variable name, my_name or myName. Below the variable name is my_name.

my_name = "John"

String: set of characters between quotation marks; the quotation marks can be single or double, but the same type must be used for both opening and closing marks

These are treated as one would a written language and can be read as such. A string may be words or numbers. If numbers are treated as a string, they cannot be used for mathematical functions. Below the string is "John".

my_name = "John"

List: multiple items of data separated by commas and contained in brackets, []

Lists have an order, can be changed, contain various data types, and can have duplicates.

farm = ['horse', 'cow', 'pig', 'sheep', 'chicken']

Set: multiple items separated by commas and contained in parentheses and brackets, set([ ])

Sets have no order, can be changed, can contain various types of data, but may not have duplicates.

pets = set(['dog', 'cat', 'rabbit', 'fish', 'iguana'])

Dictionary: similar to a list but contained in braces, {}, and values are accessed by a key

Dictionaries have no order, can be changed, must have key-value pairs, and can be numbers, strings or lists.

kid_menu_costs = {"Grilled Cheese": 4.95, "Hot Dog": 3.59, "Apple Slices": 1.00}

Statement: commands or actions telling the computer what to do with the data provided

if a==b:
    print("a")

Function: a block of reusable code; functions can be defined by the user or be built-in with Python

Functions are generally defined with def, the function name, and (). However, in some cases where the function is built into Python, def can be omitted. In Python 3 print is one such function.

print(kid_menu_costs)

Parameter: acts as a variable name for an argument of a function

Below name and room are the parameters for the function guest.

def guest(name, room):
    print(name + ' is in Room Number ' + room)

Arguments: found inside parentheses and provide the information for a statement or variable

These arguments could be passed into the function above, producing the result, "John is in Room Number 402".

guest("John", "402")

Comparators: equal to, ==; not equal to, !=; greater than, >; greater than or equal to, >=; less than, <; less than or equal to, <=

10 <= 5 * 2

%s: indicates the user is providing a variable and to treat the variable as a string

The number of variables appearing in a print function must match the number of variables in a list if occurring in the print function.

print("%s is in Room Number %s" %(name, room))

#: marks the line as a comment that will not be included in the code

#This is not included in the code, it can be instructions, notes, or code.

'''text''': multiline comment that will not be included in the code

Use a set of three quotes at the beginning and the end of the comment.

'''This is not included in the code,
it can be instructions, notes,
or sections of code that shouldn't be included.
The comment can span multiple lines.'''

len(): provides the number of items included in a list or set, or the number of characters in a string

The example below will give a count of the number of items in the aforementioned list farm. The output would be 5.

print(len(farm))

if, elif, else, while and for

These are some reserved words in Python language and cannot and should not be used for variable names or identifiers of any type.

The following Python script uses the if, elif and else statements. Notice the use of quotation marks around the strings, colons, double equal signs and indentation. In this case, since the variable myname is assigned "John", the output is "Hey there!" If the variable had been assigned the value "Mary" the output would have been, "Hello, stranger."

myname="John"
if myname=="Jane":
    print("Hi, Jane")
elif myname=="John":
    print("Hey there!")
else:
    print("Hello, stranger")

if

Use the if statement to test if an expression is true.

if myname == "Jane":
    print("Hi, Jane")

elif

Use the elif (stands for "else if") statement to test and perform a second if statement if the previous statement is not true. It is possible to have multiple elif statements in a query.

elif myname == "John":
    print("Hey there!")

else

Use else to perform a second task if the previous if or elif statements are not true.

else:
    print("Hello, stranger")

while

The while statement performs a loop that will continue going through the checks until the statement becomes true or breaks. The example below will return all numbers 0 through 9.

number = 0
while(number < 10):
    print("%s" %number)
    number = number + 1

A potential problem with while loops is that the loop will not stop if there is no ending condition. The loop below has an ending condition that tells the computer that the loop should stop when the value becomes greater than 10. The output will return the numbers 0.00, 1.25, 2,50, 3.75 . . . 8.75, 10.00.

notfinished = True
number = 0
while(notfinished):
    print("%0.2f" %number)
    number += 1.25
    if(number > 10):
        notfinished = False

for

Use for to perform a repeated task for every item in a list, set, dictionary or range. This example will create a list of all items in the variable pets. The use of the word "pet" in the for statement is an arbitrary word used as an identifier for each list item in the variable pets. The user could use any word desired--banana, farm, shoe, etc.--but it is best practice to use a word that is related to the contents of the list. Avoid using reserved words in the Python language: print, for, if, def, else, set, etc.

pets = ["dog", "cat", "fish", "rabbit"]
for pet in pets:
    print("%s" %pet)

Lists

Lists are groups of items. They can be changed, may contain duplicates, have an order, and may contain various types of data at the same time. Use field names, floats, integers, strings, dictionaries, etc. within lists. Lists are used mainly to group multiple items into one name that can be used in multiple functions. To create a list, assign it a name, use the equal sign, and place the items within a set of square brackets, [ ].

This example counts the number of items in the list mythings and then returns that number as well as running through the entire list and printing each value along with the item number. Notice that the count begins with 0.

mythings = ['book', 'computer', 'car']
print(len(mythings))
for number in range(len(mythings)):
    print("%s is the %s item in mythings" %(mythings[number], number))

The output of the following query looks like this:

3
book is the 0 item in mythings
computer is the 1 item in mythings
car is the 2 item in mythings
Note:
When assigning indexes to items in a list, Python begins with 0. Therefore, a list containing 6 items has indexes 0 through 5.

It is possible to call a specific item from a list. To do so, use the print function along with the variable name and the index number. The following prints Dialysis Center if using the list myHospitals.

myHospitals = ['Mountain Valley Hospital', 'Urban Urgent Care', 'Suburban Hospital', 'Rural Hospital', 'County Radiology', 'Dialysis Center']

print(myHospitals[5])

To call a slice instead of a single item, follow the example below. This prints Suburban Hospital, Rural Hospital and County Radiology.

Note:
When printing a slice, the first index is included, but the second index is excluded.
print(myHospitals[2:5])

Lists can be edited by adding items, changing items, or editing items. It is also possible to combine items from two or more lists.

To add items to a list, do the following:

To add items to a list, do the following:

myHospitals.append("Intermountain Hospital on C Street")

To change an item in a list, do the following. This will replace the item at index 4 with the new information.

myHospitals[4] = "Intermountain Hospital on B Street"

To delete an item from a list, choose one of the following methods:

my_list.pop(0)
my_list.pop()
my_list.remove("Urban Urgent Care")
del(my_list[1])

Using .pop() will remove the last item if the parens are left empty. Otherwise, include the index of the item to be removed. Using .remove() will remove the item as long as the item matches exactly as it was entered in the list. Using del() also requires the index of the item.

To combine lists, do one of the following. The same two lists are used in both examples.

listA = [x, y, z]
listB = [a, b, c]
print(listA + listB)

Or use this second method:

def combine_lists(x, y):
    return x + y
print(combine_lists(listA, listB))

Python Sets

Sets are similar to lists in that they may contain items of multiple types of data. However, they cannot contain duplicates and have no order. Sets are beneficial because they have the ability to calculate differences and intersections between other sets. A good reference to this can be found here. To create a set, start with the keyword set and include parentheses along with brackets in the variable, ([ ]).

mythings = set(['book', 'computer', 'car', 'book'])
print(len(mythings))
for item in mythings:
    print("%s is in mythings" %(item))

The output of the previous set is below. Notice that one occurrence of book has been removed from the count as well as from the list.

3
car is in mythings
book is in mythings
computer is in mythings

Because sets have no order, they do not support indexing. Thus calling, deleting, or editing an item by an index number is not possible. In fact, sets are mostly immutable (unable to be changed). However, it is possible to add an item:

mythings.add('pots and pans')

It is also possible to remove items from the set:

mythings.remove('computer')

The same two sets will be used in all of the set examples below. Intersecting sets to determine which information is the same in the two sets can be done by using the intersection method.

BStreet_patients = set (["john", "jane", "jr", "baby"])
CStreet_patients = set(['mary', 'jack', 'boy', 'john'])

print(BStreet_patients.intersection(CStreet_patients))

To determine which patients went to only one location, use the symmetric_difference method. This method compares both sets and prints all patients that only attended one hospital.

print(BStreet_patients.symmetric_difference(CStreet_patients))

To determine which patients went to only the first hospital and not the second, use the difference method. This method compares both sets and prints all patients that only attended the first hospital.

print(BStreet_patients.difference(CStreet_patients))

To receive a list of all of the patients that attended either or both hospitals, use the union method:

print(BStreet_patients.union(CStreet_patients))

Dictionaries

Dictionaries provide efficient look up of information. Dictionaries are most beneficial for grouping items of information together; for example, patient names and account numbers. The use of key-value pairs renders it unnecessary for the user to know the index of an item because the item can be accessed by the key.

A basic dictionary looks like the example below. Notice that dictionaries are given a name and assign the information with an equal sign. The information is then entered in key-value pairs and enclosed in braces, { }. Make sure to enter a colon between the key and the value and to separate the pairs with a comma. Either the key or the value may be a string or an integer. It is possible to start with an empty dictionary and add to it as needed.

patient_id = {"John Doe": 123456789, "Jane Doe": 112345678, "Jr. Doe": 111234567}

An empty dictionary looks like this:

my_dictionary = {}

To print an item from a dictionary, do the following:

patient_id = {"John Doe": 123456789, "Jane Doe": 112345678, "Jr. Doe": 111234567}
print(patient_id['John Doe'])

To add an item to the dictionary, do the following:

patient_id["Baby Doe"]=111123456

To delete an item, choose one of the following methods:

del patient_id['John Doe']
patient_id.pop('John Doe')

Remember that items must be typed exactly as it is entered in the dictionary. If spelling or capitalization is off, then the item cannot be located and removed.

To assign a new value:

patient_id["John Doe"] = 987654321

Counts

Counts are most useful when iterating through lists and when comparing a list to a set to see how many duplicates are contained in that list. Use counts with lists, Python sets and dictionaries. To calculate the number of items, use either of the methods below.

The first and most basic method is to use the len() statement.

myHospitals = ['Mountain Valley Hospital', 'Urban Urgent Care', 'Suburban Hospital', 'Rural Hospital', 'County Radiology', 'Dialysis Center']
len(myHospitals)#No print statement was used so nothing will print in the output section

The return for this is 6 because there are six items in the list.

The second method of getting a count is to iterate over a list in a function or statement and incrementally add a count for each item.

myHospitals = ['Mountain Valley Hospital', 'Urban Urgent Care', 'Suburban Hospital', 'Rural Hospital', 'County Radiology', 'Dialysis Center']
count = 0
for item in myHospitals:
    count += 1
print(count)

In the above example, the line count += 1 can also be written count = count + 1 to increment the count by 1.


Averages

Averages can only be computed using integers or floats. Floats include decimal places while an integer does not. In some cases, though the data is a number, it is stored as a string. If this is the case, it is necessary to convert the strings to integers or floats. Use the int() or float() command to make the conversion.

Once the data is in the form of integers or floats, it is possible to calculate the average.

length_admitted = [2, 5, 9, 15]
print(sum(length_admitted)/len(length_admitted))

Was this article helpful?