lab01.py - \/usr\/bin\/env python coding utf-8 Lab 1 Introduction to Python Welcome to Lab 1 Each week you will complete a lab assignment like this one In

# lab01.py - /usr/bin/env python coding utf-8 Lab 1...

This preview shows page 1 - 3 out of 13 pages.

#!/usr/bin/env python# coding: utf-8# # Lab 1: Introduction to Python# # Welcome to Lab 1! Each week you will complete a lab assignment like this one.In this lab, you'll get started with the Python programming language through numbers, names, and expressions.# # As you go, please regularly select **Save and Checkpoint** from the File menu below the Jupyter logo to save your work.# ## 1. Numbers# # Quantitative information arises everywhere in data science. In addition to representing commands to print out lines, expressions can represent numbers and methods of combining numbers. The expression 3.2500 evaluates to the number 3.25. (Run the cell and see.)# In[1]:3.2500# Notice that we didn't have to print. When you run a notebook cell, if the last line has a value, then Jupyter helpfully prints out that value for you. However, it won't print out prior lines automatically. If you want to print out a prior line, you need to add the print statement. Run the cell below to check.# In[2]:print(2)34# Above, you should see that 4 is the value of the last expression, 2 is printed, but 3 is lost forever because it was neither printed nor last.# # You don't want to print everything all the time anyway. But if you feel sorryfor 3, change the cell above to print it.# ### 1.1. Arithmetic# The line in the next cell subtracts. Its value is what you'd expect. Run it.# In[3]:3.25 - 1.5# Many basic arithmetic operations are built in to Python. The textbook sectionon [Expressions]() describes all the arithmetic operators used in the course. The common operator that differs from typical math notation is **, which raises one number to the powerof the other. So, 2**3 stands for $2^3$ and evaluates to 8. # # The order of operations is what you learned in elementary school, and Python also has parentheses. For example, compare the outputs of the cells below. Use parentheses for a happy new year!
# In[4]:3+6*5-6*3**2*2**3/4*7# In[5]:3+(6*5-(6*3))**2*((2**3)/4*7)# In standard math notation, the first expression is# # $$3 + 6 \times 5 - 6 \times 3^2 \times \frac{2^3}{4} \times 7,$$# # while the second expression is# # $$3 + (6 \times 5 - (6 \times 3))^2 \times (\frac{(2^3)}{4} \times 7).$$# # **Question 1.1.1.** <br /> Write a Python expression in this next cell that's equal to $5 \times (3 \frac{10}{11}) - 49 \frac{1}{3} + 2^{.5 \times 22} + \frac{26}{33}$. That's five times three and ten elevenths, minus 49 and a third, plus two to the power of half of 22, plus 26 33rds. By "$3 \frac{10}{11}$" we mean $3+\frac{10}{11}$, not $3 \times \frac{10}{11}$.# # Replace the ellipses (...) with your expression. Try to use parentheses only when necessary.# # *Hint:* The correct output should be a familiar number.# In[6]:5*(3+10/11)-(49+1/3)+2**(.5*22)+26/33# ## 2. Names# In natural language, we have terminology that lets us quickly reference very complicated concepts. We don't say, "That's a large mammal with brown fur and sharp teeth!" Instead, we just say, "Bear!"# # Similarly, an effective strategy for writing code is to define names for data as we compute it, like a lawyer would define terms for complex ideas at the start of a legal document to simplify the rest of the writing.

#### You've reached the end of your free preview.

Want to read all 13 pages?

• Fall '17
• Human height