Closure#
Syllabus Week 1: Introduction#
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
.py
extensionPython 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()
Additional Reading#
Benefits of Using IDLE#
Now that you have seen how Python is executed from the Terminal and from IDLE, you should be able to see the benefits of having an environment to work in when writing Python code. Despite being very simple, IDLE has useful features:
You can write and execute scripts within the same program.
You execute scripts with a button click instead of typing a command.
Syntax highlighting makes the code easier to read.
Code completion helps with writing code faster.
Code is checked for syntax errors before execution.
Definitions are kept after script execution, allowing you to continue interactively. It is important to remember tha under the hood, IDLE uses the same Python interpreter as the CLI.
More About Terminal#
In our course, we only show how to use terminal to run Python scripts. However, it might be useful to know more about terminal, as it you might need it to control your Python installation, or other settings on your computer. Here are links to two tutorials we recommend (where terminal is called command line interface, CLI):
CLI at w3schools - Educational website w3schools is excellent for learning coding and web development, run by a Norwegian company. In this course, we will use often link to w3school as an additional resource.
CLI at Django Girls tutorial - An international non-profit organization Django Girls introduces girls to programming. Their tutorials are excellent for all beginners.
By looking at the table Basic CLI commands for Windows or Linux (Linux commands are also used on MacOS) try using terminal to navigate to a folder, and display its content. You might see some hidden files, which are not shown in the file explorer.
Why Learn Programming?#
Programming is an essential skill for students at technical universities. In 2023, the revised polytechnical foundation (in Danish det polytekniske grundlag) placed programming at the first semester for all bachelor study lines at DTU. You will benefit from programming skills in your studies, your future career, and in everyday life.
Why Learn Python?#
Python is a popular programming language that is widely used in science and engineering. According to TIOBE index, Python is one of the most popular programming language and still gaining popularity. Python is simple, and easy to learn. It is used in scientific fields including physics, chemistry, biology, life science 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. Unexperienced programmers may have hard time trying to find the specific information in Python documentation. Nevertheless, the documentation is a valuable resource for we will link to its specific parts it throughout the course. (Think of the documentation as of a detailed user manual, similar to the user manual for a car.) Official Python tutorial is more suitable for learning Python, but it can still be 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, with a sentence “Do you have Python installed?” we ask if you have the Python interpreter installed on your computer, almost like asking whether your computer understands Python.
Advanced#
Advanced 1.1: The input()
function#
The input()
function is used to get input from the user. Try it out by running the code below.
name = input("Enter your name: ")
print("Hello,", name)
Check also whether you can use input()
without giving it a string argument.
Advanced 1.2: Optional arguments for print()
function#
When given more than one argument, the print()
function will separate them with a space, and add a new line at the end. You can change this behavior by using the optional arguments sep
and end
. Try it out by running the code below.
name = "Sasha"
print("Hello ", name, "!", sep="", end=" ")
print("How are you?")
Advanced 1.3: Weather Report#
The function input()
allows you the make small interactive programs. For example, look at our weather report program below. Try it out.
temperature = input("Enter temperature (in degrees Celsius): ")
condition = input("Enter weather condition (e.g. sunny, rainy, cloudy): ")
print("Weather Forecast: It's ", temperature, " degrees and ", condition, ". ", sep = "", end="")
print("We predict it will stay so for some time. Have a great day!")
Advanced 1.4: Adding some randomness#
A dialog is more fun, when it’s not fully decided in advance. Try running the code below a few times. We will later explain what import random
does. For now, just run the code and observe the output.
import random
number = random.randint(1, 100)
print('My favorite number is', number)
Try also this example. You will learn later why square brackets are used. For now, just run the code.
import random
place = input('Where do you live? Enter the name of the place: ')
attribute = random.choice(['nice', 'cool', 'cozy'])
print(place, 'is a', attribute, 'place!')
Advanced 1.5: Interactive Python from terminal#
To practice different ways of running Python, also without IDLE, try running the interactive Python from the terminal. If you have never used the terminal before, refer to the Python Support page on Terminal for a short introduction.
You should start an interactive Python session in the terminal by typing python3
or python
(depending on your installation). You can then type Python commands directly into the terminal. To exit the interactive session, type exit()
.
In the interactive Python session try:
Printing some text. For example, print your name.
Computing some numbers. For example, compute the square with side length 5.12. Do you need to use
print
to see the result?Writing some code with a syntax error. For example,
print(Hello World)
. What happens?After executing a few commands, try using the up arrow key. What happens?
Advanced 1.6: Running Python scrips from terminal#
To practice different running Python scripts, try executing one of your scripts from the terminal. Also this is shown at Python Support page on Terminal.
To tell the terminal which stript to run, you should either use the full path to the script (shown in Python Support video), or navigate to the folder where the script is located (sketched below), or open the terminal directly in the folder where the script is located (from the right-click menu of your file browser).
To run a script after navigating to the folder, follow these steps:
Identify the script you want to run and ensure you know its location on your computer. This can be one of the scripts you created from IDLE.
Open the terminal on your computer.
Navigate to the folder where your script is located. Use the command
cd
to change the directory.Run the script by entering command
python script_name.py
, but replacescript_name.py
with the name of your script. You might need to usepython3
instead ofpython
, depending on your installation.Observe the output of the script - where is it printed?
Now where you have tried different ways of executing Python, you may revisit our text Executing Python as it should be clearer now.