III. Variables, data types, and operations
2. Programming languages
Variables, data types, and operations
The values can vary in type. For example, we can store text, numbers or Boolean values (true or false) in our variables. In our example, text and numbers are stored in our variables. The variables 'likes', 'comments', 'shares' all have numeric values, while 'topic' has a text value. Normally, in the syntax of programming languages, we say that the data type of a variable is “string” if the value assigned to the variable is text. And we can say that the data type of a variable is either “int” or “float” if the value stored in the variable is a number.
Data types
The following table explains some important data types in Python. Familiarize yourself with the data types, as texts are now called 'strings (str)' and numbers either 'integers (int)' or 'floating point numbers'.
| Data type | Identifier | Description | Example |
|---|---|---|---|
int |
Integer | Represents whole numbers, positive or negative, without decimals. | x =
42 |
float |
Floating point number | Represents decimal numbers (floating point numbers). | y =
3.14 |
str |
String | Represents text that is enclosed in single or double quotation marks. | name =
'Alice' |
bool |
Boolesch | Represents a truth values, either True or False. |
is_valid = True |
In addition, as can be seen in our example, we assign the forest characteristic values to the variable with “=”. We classify „=“ as operator. Operators are symbols or keywords in programming languages that perform operations on operands, such as arithmetic, logical or comparison tasks (i.e. +, &&, ==).
Arithmetic operators
The following table explains some arithmetic operators and how they are defined differently for different data types. Int and float usually work in the same way, but with str they sometimes behave quite differently.
| Operator | Operation | int | float | str |
|---|---|---|---|---|
| + | Addition | Adds whole numbers | Adds decimal numbers | Concatenates strings |
| - | Subtraction | Subtracts whole numbers | Subtracts decimal numbers | Not applicable |
| * | Multiplication | Multiply whole numbers | Multiply decimal numbers | Repeats strings, only applicable with int |
| / | Division | Divides whole numbers (Result is a float) | Divides decimal numbers | Not applicable |
| // | Integer division | Integer division (Result is rounded off) | Decimal point division, result rounded down | Not applicable |
| % | Modulo | Rest of a division | Rest of a decimal number division | Not applicable |
| ** | Potentiation | Raises an integer to a power | Raises decimal point to power | Not applicable |
In the next modules, we will refer to these properties of things as “features” in machine learning.
EXERCISES
A journalist has published a breaking news story in one of Germany's most popular online newspapers. Now she is keen to collect relevant data about user engagement with the article to share with her boss.
Could you help her by completing the following tasks?
EXERCISE 1
The first piece of information she was interested in was the engagement trend of the contribution over time to see if it was increasing, decreasing or remaining stable.
Now watch the code and answer the questions. Achieve 100% in this quiz. Don't hesitate to try again if you don't get it right the first time.

EXERCISE 2
Now you want to help the journalist to prepare the data more professionally for her boss. To do this, you present the metric of the engagement rate. Simply put, engagement rate is a metric that measures how actively people interact with content. The engagement rate is usually calculated by taking the total number of interactions (engagements) and dividing it by the reach (the number of people who have seen the content), then multiplying by 100 to get a percentage. A high engagement rate indicates that people find the content valuable or interesting, while a low engagement rate could indicate that the content is not resonating with the target audience. This rate helps creators understand what content works best and refine their strategies.
Based on that, let's implement an algorithm that takes the engagement value on day 01 and the number of views of the post on day 01 and calculates the engagement rate so we can better explain the metric to the journalist.
Run the code in Blockly or Python, submit it and get a “congratulations” message in both.
Instructions: Blockly (OPTION I)
- Replicate the code above in the Blockly interface.
- Click on 'Submit' to check that the result is actually what we wanted.
Instructions: Python (OPTION II)
engagement_day1 = 4520.0
views_day1 = 67000.0
engagement_rate_day1 = (engagement_day1 / views_day1) * 100
output_message = "The engagement rate of the article on Day 1 is: " + str(engagement_rate_day1) + "%"
print(output_message)
- Copy the code from the section above and paste it into the lower code area (on the right-hand side).
- Click on 'Submit' to check that the result is actually what we wanted.
Just for fun: What do you think the code will output? Make a guess and run the program to see if your guess was correct. Why do you think this result was output?
EXERCISE 3
Now the journalist wants to calculate the engagement rate for the second day. However, her code contains an error.
Investigate and fix the error in the given code and submit the code to receive the 'Congratulations' message.
The following code uses an incorrect definition of the engagement rate. Correct the code and submit it to receive the Congratulations message! Note that you only need to change one line of code. It should output this message:
The engagement rate of the article on day 2 is: 5.65731707317%
Hint: Remember that the engagement rate is usually calculated by dividing the total number of interactions (engagements) by the total reach (number of people who have seen the content) and then multiplying by 100 to get a percentage.
3. Possible solution

