Workshop 1: Introduction to Python

  • Intro
  • Download Week 1 Jupyter Notebook
  • The Basics
  • Assigning Variables
  • Mathematical Operations
  • Data Types
  • Basic Python Functions
  • Helpful Resources
  • Conclusion / Next Workshop
  • Required Readings before Workshop 2

Information

Primary software used Python
Software version 1.0
Course Creative Robotics in Spatial Design (Q5 Interfaculty Course from BK)
Primary subject Prototyping and Manufacturing
Secondary subject Robot Programming
Level Beginner
Last updated August 27, 2025
Keywords

Responsible

Teachers
Faculty

Workshop 1: Introduction to Python 0/9

Workshop 1: Introduction to Python

Learn Python, from the absolute basics!

Learning Objectives

  1. Differentiate between data types and their utilities
  2. Assign variables to execute basic functions in Python
  3. Write the logic for mathematical operations in Python code syntax
  4. Develop code to solve real-world problems

This tutorial is published as part of a series of workshops for an interfaculty course from TU Delft Faculty of Architecture and the Built Environment to support learning in and outside the class with open learning material. The intent of the workshop series is to increase familiarity with programming language Python for research and scientific applications as part of the course Creative Robotics in Spatial Design. 

You are free to go through the learning material at your own to get familiar with Python fundamentals.

Workshop 1: Introduction to Python 1/9

Download Week 1 Jupyter Notebooklink copied

Download the Jupyter Notebook here to follow along the exercises. There are two files: one with the codes already typed in them, and one without where you can manually type as you go along (Recommended).

Workshop 1: Introduction to Python 2/9

The Basicslink copied

Important before getting started with the lesson:

  • We will provide all the code examples here that we will try out. Try typing the code instead of copying/pasting to be more hands-on with the lesson.
  • At this stage, no LLM guidance is necessary.
  • Python is case sensitive! Be aware of that.
  • Read the error messages carefully to debug your own problems. 

Open your Jupyter Notebook for week 1 and start from the very top (workshop 1). 

Print Statement

manually type the code here on the left and run it by clicking on the 'play' button
manually type the code here on the left and run it by clicking on the 'play' button

Run the following code by clicking on the play button 

print('Hello, world!')

You just ran a function called `print()` with an argument “Hello, world!”

`print()` is a built-in function of Python that prints the variable between the brackets in the output console just underneath the code.

Commenting Out Code in Python

You can use the hashtag `#` to add comments inside the code. Commenting is also a handy way to deactivate code.

You can do this by highlighting all the lines you wish to deactivate then clicking `ctrl+/` in VS Code to add or remove comments.

Give it a try! If you run the code again, you will see no output being generated this time. 

# print('Hello, world!')

Expected outcome: nothing will be outputted. 

Multi-line comment can be used with triple single quotes. It can span as many lines as you want. The Python interpreter will essentially ignore this block as long as it’s not assigned to a variable or is the last expression in an interactive cell.

 

'''
This is a "multi-line comment" using triple single quotes.
It can span as many lines as you want.
The Python interpreter will essentially ignore this block
as long as it's not assigned to a variable or is the
last expression in an interactive cell.
'''

print("Hello, World!")
# notice that the single and double quotes are both valid way to define a string structure

Workshop 1: Introduction to Python 3/9

Assigning Variableslink copied

Variables in programming are named storage locations that hold data values, allowing developers to reference and manipulate those values throughout the code. They act as placeholders for information that can change or be reused during program execution. Variables are used to store data in the computer’s memory. We tell the computer that there is data stored that has a name. The value can then change, but the name remains.

Each variable has a certain data type. Data types define the kind of values a variable can hold. They determine how the data can be used and what operations can be performed on it.

You can assign variables to any value in Python. Let’s practice a few.

greeting = 'Hello, World!'
x = 0
y = 1

print(greeting)
print(x)
print(y)

 

Important Note: Once you assign a value to a variable, that value is stored and persists. It will remain the same whenever you call the variable again.

For example, a variable `x` will keep its assigned value no matter how many times you use it, unless you explicitly reassign it. This reassignment only affects what comes next; it does not change the behavior of any cells you ran previously, only the ones you run from this point forward… unless you run cells out of order(!).

The variables are assigned globally in the notebook, even if you run the cell backwards
The variables are assigned globally in the notebook, including if you execute out-of-order cells
print(x)

