{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Programming in Python: Fundamentals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 1a
\n", "\n", "To understand just how exact you need to be with Python, play with the line:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(5.0**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What kind of errors do you get when you make each of these mistakes? (All you need to do right now is copy the code above, make the changes, run the cells and look at the error messages.)
\n", "1. Replace `**` with `^`:
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Use **`write`** in the place of **`print`**:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Use **`Print`** (with a capital P) in the place of **`print`**:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 1b
\n", "\n", "Just like we did above, we're going to see what happens when we try to break some of Python's rules. For both tests, you will start by copying the code below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Solar_Flux = 1366\n", "Albedo = 0.3\n", "Boltzmann_Constant = 5.67e-8\n", "Effective_Temperature = ((Solar_Flux/(4*Boltzmann_Constant))*(1-Albedo))**0.25\n", "print(Effective_Temperature)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens if you:

\n", "\n", "1. Change the name of the variable `Albedo` to `albedo` the **second** time you use it (**but not the first time**):
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Add a space instead of an underscore to the name for the variable representing solar flux (so it is now called `Solar Flux` instead of `Solar_Flux`):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 1c
\n", "\n", "Just to bring the point home, fix the programs below so that they don't give an error message. (Use any value you want for x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Original\n", "print(x+2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Fixed\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Original\n", "y=4\n", "\n", "print(y+z)\n", "\n", "z=2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Fixed\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 2
\n", "\n", "We're going to put everything we have learned so far into practice with an example that is relevant to what we are learning about in the lecture this week.
\n", "\n", "**Your goal is to write a simple program to solve the problem. Your program should include variables with understandable names and comments that explain every step of what you are doing.**

\n", "\n", "A so-called \"blackbody\" is something that emits (and absorbs) radiation with 100% efficiency (see textbook, page 55). Wien's Law states that \"the flux of radiation emitted by a blackbody reaches its peak value at a wavelength $\\lambda_{max}$, which depends inversely on the body's absolute temperature\" (textbook, page 56). If $T$ is the temperature in Kelvin, $\\lambda_{max}$ in microns (10$^{-6}$ m) is calculated as:

\n", "$$\\lambda_{max} = \\frac{2898}{T} $$
\n", "\n", "The surface temperature of the sun is about 5780 K and the surface temperature of the Earth is about 288 K. Write a program to calculate the *wavelength of maximum radiation* for both the sun and the Earth. Your answer can be in either microns or meters, but make sure your comments tell us which one you've used!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 3
\n", "\n", "Copy your solution from Exercise 2 here, and modify it so that at the end it prints out not just the final answer but also which body (sun or Earth) and what the units are." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 4a
\n", "Given a quadratic equation, $ax^2 + bx + c = 0,$\n", "\n", "we can find solutions for the values of x:

\n", "\n", "$$x1 = \\frac{−b+\\sqrt{b^2 −4ac}}{2a},$$ and\n", "$$x2 = \\frac{−b−\\sqrt{b^2 −4ac}}{2a}.$$

\n", "\n", "Note that the $\\sqrt{x}$ is the same thing as $x^{0.5}$.

\n", "\n", "Why does the following program not work correctly?

\n", "\n", "Identify the errors **AND** use comments to create a version of the program that does run correctly.

\n", "\n", "**If you have fixed ALL the problems, you should find that the values for x1 and x2 are roughly 0.618 and -1.618** (you can verify this using a calculator, for example: https://www.calculator.net/quadratic-formula-calculator.html). If you're getting different numbers, that means there are still some ***logic errors*** that you haven't fixed. **Hint** triple check that you have the brackets in the right place!
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Original Program\n", "a = 2\n", "b = 2\n", "c = -2\n", "\n", "q = b**2 - 4ac)**0.5\n", "x1 = -b + q/2*a\n", "x2 = -b - q/2*a\n", "\n", "Print(x1, x2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Fixed Program\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 4b
\n", "Now try to use your new and improved program with
\n", "a = 0
\n", "b = 1
\n", "c = 2

\n", "\n", "Why do you get an error? What does that error mean?
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Fixed Program with new values\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 5: BRINGING IT ALL TOGETHER
\n", "\n", "This final exercise is designed to give you a chance to practice everything you have learned so far today - and also get you into the practice of thinking about what your answers **mean**.

\n", "\n", "### EXERCISE 5a

\n", "\n", "Remember from the start of the practical (or the lectures, or the textbook) that the temperature a planet would have without an atmosphere is called the *effective temperature* of a planet ($T_e$, in Kelvin) and can be calculated from the formula:\n", "$$T_e = \\left (\\dfrac{S}{4\\sigma}*(1-A) \\right )^{0.25}$$\n", "\n", "where:\n", "\n", "* $\\sigma$ is the Stefan-Boltzmann constant, equal to $5.67\\times 10^{-8}$ W/m$^2$/K$^4$\n", "* S is the solar flux reaching the surface of the planet, in W/m$^2$\n", "* A is the albedo, the amount of solar energy that is reflected by the planet's surface, and is unitless.\n", "\n", "Using the parameters in the table below, write a program that:\n", "1. Calculates the effective temperature at Venus, Earth, and Mars\n", "2. Prints the name of each planet and the corresponding effective temperature (including the unit)\n", "\n", "| Planet | Solar Flux (W/m$^2$) | Albedo |\n", "|:-------|:---------------------|:-------|\n", "| Venus | 2643 | 0.8 |\n", "| Earth | 1366 | 0.3 |\n", "| Mars | 593 | 0.2 |\n", "

\n", "\n", "***Do not delete the %reset in the cell below*** (this makes sure your program isn't just \"remembering\" something you did before)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%reset -s -f\n", "# Program for computing the effective temperature of a planet\n", "# YOUR CODE STARTS HERE\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### EXERCISE 5b
\n", "\n", "The actual surface temperatures are\n", "- Venus: 730 K\n", "- Earth: 288 K\n", "- Mars: 218 K

\n", "\n", "Which planet shows the smallest difference between the effective temperature (without an atmosphere) and the actual temperature (with an atmosphere)? Which shows the biggest difference? What can you infer about the atmospheres of these three planets based on these differences?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your answer here, as comments\n", "# Use as many lines as you need to make it readable\n", "#\n", "#\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EXERCISE 6 (extra if you have time and want more of a challenge!)
\n", "We can measure the solar flux at Earth. But what if we want to know the solar flux at another planet? We can find the solar flux S anywhere else in the solar system using the inverse-square law (see textbook, page 54), expressed as:
\n", "$$S = S_0*\\left (\\frac{r_0}{r}\\right ) ^2$$\n", "\n", "where $S_0$ is the known solar flux at a known reference distance from the sun $r_0$ (here, the distance of Earth) and $r$ is the distance from the sun where we would like to calculate the solar flux (in the same units as $r_0$. The distance from the Earth to the Sun is 1.00 AU (AU is an \"astronomical unit\" defined as 149600000 km), and the solar flux at Earth is 1366 W/m$^2$.

\n", "\n", "Write a program that computes the solar flux at a specified planet. Your program should:
\n", "1. Define $S_0$, $r_0$ and $r$ as variables, and include a comment with the corresponding unit.
\n", "2. Print the calculated solar flux, with one decimal place.
\n", "3. Calculate the distance to the planet in km and print using exponential notation with three decimal places.

\n", "\n", "Use the program to calculate the solar flux at:
\n", "- Mars (distance from Mars to the Sun is 1.52 AU)
\n", "- Venus (distance from Venus to the Sun is 0.72 AU).

\n", "\n", "Make sure you use the formatting statements introduced in the optional extras in the lab.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "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.5.4" } }, "nbformat": 4, "nbformat_minor": 1 }