Closure#

Syllabus week 2: Variables and types#

  • Concept of statement illustrated by the print statement and assignment statement.

  • Assignments and assignment operator = (formal definition)

  • The concept of expression.

  • Variables and types (formal definition)

  • Valid variable names and naming conventions

  • The type() function

  • Concept of namespace (an informal definition)

  • Reassignments

  • Numeric data types: int, float

  • String data type, str with single or double quote

  • String operators + (concatenation) and * (repetition)

  • The len() function for strings

  • Type casting with int(), float(), str()

  • Numeric operators: % (modulus) and // (floor division)

  • Built-in-functions with multiple arguments: max(), min(), and print()

  • The math module

  • Via example keyword import and the import statement

  • Functions from the math module: math.sin(), math.cos(), math.sqrt(), math.exp(), math.log(), math.ceil(), math.floor().

  • Constant math.pi

  • Boolean data type and bool()

  • Comparison operators: ==, !=, <, >, <=, >=

  • Logical operators: and, or, not

  • Good practice, e.g. avoid print = 6

  • Understanding error messages

Advanced#

Note about the material in Advanced section. The advanced material contains more some additional topics related to the weeks’s content. You are not required to read this material, and none of the exercises or exam questions will rely on this material.

Advanced 2.1: Augmented assignment#

Python provides specific notation which combines a binary operation and an assignment. The table below shows the most commonly used augmented assignments:

Operator

Meaning

Equivalence

+=

Addition

a += b is a = a + b

-=

Subtraction

a -= b is a = a - b

*=

Multiplication

a *= b is a = a * b

/=

Division

a /= b is a = a / b

%=

Modulus

a %= b is a = a % b

**=

Exponentiation

a **= b is a = a ** b

//=

Floor division

a //= b is a = a // b

To illustrate the usage of augmented assignment, consider the code below. Copy and run the code, then observe the printed outputs.

value = 12
increment = 6
value += increment
print(value)  # value = value + increment

value -= increment  
print(value)  # value = value - increment

value /= increment
print(value)  # value = value / increment

value *= increment
print(value)  # value = value * increment

Solve the exercises below using augmented assignment.

Advanced 2.1a: Strange growth#

You are modeling the population dynamics of a virus with an unusual growth model. The initial population of the virus is \(P = 23\) million. According to the growth model, during the 1st hour the population of the virus doubles, and then decreases by 20 million. What is population of the virus after 1 hour?

Advanced 2.1b: Population after 5 hours#

In the subsequent hours, we have:

  • During the 2nd hour: The population of the virus triples, then decreases by 30 million.

  • During the 3rd hour: The population of the virus quadruples, then decreases by 40 million.

  • During the 4th hour: The population of the virus quintuples (5x), then decreases by 50 million. What is population of the virus after the 4th hour?

Advanced 2.2: Problem solving using scientific notation#

Scientific notation allows for compact representation of very small or very large numbers. For instance, the scientific notation for the number \(6720000000\) is \(6.72 \times 10^{9}\)

Python provides a way of defining variables using scientific notation using e, which is the exponent symbol. You can use scientific notation to represent numbers by expressing them as a number followed by e (or E), and then an exponent. For example 3.1e4 is equal to 31000.

To illustrate the usage of scientific notation, consider the code below. Copy and run the code, then observe the printed outputs.

number_scientific_1 = 23e2 # 23 * 10^2 = 2300
number_scientific_2 = 1e-4 # 1 * 10^-4 = 0.0001
number_scientific_3 = 3.14e2  # 3.14 * 10^2 = 314.0
number_scientific_4 = 5.67e-4 # 5.67 * 10^-4 = 0.000567

print(number_scientific_1)
print(number_scientific_2)
print(number_scientific_3)
print(number_scientific_4)

Advanced 2.2a: Finding the energy of a photon using Planck’s Equation#

According to Planck’s Equation, the energy of a photon \(E\) in joules can be calculated from its frequency \(f\) using the formula:

\[E = h \cdot f\]

where \(h = 6.626 \times 10^{−34}\) is Planck’s constant.

The frequency of a photon is related to its wavelength by the formula:

\[f = \frac{c}{\lambda}\]

where \(c = 3 \times 10^{8} \text{ m/s}\) is the speed of light.

Assume you have a light source which produces photons with a wavelength of \(\lambda = 440 \times 10^{-9} \text{ m}\) or 440 nanometers.

How much energy does the photons have according to Planck’s equation? Use scientific notation to calculate the energy \(E\).

Hint: To print a value in scientific notation with 3 digits of precision, you can use one of the two examples print statements code given below:

print(f"The energy of the photon is {energy:.3e} Joules")
print("The energy of the photon is %0.3e Joules" % energy)

Advanced 2.3: Other numeric types#

Python assumes the standard decimal representation when we define variables, however, we can also choose to represent values using other numerical formats. These include hexadecimal, octal and binary.

The difference between these formats are their base. Decimal uses base 10, hexadecimal uses base 16, octal uses base 8, and binary uses base 2.

In Python, a prefix is used to signify which numerical format a value is stored in. These prefixes are:

  • No prefix for decimal.

  • Prefix: 0x for hexadecimal.

  • Prefix: 0o for octal.

  • Prefix: 0b for binary.

The table below shows three examples of decimal numbers as well as their hexadecimal, octal and binary representation.

Decimal

Hexadecimal

Octal

Binary

10

0xa

0o12

0b1010

255

0xff

0o377

0b11111111

1024

0x400

0o2000

0b10000000000

You can change the numerical format of a value using the functions oct, hex and bin which will convert a value into its octal, hexadecimal and binary representation, respectively.

Advanced 2.3a: Change the numerical format of a number#

Assume you a given three variables \(a\), \(b\) and \(c\) with the decimal values \(10\), \(255\) amd \(1024\). Create a small piece of code where you change the value of the variables to hexadecimal, octal and binary format. Print the value of the variables in each of the three formats. Do you get the same values as shown in the table above?