You should get the same value as before. 
If you run subsequent cell to re-assign x to 10, then this also will remember the latest value you’ve assigned

In this example, we’ve declared a variable “x” and assigned a boolean value (True) to it. Then, we reassign the value of variable x to an integer, and after that to a string. The data type of variable “x” is changed accordingly. In most other programming languages, this would not be possible!

x = 0
print(x)
x = 12341265
print(x)
x = "Hello, world!"
print(x)

Output: 

0
12341265
Hello, world!

Print Multiple Arguments with f-string

You can print multiple arguments like this to add additional text in the print message. See below example using f-string format:

print(f'the value of x is: {x}') # slightly different way to print the value of x
# this is the same as: 
print('the value of x is ', x)

Output:

the value of x is: Hello, world!

TIP: the print function has a few arguments that you will see more and more as you progress in your programming learning journey. For your reference, see the possible arguments that you can instruct the print function to behave.

the important arguments for the print function to remember are:

print(‘string to print’, sep=’ ‘, end=’\n’)

Assigning Multiple Variables at Once

You can assign two or more variables at once: 

x, y = 0, 1
print(x, y)

Output:

0 1

Swapping Variables in One Operation

You can swap values of variables at the same time. Meaning, x will take the value of y and vice versa. 

Consider: 

x, y, z = 0, 1, 2
x, z, y = z, y, x
print(x, y, z)

Output:

2 0 1

Shortened Mathematical Notation

In your coding journey, you might discover another way to represent mathematical notation. Consider below:

x = 0
x += 1
print(x)

We use this notation to shorten the often used such as the example provided x = x + 1

Workshop 1: Introduction to Python 4/9

Mathematical Operationslink copied

Below are some examples for the python syntax for common mathematical operations:

Addition:

x = 4 + 7
print(x)

You can use variables and other values in the same operation:

x = 5
y = x + 10
z = x + y
print(z)

Output::

x = 5 > 3
print(x)

Output:

True

Multiplication (*):

x = 2 * 6
print(x)

Division (/):

x = 11 /2 # division operator
y = 11/2 # modulus operator (division remainder)
print("the division result is:", x)
print("the remainder is:", y)

Output:

the division result is: 5.5
the remainder is: 1

Tip: the modulus is a useful tool when asking for the modulus for division by to 2, which will output either 0 or 1, odd or even. A handy way to seperate a list in half.

Mathematical Operations on Lists

Operators can also be used on lists.

Using the + operator for two lists performs a concatenation operation (two connect two elements in a series).

You can perform the * operator on a list to multiply the size of the list.

a = [3600]
b = a * 3 + [4000, 4000]
print(b)

Output:

[3600, 3600, 3600, 4000, 4000]

 This is the same as appending an item to a list (we will learn more about it later):

a = [3600]

a.append(6)
print(a)

Output:

[3600, 6]

 

Other mathematical operations are not allowed with lists such as substraction and division. You cannot add an integer/float value to a list.

These will cause an error:

a=[3] + 6
print(a)

in this case, both items should be lists:

a=[1]
addition = a + a
print(addition)

This will cause an error because the – operator is not supported between two lists:

a = [1, 2, 3]
b = [4, 5, 6]
list_a = a - b

Workshop 1: Introduction to Python 5/9

Data Typeslink copied

The most common data types that you will be working with include:

Integer (int):

Represents whole numbers, such as 3 or -42.

Float (float):

Represents decimal numbers, like 3.14 or -0.001.

String (str):

A sequence of characters enclosed in quotes, such as “hello” or ‘Python’.

Boolean (bool):

Represents truth values, either True or False.

List (list):

An ordered, mutable collection of items which can hold different data types, defined with square brackets, e.g., [1, 2.0, ‘three’].

Array (NumPy):

A special variable, which can hold more than one value at a time of the same data type., e.g., [1, 2, 3]

Tuple (tuple):

An ordered, immutable collection of items, defined with parentheses, e.g., (1, 2, 3).

Dictionary (dict):

An unordered collection of key-value pairs, defined with curly braces, e.g., {“key”: “value”}.

Set (set):

an unordered collection of unique items. Sets are useful when you want to store multiple values without duplicates. They are defined using curly braces {}, e.g., {1, 2, 3} 

integer = 1
float = 1.0
string = 'text'
boolean = True
list = [1, 2.0, 'three']
tuple = (1,2,3,4)
dictionary = {'fruit':'apple'}
set = {1, 2.0, 'three'}

You can read more about data types here: https://www.w3schools.com/python/python_datatypes.asp

Workshop 1: Introduction to Python 6/9

Basic Python Functionslink copied

We can retrieve the function of a variable or data attribute by calling a function called `type()`. This function gives us the type of data that we just gave him.

For example, you can check the data type of a variable by typing `type(1.0)` and it should output `float`. You can create a print wrap it in a `print()` function to explicitly display it in the console. For example, `print(type(1.0))`

Check the data type:

type(1), type(1.0), type('text'), type(True), type([1, 2.0, 'three']), type((1,2,3,4)), type({'fruit':'apple'}), type({1, 2.0, 'three'})

Output:

(int, float, str, bool, list, tuple, dict, set)

Declare Data Types

It is possible to explicitely declare variable types:

u = 9.0
print(type(u))
print(type(int(u))) # changed from float to integer

# to make the change last, re-define the variable
u = str(u)
print(type(u))

Rounding Float Values

You can round numbers using the `round()` function which rounds to the nearest integer or float with the specified number of decimal digits. The `round()` function asks for two arguments which are separated by a comma.

  1. Required: a float numeric value, e.g. `9.58290583`
  2. Optional: the number of decimals, e.g. `1`. Otherwise the default is `0`
# using the round() function can convert your data from float to integer
v = 9.6668956489
v = round(9.6668956489)
print(type(v))
v = round(9.6668956489, 3)
print(type(v))

Select an Item from a List

You can select specific value within a list variable using the `[]` bracket after the list and choosing the index of the item in the list.

v = ["a", "b", "c", "d", "e", "f"]
print(v)
print(type(v))

print(v[0])
print(v[3])
print(v[5])

Output: 

['a', 'b', 'c', 'd', 'e', 'f']
<class 'list'>
a
d
f

Important Note: the first item/index for any list or operation is always 0 in Python. The first index value is therefore index `0`, the second index is `1`, and so on. 

List Length

You can ask Python to count how many items are in a list using the function `len()`

len(v) # Gives us the lengths of the list
print('the number of items in the list is:', len(v))

 

Replace Values from a List

We can use the index in order to call a specific item from the list. Note that items always begin at index 0.

If we want to know the thrid item we call the item with index 2. In the code above you can see how we call them.

We can also change the items simply by replaing the value like this:

v[0] = 'first item in the list' # replaces the first (zeroth) item no the list using the index value of 0
print(v)

Output:

['first item in the list', 'b', 'c', 'd', 'e', 'f']

In addition to defining your own value using the index value of a list, like `v[0]`, you can also append and insert new items to the list, a far more common way to add new items to a list

v.append("new item to the list") 
print(v)

Output:

['first item in the list', 'b', 'c', 'd', 'e', 'f', 'new item to the list']

 

You can also specify the position on the list where you like to insert an value:

v.insert(1, "second position in the list") # select where a new item you want to insert in the list
print(v)

Output:

['first item in the list', 'second position in the list', 'b', 'c', 'd', 'e', 'f', 'new item to the list']

 

A list could also be extended by another list. Examine below:

u = [1, 2, 3]
w = ['a', 'b', 'c']

u.extend(w) # equivelent to `u + v`
print(u)

Output:

[1, 2, 3, 'a', 'b', 'c']

Nested Lists (a list within a list)

A list variable can also contain sublists within them. Examine the example below: 

u = [1, 2, 3]
w = ['a', 'b', 'c']
u_w = [u, v]

print(u_w)
print(u_w[0][1])

Output: 

[[1, 2, 3], ['a', 'b', 'c']]
2

 

Workshop 1: Introduction to Python 7/9

Helpful Resourceslink copied

Recommended Pythin Learning Resources:

Comprehensive Python Tutorials:

Test Your Knowledge:

Helpful Short Educational Videos:

 

Workshop 1: Introduction to Python 8/9

Conclusion / Next Workshoplink copied

As you will notice throughout this course, in coding there are often multiple approaches to solve the same problem. There’s rarely a single “best” way to code something: different solutions can be equally valid, depending on factors like readability, efficiency, or personal preference.

Take the opportunity to experiment with different techniques and adopt a problem-solving attitude. By exploring various methods, you’ll deepen your understanding and discover the most effective solutions for different scenarios. 

Workshop 1: Introduction to Python 9/9

Required Readings before Workshop 2link copied