---
# System prepended metadata

title: 11-18 If-else 1
tags: [Unit 4 - Conditionals]

---

>Lord, you rule the world with a good and rightful code.
We ask for your wisdom to see things correctly and represent them faithfully.
Fill us with your justice so that we may know how to reflect it in the way we code,
Give us humility so that we may know the limits of our knowledge and its application in society,
Give us the courage to stand firmly when we feel pressured to go beyond these limits,
And give us responsibility as rulers we may be in the areas of society where you put us.
Lord, we want to reflect your good rule, so that people of all nations may fear your name.
Make known to us the path of life;
For in your presence there is fullness of joy;
and at your right hand are pleasures forevermore (Psalm 16).
Amen.

# Comparison operators

| Operator | Name                     |
|----------|--------------------------|
| ==       | Equal                    |
| !=       | Not equal                |
| \>       | Greater than             |
| \<       | Less than                |
| \>=      | Greater than or equal to |
| \<=      | Less than or equal to    |

-   Result will always be a Boolean object! (`True` or `False`)
-   *In principle*, works for numbers (`int`, `float`, `complex`), sequences (`list`, `tuple`, `string`) and dictionaries (`dict`)

## Floating point comparisons

```python
a = 0.15 + 0.15
b = 0.10 + 0.20

print(a == b) # False
print(a >= b) # False
```

What is happening here? How might we perform a reasonable comparison between these floating point numbers?

One way to solve: use `math.isclose()`:

```python
import math

a = 0.15 + 0.15
b = 0.10 + 0.20

print(math.isclose(a,b)) # True
```

## Sequence comparison

-   For equality and non-equality:

```python
a = "Hi"
b = "Hi"
c = "Hello"
print(a == b) # True
print(a == c) # False
print(a != b) # False
print(a != c) # True
```

However, order matters:

```python
a = [1,2]
b = [2,1]
print(a == b) # False
```

*(after all, with strings it is also easy to see that `Hi` is different than `iH`).*

If we need to ignore order, and compare only elements, we can **compare the sorted lists**:

```python
a = [1,2]
b = [2,1]
print(sorted(a) == sorted(b)) # True
```

## Greater and smaller than

Sequences follow a [lexicographical ordering](https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types): first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

For example, all comparisons below are `True`:

```python
(1, 2, 3)              < (1, 2, 4)
[1, 2, 3]              < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4)           < (1, 2, 4)
(1, 2)                 < (1, 2, -1)
(1, 2, 3)             == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)
```

## Comparison operator chaining

Differently from most other languages, Python supports operator chaining, like `2 < x < 5`.

-   Chaining compares left to right, evaluating a \< b first
-   If the result is true, then b \< c is evaluated next

```python
age = int(input('Please enter your age: '))
if 12 < age < 20:
  print('You are a teenager')
```

# Membership operators

-   The membership operators in returns `True` if the left operand matches the value of some element in a **container**

```python
sizes = [3, 5, 7, 9.5, 10, 10.5, 11]
if 10 in sizes:
  print('Sizes include size 10')
```

It can also be used to check whether a substring is part of a larger string:

```python
data = '123abc456'
if 'abc' in data:
  print('abc found in data')
```

## Remember precedence order!

1.  Parentheses: `()`
2.  Exponents: `**`
3.  Multiplication, divisions and modulus: `* / // %`
4.  Addition and subtraction: `+ -`
5.  Comparisons: `<= < >= > == != is in`
6.  Boolean `not`
7.  Boolean `and`
8.  Boolean `or`

# If statement

-   What if we want our code to execute different code given some conditions?

-   In Python, an if statement takes the following form:

```python
if expression:
  # statements executed if expression is true
# statements following the if expression (execute independently of expression being true or false)
```

-   Attention to **indentation**: all lines that will be executed if the expression is true must start with a "tab" or 4 whitespaces
    -   Lines of code that are not indented ("inside the if") just are followed after it is finished

# Exercise: are you a horse?

![amIahorse](https://hackmd.io/_uploads/Bk1b9JFf1x.jpg)


# Algorithmic intelligence

**Medical Expert Systems** in the decades of 90 and 2000 used to have a similar behavior of what we are seeing so far: checking conditions and following through a flowchart. Programmers would add many Medical Logic Modules (MLM) to the health information system indicating what conditions and actions would be taken given some patient's input.

This is today called the paradigm **Classic or Symbolic Artificial Intelligence**. (Nowadays we have a different paradigm, Adaptive AI, which uses data to train models instead of just elaborating logic flows).

* Questions:
  * Can we consider intelligence as just following logic flowcharts?
  * Is thinking just an algorithm? (See: [computationalism/computational theory of mind](https://en.wikipedia.org/wiki/Computational_theory_of_mind))
  * Is everything an algorithm? (See: [pancomputationalism](https://philpapers.org/browse/pancomputationalism))
  
![ai_scooby](https://hackmd.io/_uploads/HJGtF1FG1g.png =400x)
  
There are lot of critiques to this suggestive idea:

## Critique 1: non-computability

* Strictly speaking, there are problems that really can't be solved by sequential, step-by-step instructions. These are called [non-computable problems](https://www.geeksforgeeks.org/computable-and-non-computable-problems-in-toc/).
  * Most of them include problems where there is not a way to determine if the machine will stop running code while searching for a solution (ex.: the Halting Problem).
* A simple example: there is no algorithm to solve the [2048 game](https://drops.dagstuhl.de/storage/00lipics/lipics-vol049-fun2016/LIPIcs.FUN.2016.1/LIPIcs.FUN.2016.1.pdf), famous in 2014.
* However, problems like these can have their solution "approximated" with heuristics (exploring different options and seeing which is better). So one could argue that this is the way we think when trying to solve them... even though there is not an "analytical" way to solve.

## Critique 2: the Chinese Room experiment

* Proposed by philosopher John Searle in 1980.

![chineseroom](https://hackmd.io/_uploads/Sy32tJYfyl.gif)

* A person who does not understand Chinese sitting in a sealed room with a set of instructions in English for manipulating Chinese symbols. People outside the room slide Chinese symbols under the door, and the person inside follows a translation table to produce appropriate responses in Chinese, without actually comprehending the meaning of the symbols.

* Searle's argument is that even though the person inside the room can generate responses that seem intelligent to those outside, the person doesn't truly understand Chinese; they are just manipulating symbols according to a set of rules. Similarly, Searle argues that a computer program that processes symbols according to a set of rules (like many AI systems) may simulate intelligence but does not possess genuine understanding or consciousness.

## Critique 3: why are we really wanting that?

* Philosopher Hubert Dreyfus made a powerful critique to Classic AI in his 1972 book *"What Computers Can't Do"*.
* His argument is long and complex, and involves noticing that human intelligence is fundamentally embodied and situated in a certain narrative, and since an artificial device wouldn't have this, it cannot mimic completely how humans reason (we're exploring a bit of that in a moment).
* In any case, Dreyfus reflected on the quest for Artificial Intelligence as the quest of almost all Western science and philosophy (emphasis added):

> “Since the Greeks invented logic and geometry, the idea that all reasoning might be reduced to some kind of calculation so that all arguments could be settled once and for all has fascinated most of the Western tradition's rigorous thinkers. Socrates was the first to give voice to this vision. The story of artificial intelligence might well begin around 450B.C. when (according to Plato) Socrates demands of Euthyphro, a fellow Athenian who, in the name of piety, is about to turn in his own father for murder: "I want to know what is characteristic of piety which makes all actions pious ... that I may have it to turn to, and to use as a standard whereby to judge your actions and those of other men. Socrates is asking Euthyphro for what modern computer theorists would call an "effective procedure," "a set of rules which tells us, from moment to moment, precisely how to behave.

> “The belief that such a total formalization of knowledge must be possible soon came to dominate Western thought. It already expressed a basic moral and intellectual demand, and the success of physical science seemed to imply to sixteenth-century philosophers, as it still seems to suggest to thinkers such as Minsky, that the demand could be satisfied.”

* Another philosopher, Michael Polanyi, explored how knowledge can be **tacit** - in other words, it cannot be turned to explicit words and rules ([The Tacit Dimension](https://press.uchicago.edu/ucp/books/book/chicago/T/bo6035368.html)).
  - Sociologist Harry Collins, who we presented previously, has also argued about that in his books [The Shape of Actions](https://mitpress.mit.edu/9780262526524/the-shape-of-actions/) and [Artifictional Intelligence](https://www.wiley.com/en-us/Artifictional+Intelligence:+Against+Humanity's+Surrender+to+Computers-p-9781509504152). Algorithms still don't seem to share with us a form of life in order to make sense of the dynamic, situated and embodied character of human life.

Take home message: *algorithmic intelligence can correctly, and usefully, mimicry some human reasoning. We can be grateful for that. But this can be limited, and there are lots of reasons to believe that our intelligence goes beyond mere mathematical/logical rule-following. Therefore, so far, it seems we haven't yet seen an undebatable way to mimic human intelligence exhaustively*.

Calvin professor Ken Arnold's opinion: [Avoid Magical Thinking about AI](https://kenarnold.org/posts/magical-thinking/)