Connaissance : Types of Knowledge

1) Declarative Knowledge : the statements of fact.

2) Imperative Knowledge : recipies that can be applied to solve a problem.

Declarative Knowledge (definition)

Any statement or affirmation, that we estimate "as beeing desirable to be true"

  • it can be True
  • it can be False

...The Answer to the Ultimate Question of Life, The Universe, and Everything:

42


In [1]:
As_soon_as_I_become_a_good_programmer_I_will_be_rich=False
# Let us test it : 
if As_soon_as_I_become_a_good_programmer_I_will_be_rich: print("Then let us start programming !")
else: print('Do Algorithmics !')


Do Algorithmics !

1.1) Declarative Knowledge on Palindromas

"A palindroma is a word that can be spelled from both sides"

  • a palindroma is a character's string containing a chain of lenght n

  • "abc...cba" is a palindroma

  • "aba" is a palindroma

  • any word with two identical letters is a palindroma

1.2) Imperative Knowledge to check that a word is a Palindroma:

  1. The word must be declared as a variable that alows to chain letters (string)
  2. The lenght of the string must be known using a build in function len
  3. The string must be readed letter by letter from both sides untill we meet
  4. Check that both letters are identical, if yes then go to (3)
  5. If both ends have been reached conclude that that word is a "True" Palindroma

Pasta al dente Cooking Recipies:

  1. Start puting hot water on the pot add salt
  2. When $T$ of the water is greater than 100$°$ Celcius put 150mg of Pasta
  3. Otherwise check that the water is heating up properly and wait
  4. When the Pasta is in the cooker wait until it is al dente

In [2]:
## Check if the french word "ressasser" is a palindroma
# We first declare some variables
word_to_test='ressasser'
# We need to define the indexes in the chain of characters
index_head=0
index_tail=len(word_to_test)-1
# Now we are going to print
print(word_to_test)
print("The lenght of the word is: {}".format(len(word_to_test)))
print("The first letter is: {}".format(word_to_test[index_head]))
print("The last letter: {}".format(word_to_test[index_tail]))


ressasser
The lenght of the word is: 9
The first letter is: r
The last letter: r

In [3]:
if (word_to_test[index_head]==word_to_test[index_tail]):
    
    if index_head < index_tail: 
        print("Untill now it seems a palindroma")
        index_head=index_head+1
        index_tail=index_tail-1
        print(index_head)
        print(index_tail)
    else: print("The word '{}' is a palindroma".format(word_to_test))
else: print("The word '{}' is NOT a palindroma".format(word_to_test))


Untill now it seems a palindroma
1
7

2.1) Declarative Knowledge on square roots

"The square root $\displaystyle x=\sqrt{y}$, of a number $y$ is a number $x$ such as:

$\displaystyle x \times x = y $ "

2.2) Imperative Knowledge associated to solve square roots

Here is a Reciple for deductig the square root of a number $ y $, attributed to Heron of Alexandria in the first century AD

  1. Start with a guess for unknown $x$ called $g$
  2. If $g \times g$ is close enought to $y$ : stop and return $g$
  3. Otherwise make a new guess by averaging $g$ and ${y}/{g}$
  4. Using the new guess, repeat the process to point (2)

In [4]:
# Give a number and find the first guess
print('Enter the number y, from whom you are asked to find the square root')
y=eval(input('y = '))
print('Give a first guess for x, called g')
g=eval(input('g = '))
print("End of step 1")


Enter the number y, from whom you are asked to find the square root
y = 24
Give a first guess for x, called g
g = 5
End of step 1

In [5]:
print("(The square product of {0} is {1})".format(g,g*g))
print("The square of the average of the guess and y divided by guess is:")
print((0.5*(g+y/g))**2)


(The square product of 5 is 25)
The square of the average of the guess and y divided by guess is:
24.010000000000005

In [6]:
print("How accurate is this result ?")
print("Well, the square of your guess is {}".format(g*g))
if g*g == y:
    print("Whoah, it is a good guess")
    print("The solution is:")
    print((0.5*(g+y/g)))
