Week 2: Variables and Types

Week 2: Closure#

Syllabus#

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

  • Assignments and assignment operator = (formal definition)

  • The concept of expressions

  • Variables and types (formal definition)

  • Valid variable names and naming conventions

  • The type() function

  • Concept of namespace (an informal definition)

  • Reassignments

  • Augmented assignments: += , -= , *= , /= , etc.

  • 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

  • By example, the 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

Checkpoint#

Checkpoint 2.1: Road angle #

In cycling, a road gradient of 5% means that for every 100 meters traveled horizontally, you climb 5 meters vertically.

Let \(g\) be the gradient in percent. The road angle \(\alpha\) satisfies

\[ \tan\alpha = \frac{g}{100} \]

Write a Python script road_angle.py where you initialize a variable g for the road gradient (in percent), calculate the road angle in degrees, and print the angle.

Checkpoint 2.2: Elapsed time #

A time interval is measured in seconds. You want to convert this to days, hours, minutes, and seconds.

Write a Python script elapsed_time.py where you initialize a variable seconds for the time interval in seconds, calculate the equivalent number of days, hours, minutes, and seconds, and print the result.