An Introduction to Python

Basic Data Types and Operations

This notebook contains the programming exercises for An Introduction to Python: Basic Data Types and Operations.


Do the following problems.

Jupyter Notebook Shortcuts:

There are two modes when a cell is highlighted.

Command Mode: Press ESC to activate. The cell has a blue border if this mode is active. In this mode, you can add, delete, create, copy and paste cells.

  • create a new cell above the current cell: a
  • create a new cell below the current cell: b
  • delete the current cell: dd
  • change the current cell’s type to “Code”: y
  • change the current cell’s type to “Markdown”: m

Edit Mode: Press ENTER to activate. The cell has a green border if this mode is active. In this mode, you can edit and type text into the cell.

  • execute the current cell and create a new cell: SHIFT + ENTER

Write code to compute the following. Remember to import the math library.

  • $2^{16}$
  • $\dfrac{2-(3+4\cdot7)}{5}$
  • $(2-3i)+(4+i)$
  • $(2+i)(4-3i)$
  • $\sqrt{234}$
  • 13//4
  • -10 // 3
  • $\sin(3\pi/5)$ (Use math.pi)
In [ ]:

Compute the following boolean expression with code.

x = 3 y = 10 z = -5

  • $x\leq 1$ or $y = 10$
  • $z > 0$ and $y > 0$
  • not $(x\neq 3$ or $y >= -5)$
In [ ]:

Mike's total on five tests is 384. What is the average of his tests?

In [103]:
total = 384

Suppose that $a=5, b=2, c=6$ are three sides of a triangle. The area of the triangle is given by $$A=\sqrt{s(s-a)(s-b)(s-c)}$$

where $s=\dfrac{a+b+c}{2}$. Compute the area of this triangle. Your code should include all 5 variables.

In [ ]:

Use the quadratic formula to solve $x^2+4x-3=0$. The quadratic formula is

\begin{equation} \frac{-b\pm\sqrt{b^2-4ac}}{2a}. \end{equation}

Your answers should be 0.6457513110645907 and -4.645751311064591.

In [1]:
import math # use math.sqrt()

If today is Monday. Which day of the week is $4534$ days from now? (Hint: Use %).

In [ ]:

How many hours, minutes and seconds are in t=9452 seconds? Hint: First use // to find the number of hours then % to extract the remaining difference. Then repeat for minutes and seconds.

Create at least four other variables: hours, minutes and seconds and remaining.

In [ ]: