In [1]:
import numpy as np
Make some arrays that hold star temp and mass¶
In [2]:
# set up arrays holding temperature and sizes of some stars
temp = np.array([ 3000, 8000, 6000, 4000, 9000, 3500, 7000, 10000])
mass = np.array([ 0.514, 2.015, 1.05, 0.75, 2.5, 2.03, 1.51, 0.50])
Define a boolean flag that defines hot stars to be hotter than the Sun¶
In [3]:
Tsun = 5770
hot = temp > Tsun
print(temp)
print(hot)
[ 3000 8000 6000 4000 9000 3500 7000 10000] [False True True False True False True True]
Work out how many stars there are, and how many of them are hot¶
In [4]:
# len() tells you how many elements are in an array
print('There are {} stars'.format(len(temp)))
# but you can also sum a boolean array to figure out how many satisfy the condition
print('There are {} hot stars'.format(np.sum(hot)))
There are 8 stars There are 5 hot stars
Work out average mass of all stars, and average mass of just the hot stars¶
In [5]:
# this averages all the star masses in the array
avmass_all = np.average(mass)
# this averages the star mass of just the stars with hot temps: temp[hot]
avmass_hot = np.average(mass[hot])
# print out the average mass, notice how I make sure not to print out tons of digits!
print('The average mass of ALL stars is {:.2f} Msun'.format(avmass_all))
print('The average mass of HOT stars is {:.2f} Msun'.format(avmass_hot))
# if I had omitted the ':.2f' it would have looked terrible:
print('The average mass of HOT stars is {} Msun <--- yuck!'.format(avmass_hot))
The average mass of ALL stars is 1.36 Msun The average mass of HOT stars is 1.52 Msun The average mass of HOT stars is 1.5150000000000001 Msun <--- yuck!
Figure out which stars are more massive than the Sun¶
In [6]:
bigmass = mass > 1
print(bigmass)
print('There are {} massive stars'.format(np.sum(bigmass)))
[False True True False True True True False] There are 5 massive stars
Stacking boolean flags¶
In [7]:
# a hot main sequence star should be hotter than the Sun and more massive than the Sun
hot_ms = np.logical_and(hot, bigmass)
print('There are {} hot main sequence stars'.format(np.sum(hot_ms)))
There are 4 hot main sequence stars
Using the '~' modifier¶
In [8]:
# with a boolean flag, a '~' in front means 'not'
# so white dwarfs are stars that are hot but that are not big mass
wd = np.logical_and(hot, ~bigmass)
print('There are {} white dwarf stars'.format(np.sum(wd)))
There are 1 white dwarf stars
In [ ]: