2. Programming languages

Variables, data types, and operations

 
 
In our everyday lives, we constantly interact with computer programs designed to help us with tasks.
 
For example, we constantly use social media to interact with others and keep ourselves informed. When you watch a video on YouTube, you can see how many likes the video has received and you can interact with the video to increase the likes by 1 by liking the video yourself. You can also leave comments to interact with the content creator.
 
These values, such as the number of likes, comments and views, are data that tell us important properties of a video. These properties have values and are stored in variables.
 
Take a look at the example below, which you can run by clicking on the 'Submit' button:
 
 
 

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
 
Why is it important to know the data type? It is very important to know the data type of the variable, as each data type supports certain operations. For example, you can add numbers, but if you try to add a string to a number without converting it, this will result in errors. In the code at the top we have converted an integer to a character string, namely with “str()”. We will use this conversion constantly in our tasks. This was necessary because an integer may not be added to a character string, but it can still be converted to a character string. 
 
This has many similarities with our daily lives. Just as a person of the type “child” is rejected by the operator of the “wine store”, some data types are simply not compatible with some operators. This makes sense, because you can multiply two numbers to get another number, but you can't multiply two texts. A “ticket office” operator might give a discount to a person of type “student” while charging another person of type “worker” the full price for the ticket. Similarly, some operators can work with multiple data types, but in different ways. It makes sense to add two numbers together, but it also makes sense to add two texts together. The word “add” has two similar but different meanings here. For example, you can add the two words “apple” and “juice” together to form a longer word “apple juice”.
 

 

In the next modules, we will refer to these properties of things as “features” in machine learning.

 

EXERCISES

 
We want to support your learning process by involving you in five different activities. Each activity represents a step in building your coding skills, gradually increasing in detail and complexity. Feel free to try again if you don't get it right the first time, but please try hard enough before resorting to our possible solutions.
 

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.

 

In the previous chapter, you learned how to use the Blockly and Python environments. You probably have a preference by now. So select one of the two programming languages and complete the task. Since we recommend being familiar with both, our web app will automatically translate your code written in Blockly to Python and vice versa. Click on one of the instructions and follow it to implement the program below.

Instructions: Blockly (OPTION I)
  1. Replicate the code above in the Blockly interface.  
  2. 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)
                                    
                                
  1. Copy the code from the section above and paste it into the lower code area (on the right-hand side).
  2. 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

    1. Copy and paste the code below into the Python interface.
    2. Create variables for the third day:
      • Define variables for the statistics of the third day: likes_day3, shares_day3, comments_day3 und views_day3.
      • Add the following values:
        • likes_day3 = 2900
        • shares_day3 = 380
        • comments_day3 = 80
        • views_day3 = 48000.0
    3. Calculate engagement:
      • Create a variable namedengagement_day3, that represents the whole engagement by adding likes_day3, shares_day3 and comments_day3.
    4. Calculate the engagement rate:
      • Create a variable engagement_rate_day3. Calculate the engagement rate by using the afforementioned formula.
    5. Print results:
      • Print the values of engagement_day3 and engagement_rate_day3, in order to see the results.

 

4. Possible solution
For the solution in Blockly, copy and paste the Python code into the interface above.
# 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)

 

!     Now it's time to calculate the average engagement rate over the three days.

Create the program, click “Submit” and receive the congratulatory message.
Instructions: You already know the engagement rate of the last three days. Now implement a code that calculates the average engagement rate of the last three days.
Expected result: The average engagement rate is: 6.46786190996%
Hint: You can use parts of the code provided above.

5. Possible solution
To view the solution in Blockly, copy and paste the Python code into the window above.
# 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. +, &&, ==).

Variables are named storage locations in memory that contain data values that can be manipulated and referenced in a program.
 
 
!    

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.