{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a9165db4", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "9f44a5bd", "metadata": {}, "source": [ "### Make some arrays that hold star temp and mass" ] }, { "cell_type": "code", "execution_count": 2, "id": "1079a811", "metadata": {}, "outputs": [], "source": [ "# set up arrays holding temperature and sizes of some stars\n", "temp = np.array([ 3000, 8000, 6000, 4000, 9000, 3500, 7000, 10000])\n", "mass = np.array([ 0.514, 2.015, 1.05, 0.75, 2.5, 2.03, 1.51, 0.50])" ] }, { "cell_type": "markdown", "id": "9b002dcd", "metadata": {}, "source": [ "### Define a boolean flag that defines hot stars to be hotter than the Sun" ] }, { "cell_type": "code", "execution_count": 3, "id": "80f5f92c", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 3000 8000 6000 4000 9000 3500 7000 10000]\n", "[False True True False True False True True]\n" ] } ], "source": [ "Tsun = 5770\n", "hot = temp > Tsun\n", "print(temp)\n", "print(hot)" ] }, { "cell_type": "markdown", "id": "44d17740", "metadata": {}, "source": [ "### Work out how many stars there are, and how many of them are hot" ] }, { "cell_type": "code", "execution_count": 4, "id": "c62e9a43", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 8 stars\n", "There are 5 hot stars\n" ] } ], "source": [ "# len() tells you how many elements are in an array\n", "print('There are {} stars'.format(len(temp)))\n", "\n", "# but you can also sum a boolean array to figure out how many satisfy the condition\n", "print('There are {} hot stars'.format(np.sum(hot)))" ] }, { "cell_type": "markdown", "id": "4310ffd2", "metadata": {}, "source": [ "### Work out average mass of all stars, and average mass of just the hot stars" ] }, { "cell_type": "code", "execution_count": 5, "id": "70db1d7f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The average mass of ALL stars is 1.36 Msun\n", "The average mass of HOT stars is 1.52 Msun\n", "The average mass of HOT stars is 1.5150000000000001 Msun <--- yuck!\n" ] } ], "source": [ "# this averages all the star masses in the array\n", "avmass_all = np.average(mass)\n", "# this averages the star mass of just the stars with hot temps: temp[hot]\n", "avmass_hot = np.average(mass[hot])\n", "\n", "# print out the average mass, notice how I make sure not to print out tons of digits!\n", "print('The average mass of ALL stars is {:.2f} Msun'.format(avmass_all))\n", "print('The average mass of HOT stars is {:.2f} Msun'.format(avmass_hot))\n", "\n", "# if I had omitted the ':.2f' it would have looked terrible:\n", "print('The average mass of HOT stars is {} Msun <--- yuck!'.format(avmass_hot))\n" ] }, { "cell_type": "markdown", "id": "66791946", "metadata": {}, "source": [ "### Figure out which stars are more massive than the Sun" ] }, { "cell_type": "code", "execution_count": 6, "id": "19584f6a", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False True True False True True True False]\n", "There are 5 massive stars\n" ] } ], "source": [ "bigmass = mass > 1\n", "print(bigmass)\n", "print('There are {} massive stars'.format(np.sum(bigmass)))" ] }, { "cell_type": "markdown", "id": "9041a55d", "metadata": {}, "source": [ "### Stacking boolean flags" ] }, { "cell_type": "code", "execution_count": 7, "id": "1402ec66", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 4 hot main sequence stars\n" ] } ], "source": [ "# a hot main sequence star should be hotter than the Sun and more massive than the Sun\n", "hot_ms = np.logical_and(hot, bigmass)\n", "print('There are {} hot main sequence stars'.format(np.sum(hot_ms)))" ] }, { "cell_type": "markdown", "id": "c5f77e84", "metadata": {}, "source": [ "### Using the '~' modifier" ] }, { "cell_type": "code", "execution_count": 8, "id": "3be5ec74", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 1 white dwarf stars\n" ] } ], "source": [ "# with a boolean flag, a '~' in front means 'not'\n", "# so white dwarfs are stars that are hot but that are not big mass\n", "wd = np.logical_and(hot, ~bigmass)\n", "\n", "print('There are {} white dwarf stars'.format(np.sum(wd)))" ] }, { "cell_type": "code", "execution_count": null, "id": "5eb637f1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }