Portions of this notebook are based on exercises put together by [Jake Vanderplas](http://www.vanderplas.com), though they've been modified to suit the purposes of this course, including expansion/modification of explanations and additional exercises. Source and license info for the original is on [GitHub](https://github.com/jakevdp/2014_fall_ASTR599/).
In [ ]:
import numpy as np
import astropy.units as u
In [ ]:
faren = input("enter a temperature (in Fahrenheit): ")
print(faren)
Write a function that has no required input, but one optional argument with a default value "C". The function should ask the user what the temperature is and should print that value in Celcius, Farenheit and Kelvin. If the keyword is set to "F" or "K" it should assume the temperature has been given in Farenheit. Otherwise, it should assume Celcius.
In [ ]:
backslashes (\
) start special (escape) characters:
\n = newline (\r = return)
\t = tab
\a = bell
string literals are defined with double quotes or quotes. the outermost quote type cannot be used inside the string (unless it's escaped with a backslash)
In [ ]:
print("green eggs and\n spam")
In [ ]:
# Triple quotes are another way to specify multi-line strings
y = """For score and seven minutes ago,
you folks all learned some basic mathy stuff with Python
and boy were you blown away!"""
print(y)
In [ ]:
# note the ; allows us to do two calculations on the same line
s = "spam" ; e = "eggs"
print(s + e)
In [ ]:
print(s + " and " + e)
In [ ]:
# this one won't work
print('I want' + 3 + ' eggs and no ' + s)
In [ ]:
# but this will
print('I want ' + str(3) + ' eggs and no ' + s)
In [ ]:
print(s*3 + e)
In [ ]:
print("*" * 50)
In [ ]:
print("spam" == "good"); print("spam" == "spam")
In [ ]:
"spam" < "zoo"
In [ ]:
"s" < "spam"
Write a function that does the following:
1) Takes a required input string array containing the names of the people in your group.
2) Asks the person with the longer name for their order first
3) Asks each person to enter (using user input) how many eggs they want, and then whether or not they want spam
4) prints the order in the form "name wants x eggs and [spam/no spam]" with a new line between the two orders
5) prints "name:" + "egg" a number of times equal to the number of eggs ordered + "and" + "SPAM!" or "NO SPAM!"
In [ ]:
In [ ]:
x = 1
In [ ]:
if x > 0:
print("yo")
else:
print("dude")
In [ ]:
# the one line version
"yo" if x > 0 else "dude"
In [ ]:
# conditionals can lie within function calls
print("yo" if x > 0 else "dude")
In [ ]:
z = "no"
In [ ]:
np.sin(np.pi if z=="yes" else 0)
In [ ]:
x = 1
y = 0
In [ ]:
while y < 10:
print("yo" if x > 0 else "dude")
x *= -1
y += 1
In [ ]:
In [ ]:
#can also do this with a break statement
while True:
print("yo" if x > 0 else "dude")
x *= -1
y += 1
if y >= 10:
break
In [ ]:
%%file number_game.py
# The above "magic" command, denoted with the double %% saves the contents
# of the current cell to file. We'll see more of these later
x = 0
max_tries = 10
count = 0
while True:
x_new = int(input("Enter a new number: "))
if x_new > x:
print(" -> it's bigger than the last!")
elif x_new < x:
print(" -> it's smaller than the last!")
else:
print(" -> no change! I'll exit now")
break
x = x_new
count += 1
if count > max_tries:
print("too many tries...")
break
In [ ]:
%run number_game.py
# this magic command runs the given file. It's like typing python number_game.py in the command line
Create a program (and write it to a .py file) which repeatedly asks the user for a word. The program should append all the words together. When the user types a "!", "?", or a ".", the program should print the resulting sentence and exit. For example, a session might look like this:
%run exercise2.py
Enter a word (. ! or ? to end): My
Enter a word (. ! or ? to end): name
Enter a word (. ! or ? to end): is
Enter a word (. ! or ? to end): Walter
Enter a word (. ! or ? to end): White
Enter a word (. ! or ? to end): !
My name is Walter White!
Write a program (and write it to a python file) that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz” instead of the number. A special python operator called modulo and represented with the % sign might help you. A couple of examples of its use are below. See if you can figure out what it does by experimenting.
In [ ]:
4 % 2
In [ ]:
4 % 3
In [ ]:
6.28 % 3.14
In [ ]:
6.28 % 3.1
In [ ]:
25 % 5
In [ ]:
25 % 7
In [1]:
from IPython.core.display import HTML
def css_styling():
styles = open("../custom.css", "r").read()
return HTML(styles)
css_styling()
Out[1]: