
Week 1: Closure#
Syllabus#
Basic syntax and terminology:
A program is a text with a sequence of commands executed line-by-line
Each time a program runs, it starts with a clean slate (no previous definitions)
Python programs are saved as text files with a
.pyextensionPython syntax for comments, blank lines, indentation, and case sensitivity
Some ways to execute Python code:
Interactive and script mode in IDLE (you should be able to do this)
Executing Python code from a terminal (you should know of this)
Jupyter notebooks and cell mode for some IDES (you should know of this)
Running Python code directly in a browser (you should know of this)
Key concepts introduced through examples, no formal definition yet:
The
=operatorThe
print()functionNumeric variables for integers and floats
String variables
Common numeric operators:
+,-,*,**,/Operator precedence and the use of parentheses to control precedence
Functions
abs()andround()
Checkpoint#
Checkpoint 1.1 Length of Hypotenuse #
For a right triangle with legs of length a and b, the length of the hypotenuse c can be calculated from the Pythagorean theorem
Write a Python script hypotenuse.py where you perform the computation and print the result. In the script, you should initialize variables for the lengths of the legs of the triangle, calculate the length of the hypotenuse, and print it out.
Checkpoint 1.2 Atoms Per Gram #
Given the mass \(m\) of an element and its molar mass \(M\), we can compute the number of atoms:
where \(N_A\) is Avogadro’s number which is approximately \(6.022 \times 10^{23}\).
For example, take \(10^{-3}\) grams of copper which has a molar mass of 63.546 grams per mole. The number of atoms in this mass of copper is
Write a Python script atoms_per_gram.py where you perform the computation and print the result. In the script, you should initialize variables for the mass of the element, its molar mass, and Avogadro’s number. After calculating the number of atoms in the given mass of the element, print it out.
Additional Reading#
Benefits of Using IDLE#
You can write and run Python programs without an editor. However, using an editor can make coding easier. Despite being very simple, IDLE has useful features:
You can write and run your code in the same place.
You can run your code with a button click instead of typing a command.
Syntax highlighting colors the code to make it easier to read.
Code completion helps with writing code faster with suggestions.
Code is checked for syntax errors before execution.
Definitions are kept after script execution, allowing you to continue working interactively.
Why Learn Programming?#
Programming is an essential skill for students at technical universities. Programming will help you in your studies, your future job, and in daily life.
In 2023, the revised polytechnical foundation (in Danish det polytekniske grundlag) placed programming in the first semester for all DTU bachelor study lines. In the figure below, hover over the connections to see how Computer Programming connects with the other polytechnical foundation courses.
Why Learn Python?#
Python is a popular programming language, especially in science and engineering. According to TIOBE index, Python is one of the most popular programming languages and still gaining popularity. Python is simple and easy to learn. It is used in many scientific fields, including physics, chemistry, biology, life sciences, and data science, and many Python libraries are available for these fields.
Python in xkcd with explanation.
What Exactly Is Python?#
Python is a programming language developed in 1991. We use Python version 3, which was released in 2008. The official documentation describes Python in full detail. Beginners may have a hard time finding specific information in the Python documentation. Still, it is a good resource, and we will link to it during the course. (Think of the documentation as a detailed user manual, similar to the user manual for a car.) The official Python tutorial is better for learning Python, but it can still feel overwhelming for beginners.
The term Python can also refer to the Python interpreter, which is a program that translates Python code into something that your computer understands as instructions. For example, if someone asks, “Do you have Python installed?”, they ask whether you have the interpreter installed on your computer, almost like asking whether your computer understands Python.