BerkeleyXData8.1x lab01.html - Lab 1 Introduction to Python...

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

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 Out[1]: 3.25 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) 3 4 2 Out[2]: 4 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 sorry for 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
Out[3]: 1.75 Many basic arithmetic operations are built in to Python. The textbook section on 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 power of 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]: 2+6*5-6*3**2*2**3/4*7 Out[4]: -724.0 In [5]: 2+(6*5-(6*3))**2*((2**3)/4*7) Out[5]: 2018.0 In standard math notation, the first expression is $$2 + 6 \times 5 - 6 \times 3^2 \times \frac{2^3}{4} \times 7,$$ while the second expression is $$2 + (6 \times 5 - (6 \times 3))^2 \times (\frac{(2^3)}{4} \times 7).$$ Question 1.1.1. 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{7}{33}$. That's five times three and ten elevenths, minus 49 and a third, plus two to the power of half of 22, minus 7 33rds. By "$3 \frac{10}{11}$" we mean $3+\frac{10} {11}$, not $3 \times \frac{10}{11}$.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture