[∇] [@] Introduction à Python 3

Version 2022-10-25

Licence cc-by-sa ModLibre.info sauf autres indications

NB : Copiez les instructions que vous voulez tester dans une version récente de Python 3



[∇] [Δ] Programmes OLPC traduits en Python 3

Le projet OLPC (One Laptop per Child) a été lancé en 2005 pour développer « un ordinateur portable pour chaque enfant ».

Ce projet a suivi l’évolution des technologies et a permis de créer des logiciels éducatifs libres utilisables sur les portables et les tablettes modernes.

L’essentiel de ces objectifs sont repris par la plateforme Sugarizer.

La plupart des applications OLPC originales avaient été développées en Python 2 et sont disponibles sur le Wiki d’OLPC.

On trouvera ci-dessous une sélection de ces applications traduites en Python 3.



[∇] [Δ] Liste des programmes OLPC traduits en Python 3

●      [∇] Multiplication

●      [∇] Suite de Fibonacci

●      [∇] Triangle de Pascal

●      [∇] Devine un nombre

●      [∇] Gymnastique



[∇] [Δ] [@] Maths

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Madeleine Ball
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Apples (-> Pommes)
# Author:         Madeleine Ball
# About:          Adding and dividing
# Shows:          Print Statements and Basic Math
# XOversion:  --> Bundled in 656 ?

print ("Let's do math!")
print ("On Monday I picked 22 apples. On Tuesday I picked 12.")
print ("Now I have: ", 22 + 12)

print ("My brother says he picked twice as many apples last week.")
print ("This means he picked: ", (22 + 12) * 2)

print ("I have 3 friends to whom I would like to give apples.")
print ("One third of my apples is about: ", (22 + 12) // 3)
print ("Or, more exactly: ", (22.0 + 12.0) / 3.0)

# Initial version in Python 2 ##########################################
# Title:          Apples
# Author:         Rafael Ortiz
# About:          The Fibonacci Number Series
# Shows:          Using tuple assignments. While loop.
# XOversion:      Bundled in 656
#
# Author:         Madeleine Ball
# About:          Adding and dividing
# Shows:          Print Statements and Basic Math
# XOversion:      Bundled in 656
#
#   print "Let's do math!"
#   print "On Monday I picked 22 apples. On Tuesday I picked 12."
#   print "Now I have: ", 22 + 12
#
#   print "My brother says he picked twice as many apples last week."
#   print "This means he picked: ", (22 + 12) * 2
#
#   print "I have 3 friends to whom I would like to give apples."
#   print "One third of my apples is about: ", (22 + 12) // 3
#   print "Or, more exactly: ", (22.0 + 12.0) / 3.0```
Let's do math!
On Monday I picked 22 apples. On Tuesday I picked 12.
Now I have: 34

My brother says he picked twice as many apples last week.
This means he picked: 68

I have 3 friends to whom I would like to give apples.
One third of my apples is about: 11
Or, more exactly: 11.333333



[∇] [Δ] [@] Multiplication

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Chris Ball
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Times2
# Author:         Chris Ball
# About:          Print any times table
# Shows:          Loops, range, and input
# XOversion:  --> Bundled in 656 ?

number = int(input("Which times table? "))
for i in range(1,13):
    print(i, "x", number, "=", i*number)

# Initial version in Python 2 ##########################################
# Title:          Times2
# Author:         Chris Ball
# About:          Print any times table
# Shows:          Loops, range, and input
# XOversion:      Bundled in 656
#
#   number = input("Which times table? ")
#   for i in range(1,13):
#       print i, "x", number, "=", i*number
Which times table? 7
1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35
6 x 7 = 42
7 x 7 = 49
8 x 7 = 56
9 x 7 = 63
10 x 7 = 70
11 x 7 = 77
12 x 7 = 84



[∇] [Δ] [@] Suite de Fibonacci

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Rafael Ortiz
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Fibonacci Series
# Author:         Rafael Ortiz
# About:          The Fibonacci Number Series
# Shows:          Using tuple assignments. While loop
# XOversion:  --> Bundled in 656 ?

a, b = 0, 1
while b < 1001:
    print (b, end=" ")  
    a, b = b, a+b

# Initial version in Python 2 ##########################################
# Title:          Fibonacci Series
# Author:         Rafael Ortiz
# About:          The Fibonacci Number Series
# Shows:          Using tuple assignments. While loop.
# XOversion:      Bundled in 656
#
#   a, b = 0, 1
#   while b < 1001:
#       print b,
#       a, b = b, a+b```
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987



[∇] [Δ] [@] Pythagore

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Rafael Ortiz
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Pythagoras
# Author:         Rafael Ortiz
# About:          Uses Pythagorean Theorem to compute longest edge of a triangle
# Shows:          Import statement, sqrt (square root) function.

import math
from math import sqrt

print("This is the Pythagoras Theorem")
a = float(input("Type a = "))
b = float(input("Type b = "))

c = sqrt((a*a)+(b*b))

print("c = ",c)

# Initial version in Python 2 ##########################################
# Title:          Pythagoras
# Author:         Rafael Ortiz
# About:          Uses Pythagorean Theorem to compute longest edge of a triangle
# Shows:          Import statement, sqrt (square root) function.
#
#   import math
#   from math import sqrt
#
#   print "This is the Pythagoras Theorem"
#   a=float(raw_input("Type a ="))
#   b=float(raw_input("Type b ="))
#
#   c=sqrt((a*a)+(b*b))
#
#   print "c =",c
This is the Pythagoras Theorem
Type a = 3
Type b = 4
c =  5.0



[∇] [Δ] [@] Triangle de Pascal

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Madeleine Ball
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Pascal's triangle
# Author:         Madeleine Ball
# About:          Character graphic of Pascal's triangle
# Shows:          loops, vectors
# XOversion:  --> Bundled in 656 ?

# Pascal's triangle

lines = 8
vector = [1]

for i in range(0,lines):
  vector.insert(0,0)
  vector.append(0)

for i in range(0,lines):
  newvector = vector[:]
  for j in range(0,len(vector)-1):
    if newvector[j] == 0:
      print ("  ", end=""),
    else:
      print ("%2d" % newvector[j], end="")
    newvector[j] = vector[j-1] + vector[j+1]
  print(" ")
  vector = newvector[:]

# Initial version in Python 2 ##########################################
# Title:          Pascal's triangle
# Author:         Madeleine Ball
# About:          Character graphic of Pascal's triangle
# Shows:          loops, vectors
# XOversion:      Bundled in 656
#
#   lines = 8
#   vector = [1]
#
#   for i in range(0,lines):
#     vector.insert(0,0)
#     vector.append(0)
#
#   for i in range(0,lines):
#     newvector = vector[:]
#     for j in range(0,len(vector)-1):
#       if newvector[j] == 0:
#         print "  ",
#       else:
#         print "%2d" % newvector[j],
#       newvector[j] = vector[j-1] + vector[j+1]
#     print
#     vector = newvector[:]
                 1               
               1   1             
             1   2   1           
           1   3   3   1         
         1   4   6   4   1       
       1   5  10  10   5   1     
     1   6  15  20  15   6   1   
   1   7  21  35  35  21   7   1 



[∇] [Δ] [@] Triangle de Sierpinsky

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Madeleine Ball
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Sierpinsky triangle
# Author:         Madeleine Ball
# About:          Character graphics of a Sierpinski triangle
# Shows:          Modifying Pascal's triangle program, loops, vectors
# XOversion:  --> Bundled in 656 ?

size = 5
modulus = 2

lines = modulus**size

vector = [1]
for i in range(0,lines):
  vector.insert(0,0)
  vector.append(0)

for i in range(0,lines):
  newvector = vector[:]
  for j in range(0,len(vector)-1):
    if newvector[j] == 0:
      print(" ", end="")
    else:
      remainder = newvector[j] % modulus
      if remainder == 0:
        print("O", end="")
      else:
        print(".", end="")
    newvector[j] = vector[j-1] + vector[j+1]
  print(" ")
  vector = newvector[:]

# Initial version in Python 2 ##########################################
# Title:          Sierpinski triangle
# Author:         Madeleine Ball
# About:          Character graphics of a Sierpinski triangle
# Shows:          Modifying Pascal's triangle program, loops, vectors
# XOversion:      Bundled in 656
#
#   size = 5
#   modulus = 2
#
#   lines = modulus**size
#
#   vector = [1]
#   for i in range(0,lines):
#     vector.insert(0,0)
#     vector.append(0)
#
#   for i in range(0,lines):
#     newvector = vector[:]
#     for j in range(0,len(vector)-1):
#       if newvector[j] == 0:
#     print " ",
#     else:
#       remainder = newvector[j] % modulus
#       if remainder == 0:
#         print "O",
#       else:
#         print ".",
#     newvector[j] = vector[j-1] + vector[j+1]
#   print
#   vector = newvector[:]
                                .                                
                               . .                               
                              . O .                              
                             . . . .                             
                            . O O O .                            
                           . . O O . .                           
                          . O . O . O .                          
                         . . . . . . . .                         
                        . O O O O O O O .                        
                       . . O O O O O O . .                       
                      . O . O O O O O . O .                      
                     . . . . O O O O . . . .                     
                    . O O O . O O O . O O O .                    
                   . . O O . . O O . . O O . .                   
                  . O . O . O . O . O . O . O .                  
                 . . . . . . . . . . . . . . . .                 
                . O O O O O O O O O O O O O O O .                
               . . O O O O O O O O O O O O O O . .               
              . O . O O O O O O O O O O O O O . O .              
             . . . . O O O O O O O O O O O O . . . .             
            . O O O . O O O O O O O O O O O . O O O .            
           . . O O . . O O O O O O O O O O . . O O . .           
          . O . O . O . O O O O O O O O O . O . O . O .          
         . . . . . . . . O O O O O O O O . . . . . . . .         
        . O O O O O O O . O O O O O O O . O O O O O O O .        
       . . O O O O O O . . O O O O O O . . O O O O O O . .       
      . O . O O O O O . O . O O O O O . O . O O O O O . O .      
     . . . . O O O O . . . . O O O O . . . . O O O O . . . .     
    . O O O . O O O . O O O . O O O . O O O . O O O . O O O .    
   . . O O . . O O . . O O . . O O . . O O . . O O . . O O . .   
  . O . O . O . O . O . O . O . O . O . O . O . O . O . O . O .  
 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 



[∇] [Δ] [@] Devine un nombre

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   Pilar Saenz
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-09-29 Jean.Thiery@ModLibre.info
#
# Title:          Guess a number
# Author:         Pilar Saenz
# About:          Numerical tests
# Shows:          Tests, While loop
# XOversion:  --> Bundled in 656 ?

import random
from random import randrange
R = randrange(1,100)

print(            "Guess a number between  1 and 100 !!!")
print(            "Trouver un nombre entre 1 et  100 !!!")
N = int(input(    "Enter a number        | Entrez un nombre : "))
i=1
while N!=R:
    if N>R :
        print(    "Too big...  try again | Trop grand... Essayez de nouveau")
    else :
        print(    "Too small.. try again | Trop petit... Essayez de nouveau")
    N = int(input("Enter a number        | Entrez un nombre : "))   
    i+=1
print("You got it in         ", i, "tries")
print("Vous l'avez obtenu en ", i, "essais")

# Initial version in Python 2 ##########################################
# Title:           Guess a number
# Author:          Pilar Saenz
# About:           Numerical tests
# Shows:           Tests, While loop
# XOversion:       Bundled in 656

#   import random
#   from random import randrange
#   R = randrange(1,100)
#
#   print "Guess a number between 1 and 100!!!"
#   N = int(raw_input("Enter a number: "))
#   i=1
#   while N!=R:
#       if N>R :
#           print "Too big... try again"
#       else :
#           print "Too small.. try again"
#       N = input("Enter a number: ")
#       i+=1
#   print "You got it in ", i, "tries"
Guess a number between  1 and 100 !!!
Trouver un nombre entre 1 et  100 !!!
Enter a number        | Entrez un nombre : 50
Too small.. try again | Trop petit... Essayez de nouveau
Enter a number        | Entrez un nombre : 75
Too small.. try again | Trop petit... Essayez de nouveau
Enter a number        | Entrez un nombre : 87
Too big...  try again | Trop grand... Essayez de nouveau
Enter a number        | Entrez un nombre : 81
Too big...  try again | Trop grand... Essayez de nouveau
Enter a number        | Entrez un nombre : 78
Too small.. try again | Trop petit... Essayez de nouveau
Enter a number        | Entrez un nombre : 79
You got it in           6 tries
Vous l'avez obtenu en   6 essais



[∇] [Δ] [@] Gymnastique

# Copyright (C)   2007-2022  Creative Commons Attribution 2.5
#                            https://wiki.laptop.org/go/Pippy
# Initial version Python 2   C. Scott Ananian
# Translated into Python 3   Jean.Thiery@ModLibre.info
# Version         2022-10-01 Jean.Thiery@ModLibre.info
#
# Title:          jumping man
# Author:         C. Scott Ananian
# About:          Display
# Shows:          Print Statements
# XOversion:  --> Bundled in 656 ?

# both of these functions should be in the 'basic' package or some such
def clear_scr():
    print ('\x1B[H\x1B[J') # clear screen, the hard way.

def wait():
    import time
    time.sleep(0.1)
    
# jumping man!
# was having to escape the backslash which was rather unfortunate, 
# now using python's r" strings which were meant for regex's
# i didn't have to do that in C64 BASIC
for i in range(50):
    clear_scr()
    print ("\o/")
    print ("_|_")
    print ("   ")
    wait()

    clear_scr()
    print ("_o_")
    print (" | ")
    print ("/ \ ")
    wait()

    clear_scr()
    print (" o ")
    print ("/|\ ")
    print ("| |")
    wait()

    clear_scr()
    print ("_o_")
    print (" | ")
    print ("/ \ ")
    wait()

# Initial version in Python 2 ##########################################
# Title:          jumping man
# Author:         C. Scott Ananian
# About:          Display
# Shows:          Print Statements
# XOversion:      Bundled in 656
#
# both of these functions should be in the 'basic' package or some such
#   def clear_scr():
#       print '\x1B[H\x1B[J' # clear screen, the hard way.
#   def wait():
#       import time
#       time.sleep(0.1)
#
# jumping man!
# was having to escape the backslash which was rather unfortunate, 
# now using python's r" strings which were meant for regex's
# i didn't have to do that in C64 BASIC
#   for i in xrange(50):
#   for i in xrange(50):
#       clear_scr()
#       print r"\o/"
#       print r"_|_"
#       print r"   "
#       wait()
#
#       clear_scr()
#       print r"_o_"
#       print r" | "
#       print r"/ \ "
#       wait()
#
#       clear_scr()
#       print r" o "
#       print r"/|\ "
#       print r"| |"
#       wait()
#    
#       clear_scr()
#       print r"_o_"
#       print r" | "
#       print r"/ \ "
#       wait()
\o/      _o_       o       _o_      \o/      _o_       o       _o_
_|_       |       /|\       |       _|_       |       /|\       |    
         / \      | |      / \               / \      | |      / \   ...



[@] [Δ] Fin