bots-as-digital-infrapunctures/content/Section 6 - Critical Interventions Through Bots (exercise)/3-programming-logic.md

142 lines
4.6 KiB
Markdown
Raw Normal View History

Title: Logics of Programming
Slug: 03-s6-step-3
Date: 2020-11-01 12:02
Summary: Variables, data objects, loops and if/else statements.
2020-10-30 16:41:43 +01:00
<!-- > People, things, events are "programmed", one speaks of "inputs" and "outputs", of feedback loops, variables, parameters, processes, and so on, until eventually all contact with concrete situations is abstracted away.[^weizenbaum] -->
<!-- [^weizenbaum]: Weizenbaum, Joseph "Computer Power and Human Reason, From Judgement to Calculation" (1976) -->
As bots are written in code, they are based on the features and constraints of certain logics of programming.
At the end of this section, we will be working on a bot example written in the Python programming language. Even though we will not be able to provide a programming primer here, we would like to introduce some general elements that are used in programming practices. We will look at how the following elements are operating in Python:
* variables
2020-10-30 16:41:43 +01:00
* data objects
* loops
* if/else statements
### Variables
When writing code, information can be temporary stored in the memory of the computer under a specific label, called a *variable*. These can store text (as *strings*) or numbers (for example as *integers*) and function as useful containers to reuse an element multiple times.
```
a = 3 # This is an integer
b = '3' # This is a string (note the quotation marks)
```
Variables can be used in mathematical operations, such as the following:
```
a = 3
b = '3'
a + b
> 6.0
a + b
> Error
a * a
> 9
a * b
> '333'
```
2020-10-30 16:41:43 +01:00
### Data Objects
There are multiple ways to store data, two of them being lists and dictionaries.
2020-10-30 16:41:43 +01:00
**Lists**
2020-10-30 16:41:43 +01:00
```
agents = ['bot', programmer', 'user', 'moderator', 'server', 'regulator']
colors = ['red', 'magenta', 'yellow', 'blue', 'purple', 'green']
objects = ['apple', 'book', 'cable']
some_numbers = [2, 3, 5, 7, 11]
2020-10-30 16:41:43 +01:00
```
**Dictionaries**
2020-10-30 16:41:43 +01:00
```
vocabulary = {
'welcome' : 'Hello, how are you doing?',
'disagreement' : 'That makes no sense to me.',
'greeting' : 'Thanks for this, bye!'
}
2020-10-30 16:41:43 +01:00
```
### Loops
A loop is an element in programming that allows you to execute a single line of code for a certain number of times or until a condition is fulfilled.
2020-10-30 16:41:43 +01:00
By using for example a `range()` loop, you can ask the program to execute a command `x` many times. In the following example, the `print()` command is asked to return the word `bot` as many times as the loop is iterating.
```
2020-10-30 16:41:43 +01:00
for x in range(5):
print('bot' * x)
2020-10-30 16:41:43 +01:00
> bot
> botbot
> botbotbot
> botbotbotbot
> botbotbotbotbot
```
Another type of loop is the `while` loop, which allows to repeat a command for as long as a condition is or isn't fulfilled. This type of loop can be stopped by a manual interruption of the programmer, usually by typing a specific key-combination, such as `CTRL+D`.
2020-10-30 16:41:43 +01:00
```
while True:
print('bot')
2020-10-30 16:41:43 +01:00
> bot
> bot
> bot
> bot
> ...
2020-10-30 16:41:43 +01:00
```
Other loops, such as the `for` loop, are useful to iterate over a specific set of items. If you would like to write a bot that would, for example, post a message of each sentence of a book, you could loop over all the sentences and `print()` them one by one.
```
sentences = [
'This is the first sentence.',
'This is the second sentence.',
'This is the third sentence.'
]
2020-10-30 16:41:43 +01:00
for sentence in sentences:
print('sentence')
2020-10-30 16:41:43 +01:00
> This is the first sentence.
> This is the second sentence.
> This is the third sentence.
```
### if/else statements
2020-10-30 16:41:43 +01:00
A next element in writing your bot could include the description of specific behavior at specific moments. If/else statements are used to trigger certain commands `if` a specific condition is met, or `else` execute an alternative command.
```
for x in range(5):
if x == 2:
print('This is the middle!')
2020-10-30 16:41:43 +01:00
else:
print('Counting ...')
> Counting ...
> Counting ...
> This is the middle!
> Counting ...
> Counting ...
```
These are just a few examples of how the automated tasks of a bot could be written in a programmatic way. Of course there are many more which we will not exhaust in this module, but by describing the basics of how these blocks combine, we can start to comprehend the logical operations and operators that together can be turned into more complex bots. At the same time, this extremely brief introduction in programming features also provides us with some hints at the limitations of such formal languages. For example, try to imagine how you could capture the word *bank* as a variable, both for its understanding as a financial institute and the side of a river. Contextual information is very hard to grasp for computers, computational purists would even argue that it is impossible.