
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()functionConcept of namespace (an informal definition)
Reassignments
Augmented assignments:
+=,-=,*=,/=, etc.Numeric data types:
int,floatString data type,
strwith single or double quoteString operators
+(concatenation) and*(repetition)The
len()function for stringsType casting with
int(),float(),str()Numeric operators:
%(modulus) and//(floor division)Built-in-functions with multiple arguments:
max(),min(), andprint()The
mathmoduleBy example, the keyword
importand the import statementFunctions from the
mathmodule:math.sin(),math.cos(),math.sqrt(),math.exp(),math.log(),math.ceil(),math.floor().Constant
math.piBoolean data type and
bool()Comparison operators:
==,!=,<,>,<=,>=Logical operators:
and,or,notGood practice, e.g. avoid
print = 6Understanding 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
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.