In [ ]:
# Students, you'll learn what this means in Lecture #3 so please ignore this for now
from numpy.random import randint as random_number

Stellar Classification

Background

The Harvard Spectral Classification system for stars classifies stars based on their spectral type - where the type of a star is designated as a letter that corresponds to a given temperature range (since different temperatures correspond to different spectra!). Below there is a list of temperatures in Kelvin of a stellar cluster.

Using the table linked from the above Wikipedia page, let's classify each of the stars in our temperature list.


In [ ]:
# These are your stellar temperatures, you're welcome!
temp = [5809, 16589, 4698, 1869, 37809, 8634]

Using if-elif for discrete classification

This problem will use elif statements, the useful sibling of if and else, it basically extends your if statements to test multiple situations. In our current situation, where each star will have exactly one spectral type, elif will really come through to make our if statements more efficient.

Unlike using many if statements, elif only executes if no previous statements have been deemed True. This is nice, especially if we can anticipate which scenarios are most probable.

Let's start with a simple classification problem to get into the mindset of if-elif-else logic.

We want to classify if a random number n is between 0 and 100, 101 and 149, or 150 to infinity.

This could be useful if, for example, we wanted to classify a person's IQ score.

Fill in the if-elif-else statements below so that our number, n, will be classified.

Use a print() statement to print out n and its classification (make sure you are descriptive!)

You can use the following template: print(n, 'your description here')


In [ ]:
# Fill in the parentheses. Don't forget indentation!
n = random_number(50,250) # this should be given!
if ( n <= 100 ):
    print(n,'is less than or equal to 100.')
elif (100 < n <= 150):
    print(n,'is between 100 and 150.')
else:
    print(n, 'is greater than or equal to 150.')

Test your statement a few times so that you see if it works for various numbers. Every time you run the cell, a new random number will be chosen, but you can also set it to make sure that the code works correctly. Just comment (#) before the random_number() function.

Make sure to also test the boundary numbers, as they may act odd if there is a precarious <=.

The loop

We have a list of stellar classifications above. Our new classifier will be a lot like the number classifier, but you will need to use the stellar classification boundaries in Wikipedia's table instead of our previous boundaries.

Another thing you will need to do is make a loop so that each star in temp is classified within one cell!

You can do this with a while-loop, using a dummy index that goes up to len(temp).

Construct a loop such that, for each temperature in temp, you will print out the star's temperature and classification.


In [ ]:
i = 0
end = len(temp)

# Define your loop here
while i < end:
    if temp[i] < 3700:
        print('Star',temp[i],'K is type', 'M')
    elif 3700 <= temp[i] < 5200:
        print('Star',temp[i],'K is type', 'K')
    elif 5200 <= temp[i] < 6000:
        print('Star',temp[i],'K is type', 'G')
    elif 6000 <= temp[i] < 7500:
        print('Star',temp[i],'K is type', 'F')
    elif 7500 <= temp[i] < 10000:
        print('Star',temp[i],'K is type', 'A')
    elif 10000 <= temp[i] < 30000:
        print('Star',temp[i],'K is type', 'B')
    else: # Greater than 30000:
        print('Star', temp[i], 'K is type', 'O')
        
    i = i + 1

Your result should be a printout of each star with the correct stellar type, check the table to make sure they're being classified reasonably!


In [ ]: