## Logics of Programming

As bots are written in code, they are based on the features and constraints of certain logics of programming.

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:

• variables
• 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'
``````

### Data Objects

There are multiple ways to store data, two of them being lists and dictionaries.

Lists

``````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]
``````

Dictionaries

``````vocabulary = {

'welcome' : 'Hello, how are you doing?',
'disagreement' : 'That makes no sense to me.',
'greeting' : 'Thanks for this, bye!'

}
``````

### 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.

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.

``````for x in range(5):
print('bot' * x)

> 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`.

``````while True:
print('bot')

> bot
> bot
> bot
> bot
> ...
``````

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.'

]

for sentence in sentences:
print('sentence')

> This is the first sentence.
> This is the second sentence.
> This is the third sentence.
``````

### if/else statements

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!')

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 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.