Lecture 2 – Expressions and Data Types

DSC 10, Fall 2023

Announcements

Opportunities

Discussion sections

Agenda

There will be lots of programming – follow along in the notebook by clicking the "Expressions and Data Types" link on the course website.

What is code? What are Jupyter Notebooks? 💻

What is code?

Why Python?

Jupyter Notebooks 📓

Aside: Lecture slides

This button!

Expressions

Python as a calculator

Arithmetic operations

Operation Operator Example Value
Addition + 2 + 3 5
Subtraction - 2 - 3 -1
Multiplication * 2 * 3 6
Division / 7 / 3 2.66667
Remainder % 7 % 3 1
Exponentiation ** 2 ** 0.5 1.41421

Python uses the typical order of operations – PEMDAS (BEDMAS? 🛏️)

Activity

In the cell below, replace the ellipses with an expression that's equivalent to

$$(19 + 6 \cdot 3) - 15 \cdot \left(\sqrt{100} \cdot \frac{1}{30}\right) \cdot \frac{3}{5} + \frac{4^2}{2^3} + \left( 6 - \frac{2}{3} \right) \cdot 12 $$

Try to use parentheses only when necessary.

Variables

Motivation

Below, we compute the number of seconds in a year.

If we want to use the above value later in our notebook to find, say, the number of seconds in 12 years, we'd have to copy-and-paste the expression. This is inconvenient, and prone to introducing errors.

It would be great if we could store the initial value and refer to it later on!

Variables and assignment statements

$$ \overbrace{\texttt{zebra}}^{\text{name}} = \overbrace{\texttt{23 - 14}}^{\text{any expression}} $$

Think of variable names as nametags!

Example

Note that before we use it in an assignment statement, triton has no meaning.

After using it in an assignment statement, we can ask Python for its value.

Any time we use triton in an expression, 10 is substituted for it.

Note that the above expression did not change the value of triton, because we did not re-assign triton!

Naming variables

The following assignment statements are valid, but use poor variable names 😕.

The following assignment statements are valid, and use good variable names ✅.

The following "assignment statements" are invalid ❌.

Assignment statements are not mathematical equations!

A variable's value is set at the time of assignment

Assignment statements are not promises – the value of a variable can change!

Note that even after changing uc, we did not change sd, so it is still the same as before.

Concept Check ✅ – Answer at cc.dsc10.com

Assume you have run the following three lines of code:

side_length = 5
area = side_length ** 2
side_length = side_length + 2

What are the values of side_length and area after execution?

A. side_length = 5, area = 25

B. side_length = 5, area = 49

C. side_length = 7, area = 25

D. side_length = 7, area = 49

E. None of the above

Aside: hit tab to autocomplete a set name

Calling functions 📞

Algebraic functions

$$f(x, y) = \frac{x}{y} + 2x^2 + y^5$$ $$f\left(\frac{5-3}{17 \cdot 2}, (4-3)^{-5}\right)$$

Python functions

Some functions can take a variable number of arguments

Put ? after a function's name to see its documentation 📄

Or use the help function, e.g. help(round).

Nested evaluation

We can nest many function calls to evaluate sophisticated expressions.

...how did that work?

Import statements

Example: import math

Some of the many functions built into the math module are sqrt, pow, and log.

math also has constants built in!

Concept Check ✅ – Answer at cc.dsc10.com

Assume you have run the following statements:

x = 3
y = -2

Which of these examples results in an error?

A. abs(x, y)

B. math.pow(x, abs(y))

C. round(x, max(abs(y ** 2)))

D. math.pow(x, math.pow(y, x))

E. More than one of the above

Data types

What's the difference? 🧐

To us, 2.0 and 2 are the same number, $2$. But to Python, these appear to be different!

Data types

Two numeric data types: int and float

int

float

The pitfalls of float

Converting between int and float

Aside: Jupyter memory model

Our notebook still remembers all of the variables we defined earlier in the lecture.

Summary, next time

Summary

Next time

Note: We will introduce some code in labs and homeworks as well. Not everything will be in lecture. You will learn by doing!