<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introductory Exercises
These exercises are meant to supplement the lecture notes based on Jack VanderPlas's excellent book as well as material from http
Step1:  Hello World
Let's quickly go through some basics of variable assignment.
Step2:  Try to get the following code to print the same message as the one above.
Step3:  Strings
Step4:  We now want to manipulate these strings. A few exercises are below. Take as much time as you need; you are not expected to be able to complete them all.
Step5:  Numbers
While you can define some variables explicitly, Python defines many of them implicitly and can change them when doing math. This mostly matters when working with integers and floats.
Step6:  Let's see this in action below.
Step7:  This type of distinction matters because roundoff errors can be an issue. These are a humongous pain but can be dealt with once you know what to watch out for.
Step8:  Note that everything in Python is at some level an object. Our variables are no exception.
Step9:  Math
Magnitudes are a pretty common unit in astronomy and are defined in terms of $m \equiv -2.5 \log(F)$ where $F$ is in units of flux density (energy/area/time/frequency).
Compute $m$ given $F = b(a-1)^2 + c(a-1) + 5$. Note that not all math operations are included in Python by default.
Step10:  Lists
We can keep track of many objects simultaneously by using lists.
Step11:  Now let's print out an element of the list and capitalize it. We can figure out how to do this within the notebook by taking advantage of tab completion.
Step12:  See if you can figure out how to find where 'australian cattle dog' is located using just standard Python operators and built-in list functions.
Step13:  As before, see if you can figure out how to append items to the end of a list or insert them in the middle of a list. Append 'poodle' to the end of the list, then insert 'chihuahua' after 'border collie' without entering the position by hand.
Step14:  Now change border collie to 'maltese' and remove 'labrador retriever' from the list.
Step15:  Finally, a hugely useful concept in Python is slicing objects, where we can return only relevant portions of an object without modifying the relevant object itself.
Step16:  Tuples
Like lists, tuples can keep track of many items at once. However, unlike lists, they're static and can't be changed after initialization.
Step17:  Why might we want to use tuples instead of lists?
Dictionaries
Dictionaries are extremely flexible mappings of keys to values, and form the basis of much of Python's internal implementation. They are again another alternative to lists.
Step18:  Note that dictionaries do not maintain any sense of order for the input parameters. This is by design, since this lack of ordering allows dictionaries to be implemented very efficiently.
Sets
Sets contain unordered collections of unique items only. It is really fast to perform bulk quick checks but can be hard to search. They are most useful for union, intersection, difference, symmetric difference math operations, and others. Python's sets have all of these operations built-in, via methods or operators.
Step19:  Conditionals
Python's conditional statements are pretty straightforrward.
Step20:  Loops
Python is able to work with any type of iterable, not just integers. This gives loops a lot more flexibility.
Step21:  This flexibility makes loops in Python incredibly powerful. As an example, see if you can get the loop below to run using some combination of (1) range, (2) zip, and (3) enumerate.
Step22:  Alternately, we can use while loops to get things done.
Step23:  Variable (Re-)Assignment
Beware (re-)assigning things without warning! Python by default only "protects" a certain subset of objects by automatically copying them.
Step24:  Good Coding Practices
Finally, now that our code is becoming slightly longer, let's take a step back and talk about good coding practices. Some of these are coded into Python itself.
Step25:  In addition, there's also the PEP8 style guide.
Review
Starting here.
Exercises
Courtesy of Luke Kelley.
References
<ASSISTANT_TASK:>
Python Code:
# Ensures compatibility between Python 2 and 3.
from __future__ import print_function, division
from __builtin__ import range
Explanation: Introductory Exercises
These exercises are meant to supplement the lecture notes based on Jack VanderPlas's excellent book as well as material from http://introtopython.org.
You are encouraged to modify your local version of this iPython notebook. Please add in any additional comments or advice you found helpful and definitely try to experiment with new code!
Preamble
Before we begin, we need to run this code block to ensure the code is compatible between Python 2 and 3.
End of explanation
print('Hello Python world!')
Explanation: Hello World
Let's quickly go through some basics of variable assignment.
End of explanation
...
print(message)
Explanation: Try to get the following code to print the same message as the one above.
End of explanation
# Example of two strings.
my_string = "This is a double-quoted string."
your_string = 'This is a single-quoted string.'
# Example of types.
quote = "Josh once asked, 'Is this distinction useful?'"
followup = 'What do you think?'
Explanation: Strings
End of explanation
# Combine the two lines (`quote`, `followup`) into one string with appropriate spacing.
combined_quote = ...
print(quote, followup)  # printing two strings
print(combined_quote)  # print the combined string
# Check whether the two strings are identical.
s1, s2 = "Test.", 'Test.'
same = ...
print(same)
# Split `my_string` into a separate string for each word.
collection_of_strings = ...
print(collection_of_strings)
# Check whether the *letter* `a` is in `quote`.
a_lettercheck = ...
print(a_lettercheck)
# Check whether the *word* `a` is in `quote`.
a_wordcheck = ...
print(a_wordcheck)
Explanation: We now want to manipulate these strings. A few exercises are below. Take as much time as you need; you are not expected to be able to complete them all.
End of explanation
# Define two variables.
a, b, c = 3, 2, 5.
print('a = {0}'.format(a))
print('b =', b)
print('c', '=', c)
Explanation: Numbers
While you can define some variables explicitly, Python defines many of them implicitly and can change them when doing math. This mostly matters when working with integers and floats.
End of explanation
# Addition.
x = a + b
print(x, type(x))
# Subtraction.
x = c - a
print(x, type(x))
# Multiplication.
x = b * a
print(x, type(x))
# Division.
x = a / b
print(x, type(x))
# Exponentiation.
x = c ** b
print(x, type(x))
# ???
x = b // a
print(x, type(x))
x = c // a
print(x, type(x))
Explanation: Let's see this in action below.
End of explanation
f1, f2 = 0.3, 0.1 + 0.2
f1, f2, f1 == f2  # leaving variables for auto-output
Explanation: This type of distinction matters because roundoff errors can be an issue. These are a humongous pain but can be dealt with once you know what to watch out for.
End of explanation
f1.as_integer_ratio(), f2.as_integer_ratio(), (0.5).as_integer_ratio()
Explanation: Note that everything in Python is at some level an object. Our variables are no exception.
End of explanation
# Compute magnitude given flux.
F = ...
m = ...
print(m)
Explanation: Math
Magnitudes are a pretty common unit in astronomy and are defined in terms of $m \equiv -2.5 \log(F)$ where $F$ is in units of flux density (energy/area/time/frequency).
Compute $m$ given $F = b(a-1)^2 + c(a-1) + 5$. Note that not all math operations are included in Python by default.
End of explanation
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
Explanation: Lists
We can keep track of many objects simultaneously by using lists.
End of explanation
# Access element.
dog = dogs[0]
# Print result.
print(dog.)
Explanation: Now let's print out an element of the list and capitalize it. We can figure out how to do this within the notebook by taking advantage of tab completion.
End of explanation
# Searching list.
dogs.index(...)
Explanation: See if you can figure out how to find where 'australian cattle dog' is located using just standard Python operators and built-in list functions.
End of explanation
# Append.
# Insert.
print(dogs)
Explanation: As before, see if you can figure out how to append items to the end of a list or insert them in the middle of a list. Append 'poodle' to the end of the list, then insert 'chihuahua' after 'border collie' without entering the position by hand.
End of explanation
# Remove element.
dogs.remove(...)
dogs[0] = '...'
print(dogs)
Explanation: Now change border collie to 'maltese' and remove 'labrador retriever' from the list.
End of explanation
# Slice an array
print(dogs[1:3])
print(dogs)
Explanation: Finally, a hugely useful concept in Python is slicing objects, where we can return only relevant portions of an object without modifying the relevant object itself.
End of explanation
date = (6, 11, 2018)
# Check tomorrow's date.
Explanation: Tuples
Like lists, tuples can keep track of many items at once. However, unlike lists, they're static and can't be changed after initialization.
End of explanation
# Initialize dictionary.
numbers = {'one': 1, 'two': 2, 'three': 3}
# Access a value using its key.
print(numbers['two'])
# Add a new element.
numbers['ninety'] = 90
print(numbers)
Explanation: Why might we want to use tuples instead of lists?
Dictionaries
Dictionaries are extremely flexible mappings of keys to values, and form the basis of much of Python's internal implementation. They are again another alternative to lists.
End of explanation
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7, 9}
# Union: items appearing in either
print(primes.union(odds)) # equivalently with a method
# Intersction
{1, 2, 3, 5, 7, 9}
print(primes.intersection(odds)) # equivalently with a method
# difference: Items in primes but not in odds
print(primes.difference(odds)) # equivalently with a method
Explanation: Note that dictionaries do not maintain any sense of order for the input parameters. This is by design, since this lack of ordering allows dictionaries to be implemented very efficiently.
Sets
Sets contain unordered collections of unique items only. It is really fast to perform bulk quick checks but can be hard to search. They are most useful for union, intersection, difference, symmetric difference math operations, and others. Python's sets have all of these operations built-in, via methods or operators.
End of explanation
x = -15
if x == 0:
    print(x, "is zero")
elif x > 0:
    print(x, "is positive")
elif x < 0:
    print(x, "is negative")
else:
    print(x, "is unlike anything I've ever seen...")
Explanation: Conditionals
Python's conditional statements are pretty straightforrward.
End of explanation
# Traditional loop.
for i in range(3):
    print('I like ' + dogs[i] + 's.')
# "Pythonic" loop.
for dog in dogs:
    print('I like ' + dog + 's.')
Explanation: Loops
Python is able to work with any type of iterable, not just integers. This gives loops a lot more flexibility.
End of explanation
for ... in ...:
    print(counter, ':', dog)
Explanation: This flexibility makes loops in Python incredibly powerful. As an example, see if you can get the loop below to run using some combination of (1) range, (2) zip, and (3) enumerate.
End of explanation
i = 0
while i < len(dogs):
    print(i, ':', dogs[i])
    i += 1
Explanation: Alternately, we can use while loops to get things done.
End of explanation
# Example 1.
josh = quote  # reassign
josh += " He got #rekt."  # ???
print(quote)
print(josh)
# Example 2.
quotes = quote.split()  # reassign
josh = quotes
josh += " He got #rekt.".split()  # ???
print(quotes)
print(josh)
# Example 3.
quotes = quote.split()  # reassign
josh = quotes[:]
josh += " He got #rekt.".split()  # ???
print(quotes)
print(josh)
# Example 4.
d = b
d += 3.
print(b)
print(d)
Explanation: Variable (Re-)Assignment
Beware (re-)assigning things without warning! Python by default only "protects" a certain subset of objects by automatically copying them.
End of explanation
import this
Explanation: Good Coding Practices
Finally, now that our code is becoming slightly longer, let's take a step back and talk about good coding practices. Some of these are coded into Python itself.
End of explanation
title = "Banneker + Aztlan Initiative"
month = 6
day = 11
year = 2018
width = 30
Explanation: In addition, there's also the PEP8 style guide.
Review
Starting here.
Exercises
Courtesy of Luke Kelley.
References:
- pyformat.info
docs.python.org/3/library/string.html
Starting with the following variables:
End of explanation
<END_TASK>