Review together what has been learned so far, and compare with the lecture’objectives
Clarify Python syntax
Consolidate some practical skills and some notions of Python with simple exercices
At the end, you should be able to write simple Python scripts to:
- compute basic statistics from files containaing numeric data
- compute the number of occurrences of words in a text file
- play the "guess a number" game
Then, you should be able profit more from the other Ateliers (Atelier d’Experimentation Humaine, Atelier de Statistiques, …)
Programming can be fun!
(we are doing some silly exercices, but this is like the first lessons with an instrument)
We have already covered most of the points (although superfically):
Exercice
AIP2015/resources/python-scripts/human-guess-a-number.py
dualscope.py
in the same directory.human-guess-a-number.py
with the editor atom, change the maximum value from 100 to 1000, and rerun it.We have hardly mentioned Python’s syntax yet (because it is boring…)
Let us go to the lecture 04_Python_in_a_nutshell
A peculiarity of Python: the use of indentation do delimit blocs of instructions.
Let us check the script AIP2015/resources/python-scripts/human-guess-a-number.py and verify that it comprises only the elements just described.
. . .
350 * 1.17
You can use Python’s command line as a calculator.
. . .
if (2 + 2) == 5:
print("Sorry?")
else:
print("Bonjour!")
. . .
Answer: It prints ‘Bonjour!’
. . .
while True:
print('!')
. . .
Answer: It prints ‘!’ forever. Press Ctrl-C to interrupt it. Or use the task manager of your operating system.
answer = ""
while answer != 'You are the best!':
answer = raw_input('Who is the best?')
print('Sure!'
Run it!
. . .
for x in ('a', 'b', 'c', 'd', 'e'):
print x=='c'
. . .
Run it!
Remark: strings are sequences, therefore the following code works in the same way:
for x in 'abcde':
print x=='c'
. . .
s = ""
for _ in range(80):
s = s + "#"
print(s)
Remark: the operator ’’ on strings can also be used “#” 80
. . .
reg0 = 10
reg1 = 5
while reg0 > 0:
reg0 = reg0 - 1
reg1 = reg1 + 1
print(reg0)
print(reg1)
. . .
Remember the register machine rodrego?
. . .
print "2 * 1 = 2",
print "2 * 2 = 4"
print "2 * 3 = 6"
print "2 * 4 = 8"
...
print "2 * 10= 20"
. . .
for i in range(1, 11):
print "2 * " + str(i) + " = " + str(2*i)
. . .
for a in range(1, 11):
print('---')
for b in range(1, 11):
print str(a) + ' * ' + str(b) + ' = ' + str(a*b)
import turtle
def square(n):
turtle.fd(n)
turtle.right(90)
turtle.fd(n)
turtle.right(90)
turtle.fd(n)
turtle.right(90)
turtle.fd(n)
turtle.right(90)
square(50)
a = raw_input('stop?')
Define the following function and execute it
def spirale():
for n in (50, 60, 70, 80, 90, 100):
square(n)
turtle.left(30)
How would you improve this code?
nombres = []
while True:
answer = raw_input('data point (Press just Enter to finish)?')
if answer == '':
break
nombres.append(float(answer))
sum, sumsq = 0.0, 0.0
for x in nombres:
sum = sum + x
sumsq = sum + x*x
print(sum/len(nombres))
print(sumsq/len(nombres))
. . .
def f(x, y):
return 0.5 * ( x + (y / x))
Compute f(10, 2), f(f(10,2),2), f(f(f(10,2),2),2), and so on.
Compute f(10, 9), f(f(10,9),9), f(f(f(10,9),9),9) and so on…
Compute f(10, 16), f(f(10,16),16), f(f(10,16),16,16),…
Computer f(10, 25), f(f(10,25),25), and so on…
Can you form a conjecture on what happens?
. . .