Lecture 3 – Variables and Types

Data 6, Summer 2021

Functions

Quick Check 1

Variables

Quick Check 2

Types

Typecasting

Quick Check 3

Demo

You should ignore most of this code.

I've loaded in a table of information from Wikipedia, containing the population of each country both in absolute terms ("Population") and as a proportion of the total global population ("%").

Unsurprisingly, values in the "Country" column are stored as strings.

Values in the "Population" column are stored as integers.

And values in the "%" column are stored as floats.

For fun, replace the variable country with one that you like and you'll see its population formatted nicely as a string.

Practice

Sometimes, we will include extra practice problems at the end for you to work on after class. These are not required or graded in any way, but they're highly recommended.

To see the solution for a given problem, you can click the triangle next to "Answer".

Question 1

WITHOUT running any code, what does the following expression evaluate to?

max(min(14, 15), abs(16 - min(17, max(-5, 15), -9)))

You should write out your thought process on paper.

Answer 25

Question 2

Recall, the Pythagorean theorem is $$c^2 = a^2 + b^2$$

Using the Pythagorean therem, set the variable q2 equal to the length of the longest side of a right-angled triangle whose other side-lengths are side_1 and side_2. Your code should not use the numbers 8 or 13 directly, but it should instead use the variables side_1 and side_2.

Hint: x**0.5 computes the square root of x.

Answer q2 = (side_1**2 + side_2**2)**0.5

Question 3

Assign the variable q3 to a string that reads 'Lisa Jobs is 45 years old and lives in Canada.'

You MUST use the four variables defined below to create the string. This is similar to the example on slide 50 of the lecture.

Hint: You will need to cast the variable age to an integer so that you can change it from 42 to 45.

Answer q3 = first_name + ' ' + last_name + ' is ' + str(int(age) + 3) + ' years old and lives in ' + location + '.'