else:
    g=0.5*(g+y/g)
    print("Sorry")
    print("Try again, with, g={} as new guess".format(g))
    print("The average of the new g and y divided by this new g is:")
    print(0.5*(g+y/g))


How accurate is this result ?
Well, the square of your guess is 25
Sorry
Try again, with, g=4.9 as new guess
The average of the new g and y divided by this new g is:
4.898979591836735

3) Conditional Instructions

-if

-else

-elif

3.1) What is a bissextile year


In [7]:
#Check a number
nb = eval(input('Enter a number = '))
if nb < 0:
    print('{} is a negative number'.format(nb))
elif nb > 0:
    print('{} is a positive number'.format(nb))
else:
    print('{} is a null'.format(nb))


Enter a number = -5
-5 is a negative number

In [8]:
# Leap years are back all 4 years, except secular years if these are not multiple of 400
n = eval(input('Enter a year = '))
if n % 4 != 0:
    print('Year {} is NOT a bissextile year'.format(n))
elif n % 100 != 0:
    print('Year {} is a bissextile year'.format(n))
elif n % 400 != 0:
    print('Year {} is NOT a bissextile year'.format(n))
else:
    print('Year {} is a bissextile year'.format(n))


Enter a year = 2021
Year 2021 is NOT a bissextile year

In [9]:
if ((n % 4 == 0) and (n % 100 != 0)) or (n % 400 == 0): print('Year {} is a bissextile year'.format(n))
else: print('Year {} is NOT a bissextile year'.format(n))


Year 2021 is NOT a bissextile year

4) Usual operators

Putting statements x and y in relations trough the operators is the basics of formal logic

4.1) Comparison operators

Sign Meaning
x == y x Is equal to y
x != y x Is Not equal y
x > y x Is greater than y
x < y x Is smaller than y
x >= y x Is greater or equal than y
x <= y x Is smaller or equal than y
-x opposite to x (when x a number)
+x positive to x (when x a number)
~x symmetric to x (when x a number)

4.2) Logical operators

relation between expressions meaning
(x) and (y) x and y are expressions SIMULTANEOUSLY both True
not (x) the negation of x is True
(x) or (y) at least x or y is True INDEPENDENTELY

In [10]:
a=6
print(~a)


-7

In [11]:
# There is an operator for defining an 'implication'
# p implies q means that 
# p is a sufficient condition for q to be true
# p --> q is equivalent as (not(p) or q)

Je_fais_de_longues_etudes=True
Je_deviens_bon=False

# Je_fais_de_longues_etudes --> Je_deviens_bon
# This can be writen as the following proposition :
(not(Je_fais_de_longues_etudes) or (Je_deviens_bon))


Out[11]:
False

4.3) Arithmetic operators

Number operations Arithmetics
y*x multiplication of y times x
y/x division of y by x (both numbers)
y//x floor division of y by x
y%x Modulus, returns the decimal part (remainder) of the quotient of y divided by x.

In [12]:
a=10
b=3
c=a // b
d=a % b
print(a)
print(type(a))
print(b)
print(type(b))
print(c)
print(type(c))
print(d)
print(type(d))


10
<class 'int'>
3
<class 'int'>
3
<class 'int'>
1
<class 'int'>

4.4) Operations on lists and characters

Character operation Signification
'y'*x repeat 'y' x times (when y a character)
x in y x belong to y (when y is a list)

In [13]:
'y'*3


Out[13]:
'yyy'

In [14]:
a='Je ne dois pas consulter Facebook en cours' + '//'
a*5


Out[14]:
'Je ne dois pas consulter Facebook en cours//Je ne dois pas consulter Facebook en cours//Je ne dois pas consulter Facebook en cours//Je ne dois pas consulter Facebook en cours//Je ne dois pas consulter Facebook en cours//'

In [15]:
a = 5
b = 20
(a < b)


Out[15]:
True