You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.6 KiB
141 lines
4.6 KiB
4 years ago
|
Title: Logics of Programming
|
||
4 years ago
|
Slug: 03-s6-step-3
|
||
|
Date: 2020-11-01 12:02
|
||
4 years ago
|
Summary: Variables, data objects, loops and if/else statements.
|
||
4 years ago
|
|
||
4 years ago
|
<!-- > 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) -->
|
||
4 years ago
|
|
||
4 years ago
|
As bots are written in code, they are based on the features and constraints of certain logics of programming.
|
||
4 years ago
|
|
||
4 years ago
|
At the end of this track, 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:
|
||
4 years ago
|
|
||
4 years ago
|
* variables
|
||
4 years ago
|
* data objects
|
||
4 years ago
|
* loops
|
||
|
* if/else statements
|
||
4 years ago
|
|
||
|
### 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'
|
||
|
|
||
4 years ago
|
a + b
|
||
4 years ago
|
> 6.0
|
||
|
|
||
4 years ago
|
a + b
|
||
4 years ago
|
> Error
|
||
|
|
||
4 years ago
|
a * a
|
||
4 years ago
|
> 9
|
||
|
|
||
|
a * b
|
||
|
> '333'
|
||
|
```
|
||
4 years ago
|
|
||
4 years ago
|
### Data Objects
|
||
|
|
||
4 years ago
|
There are multiple ways to store data, two of them being lists and dictionaries.
|
||
4 years ago
|
|
||
4 years ago
|
**Lists**
|
||
4 years ago
|
|
||
|
```
|
||
|
agents = ['bot', programmer', 'user', 'moderator', 'server', 'regulator']
|
||
|
colors = ['red', 'magenta', 'yellow', 'blue', 'purple', 'green']
|
||
4 years ago
|
objects = ['apple', 'book', 'cable']
|
||
4 years ago
|
some_numbers = [2, 3, 5, 7, 11]
|
||
4 years ago
|
```
|
||
|
|
||
4 years ago
|
**Dictionaries**
|
||
4 years ago
|
|
||
|
```
|
||
4 years ago
|
vocabulary = {
|
||
4 years ago
|
|
||
4 years ago
|
'welcome' : 'Hello, how are you doing?',
|
||
|
'disagreement' : 'That makes no sense to me.',
|
||
|
'greeting' : 'Thanks for this, bye!'
|
||
4 years ago
|
|
||
|
}
|
||
4 years ago
|
```
|
||
|
|
||
4 years ago
|
### Loops
|
||
|
|
||
4 years ago
|
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.
|
||
4 years ago
|
|
||
|
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.
|
||
4 years ago
|
|
||
|
```
|
||
4 years ago
|
for x in range(5):
|
||
4 years ago
|
print('bot' * x)
|
||
4 years ago
|
|
||
|
> bot
|
||
|
> botbot
|
||
|
> botbotbot
|
||
|
> botbotbotbot
|
||
|
> botbotbotbotbot
|
||
|
```
|
||
|
|
||
4 years ago
|
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`.
|
||
4 years ago
|
|
||
4 years ago
|
```
|
||
|
while True:
|
||
4 years ago
|
print('bot')
|
||
4 years ago
|
|
||
|
> bot
|
||
|
> bot
|
||
|
> bot
|
||
|
> bot
|
||
4 years ago
|
> ...
|
||
4 years ago
|
```
|
||
|
|
||
|
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.
|
||
|
|
||
|
```
|
||
4 years ago
|
sentences = [
|
||
|
|
||
|
'This is the first sentence.',
|
||
|
'This is the second sentence.',
|
||
|
'This is the third sentence.'
|
||
|
|
||
|
]
|
||
4 years ago
|
|
||
|
for sentence in sentences:
|
||
4 years ago
|
print('sentence')
|
||
4 years ago
|
|
||
|
> This is the first sentence.
|
||
|
> This is the second sentence.
|
||
|
> This is the third sentence.
|
||
4 years ago
|
```
|
||
|
|
||
|
### if/else statements
|
||
|
|
||
4 years ago
|
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.
|
||
4 years ago
|
|
||
|
```
|
||
|
for x in range(5):
|
||
|
|
||
4 years ago
|
if x == 2:
|
||
|
print('This is the middle!')
|
||
4 years ago
|
|
||
4 years ago
|
else:
|
||
|
print('Counting ...')
|
||
4 years ago
|
|
||
4 years ago
|
> Counting ...
|
||
|
> Counting ...
|
||
4 years ago
|
> This is the middle!
|
||
|
> Counting ...
|
||
|
> Counting ...
|
||
|
```
|
||
4 years ago
|
|
||
4 years ago
|
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 index the multiple meanings of the word *bank* through 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, some would even argue that it is impossible.
|