## Practical Evolutionary Algorithms

A practical book on Evolutionary Algorithms that teaches you the concepts and how they’re implemented in practice.

Get the book

## Who is this for?

This crash-course makes the assumption that you already have some programming experience, but perhaps none with Python.

``````# You can write a comment using an octothorpe or 'hash' symbol
``````

## Printing to terminal

``````print("Hello World!")
``````
```Hello World!
```

## Variables and assignment

``````my_number = 5
my_float = 0.2
my_string = "Hello"
also_my_string = "Hello"
``````
``````print("{} is of {}".format(my_number, type(my_number)))
print("{} is of {}".format(my_float, type(my_float)))
print("{} is of {}".format(my_string, type(my_string)))
print("{} is of {}".format(also_my_string, type(also_my_string)))
``````
```5 is of
0.2 is of
Hello is of
Hello is of
```

## Terminal input

``````my_result = input("please enter a word: ")
print("result: {}".format(my_result))
``````
```please enter a word:  hello world
```
```result: hello world
```

## Mathematical operations

``````my_result = 5 + 2  # add
print("resulat: {}".format(my_result))
``````
```result: 7
```
``````my_result = 5 - 2  # substract
print("result: {}".format(my_result))
``````
```result: 3
```
``````my_result = 5 * 2  # multiply
print("result: {}".format(my_result))
``````
```result: 10
```
``````my_result = 5 / 2  # divide
print("result: {}".format(my_result))
``````
```result: 2.5
```
``````my_result = 5 % 2  # modulo
print("result: {}".format(my_result))
``````
```result: 1
```
``````my_result = 5 ** 5  # exponent
print("result: {}".format(my_result))
``````
```result: 3125
```

## Boolean operations

``````my_result = True
print("result: {}".format(my_result))

my_result = False
print("result: {}".format(my_result))
``````
```result: True
result: False
```

And operator

``````my_result = False and False
print("result: {}".format(my_result))

my_result = False and True
print("result: {}".format(my_result))

my_result = True and False
print("result: {}".format(my_result))

my_result = True and True
print("result: {}".format(my_result))
``````
```result: False
result: False
result: False
result: True
```

Or operator

``````my_result = False or False
print("result: {}".format(my_result))

my_result = False or True
print("result: {}".format(my_result))

my_result = True or False
print("result: {}".format(my_result))

my_result = True or True
print("result: {}".format(my_result))
``````
```result: False
result: True
result: True
result: True
```

Not operator

``````my_result = not True
print("result: {}".format(my_result))

my_result = not False
print("result: {}".format(my_result))
``````
```result: False
result: True
```

## Relational operations

``````my_result = 5 > 2  # greater than
print("result: {}".format(my_result))

my_result = 5 >= 2  # greater than or equal to
print("result: {}".format(my_result))

my_result = 5 < 2  # less than
print("result: {}".format(my_result))

my_result = 5 <= 2  # less than or equal to
print("result: {}".format(my_result))

my_result = 5 == 2  # equal to
print("result: {}".format(my_result))
``````
```result: True
result: True
result: False
result: False
result: False
```

## Conditional statements

``````age = 17
uk_drinking_age = 18

if age >= uk_drinking_age:  # IF
print("you can drinking!")
elif age == 17:  # ELSE IF
print("you can drink on your birthday!")
else:  # ELSE
print("you can't drink!")
``````
```you can drink on your birthday!
```

## Data structures

List

``````shopping_list = ["Carrots", "Onions", "Chicken", "Coconuts"]
print(shopping_list)
print(shopping_list[0])  # Python is zero-indexed

shopping_list.append("Ice cream")
print(shopping_list)
``````
```['Carrots', 'Onions', 'Chicken', 'Coconuts']
Carrots
['Carrots', 'Onions', 'Chicken', 'Coconuts', 'Ice cream']
```

Dictionary

``````shopping_list = {"Carrots": 5, "Onions": 2, "Chicken": 1, "Coconuts": 1}

print(shopping_list)
print("Careful - dictionaries are not ordered in older versions of Python.")
print(shopping_list["Carrots"])
``````
```{'Carrots': 5, 'Onions': 2, 'Chicken': 1, 'Coconuts': 1}
Careful - dictionaries are not ordered in older versions of Python.
5
```

Tuple

``````my_result = (5, 6)
print(my_result)

my_result = (5, "hello", 5.5, True, 2)
print(my_result)
``````
```(5, 6)
(5, 'hello', 5.5, True, 2)
```

## Loops

For

``````shopping_list = ["Carrots", "Onions", "Chicken", "Coconuts"]

for item in shopping_list:
print(item)
``````
```Carrots
Onions
Chicken
Coconuts
```
``````for index in range(0,10):
print(index)
``````
```0
1
2
3
4
5
6
7
8
9
```

While

``````number = 0

while(number < 18):
print(number)
number += 1

print("number was no longer below 18!")
``````
```0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
number was no longer below 18!
```

## Functions

``````def my_function():  # create a function
print("No parameter arguments passed in")

my_function()  # call a function
``````
```No parameters passed in
```
``````def another_function(name):
print("Parameter arguments passed in: {}".format(name))

another_function("Derek")
``````
```Parameter passed in: Derek
```
``````def greater_than(left_operand, right_operand):
my_result = left_operand > right_operand
return my_result  # returning a value from the function

print(greater_than(5, 2))  # printing the value returned from the function
``````
```True
```

## Packages

``````import datetime

print(datetime.datetime.now())
``````
```2023-02-12 07:34:40.130861
```

## Practical Evolutionary Algorithms

A practical book on Evolutionary Algorithms that teaches you the concepts and how they’re implemented in practice.

Get the book

## ISBN

978-1-915907-00-4

## Cite

Rostami, S. (2020). Practical Evolutionary Algorithms. Polyra Publishing.