engagement_day2 = 4639
view_count_day2 = 82000
engagement_rate_day2 = (engagement_day2)/view_count_day2 * 100
output_message = ("The engagement rate of the article on day 2 is: " + str(engagement_rate_day2) + "%")
print(output_message)
EXERCISE 4 (OPTIONAL)
Now we have the data from the third day. Calculate the engagement on the third day and also the engagement rate.
Modify the following code to complete the task and submit it to receive the congratulatory message.
Instructions
- Copy and paste the code below into the Python interface.
- Create variables for the third day:
- Define variables for the statistics of the third day:
likes_day3,shares_day3,comments_day3undviews_day3. - Add the following values:
likes_day3 = 2900shares_day3 = 380comments_day3 = 80views_day3 = 48000.0
- Define variables for the statistics of the third day:
- Calculate engagement:
- Create a variable named
engagement_day3, that represents the whole engagement by addinglikes_day3,shares_day3andcomments_day3.
- Create a variable named
- Calculate the engagement rate:
- Create a variable
engagement_rate_day3. Calculate the engagement rate by using the afforementioned formula.
- Create a variable
- Print results:
- Print the values of
engagement_day3andengagement_rate_day3, in order to see the results.
- Print the values of
4. Possible solution
# Day 1
likes_day1 = 3960
shares_day1 = 410
comments_day1 = 150
views_day1 = 67000.0
engagement_day1 = likes_day1 + shares_day1 + comments_day1
engagement_rate_day1 = (engagement_day1 / views_day1) * 100
# Day 2
likes_day2 = 3908
shares_day2 = 523
comments_day2 = 208
views_day2 = 82000.0
engagement_day2 = likes_day2 + shares_day2 + comments_day2
engagement_rate_day2 = (engagement_day2 / views_day2) * 100
# Day 3
likes_day3 = 2900
shares_day3 = 380
comments_day3 = 80
views_day3 = 48000.0
engagement_day3 = likes_day3 + shares_day3 + comments_day3
engagement_rate_day3 = (engagement_day3 / views_day3) * 100
print("Engagement Day 1:" + str(engagement_day1))
print("Engagement Day 2:" + str(engagement_day2))
print("Engagement Day 3:" + str(engagement_day3))
print("Engagement Rate Day 1:" + str(engagement_rate_day1) + "%")
print("Engagement Rate Day 2:" + str(engagement_rate_day2) + "%")
print("Engagement Rate Day 3:" + str(engagement_rate_day3) + "%")
EXERCISE 5 (OPTIONAL)
Create the program, click “Submit” and receive the congratulatory message.
5. Possible solution
# Day 1
likes_day1 = 3960
shares_day1 = 410
comments_day1 = 150
views_day1 = 67000.0
engagement_day1 = likes_day1 + shares_day1 + comments_day1
engagement_rate_day1 = (engagement_day1 / views_day1) * 100
# Day 2
likes_day2 = 3908
shares_day2 = 523
comments_day2 = 208
views_day2 = 82000.0
engagement_day2 = likes_day2 + shares_day2 + comments_day2
engagement_rate_day2 = (engagement_day2 / views_day2) * 100
# Day 3
likes_day3 = 2900
shares_day3 = 380
comments_day3 = 80
views_day3 = 48000.0
engagement_day3 = likes_day3 + shares_day3 + comments_day3
engagement_rate_day3 = (engagement_day3 / views_day3) * 100
engagement_rate_average = (engagement_rate_day1 + engagement_rate_day2 + engagement_rate_day3) / 3
print("The average engagement rate is:" + str(engagement_rate_average) + "%")
Operators are symbols or keywords in programming languages that perform operations on operands, such as arithmetic, logical or comparison tasks (z.B. +, &&, ==).
Learning message
We learned that data is stored in variables and can be manipulated by operators . Every type of data data type, that makes operators work in different ways.