a un point décimal a est l'identifiant de la variable (attention à ne pas utiliser le mots réservés comme identifiant), = est l'opérateur d'affectation
Pour obtenir plus de décimales que la précision standard de Python, il faut utiliser le module decimal qui permet de travailler avec une précision arbitraire
2 jui 2019 · Python also uses a “round half to even” method Analysts in ISD often have to round numbers to a smaller number of decimal places
Le module decimal permet d'effectuer des calculs exacts sur les nombres décimaux, dans les limites d'une précision fixée par l'utilisateur (mais par défaut
de Python comme support à l'apprentissage de la programmation en lycée général et On peut contourner le problème à l'aide de la bibliothèque decimal
float (floating point real values) : or floats, represent real numbers and are written with a decimal point dividing the integer and fractional parts Floats
comparativement à d'autres langages, le Python est assez proche d'un Le module decimal « est basé sur un modèle en virgule flottante conçu pour les
Python stores 53 bits for the mantissa of each floating point number, so this series is truncated The truncated series, converted back to decimal is:
To be able to use the Python math library A numeric literal without a decimal point produces an intvalue us around the limitations of ints?
19200_6Chapter03.pdf ! " # $ % #$ & % # $& &$ % ! "'%( #& & % # & % #& &% ) *# # $ & $ % # $$ + $ , '-).( & - ""$ & '%-%/%/-%/( &$ - *# & &$ 0 $$ &, 1 &$ & $$ % #$! "& " $ % . *# 1& ' $ (% #"& " "& % 2 *# "$ & $& ' $&("&% 3&& 4 & & & "& & & & $&'" $ $ & /( 5 *# &$ && $"&% >>> type (3)
>>> type (3.1) >>> type(3.0) >>> myint= -32 >>> type(myint) >>> myfloat= 32.0 >>> type(myfloat) >>> mystery = myint* myfloat >>> type(mystery) 6 *# 1 4 7& "$ & '""½0 ( 8 && " $$ #$& & 9 & , $& "9 " &, / *# $& $&% >>> 3.0+4.0 7.0 >>> 3+4 7 >>> 3.0*4.0 12.0 >>> 3*4 12 >>> 10.0/3.0 3.3333333333333335
>>> 10/3 3 >>> 10%3 1 >>> abs(5) 5 >>> abs(-3.5) 3.5 *# " $ & &% #"/:, # +$ ' ; " /: ; ' (/ ' $( /<: $ " $/% *# + 6%/-%/ 6-
& = " , :'('(>'<( ? 8@ A '>BCCC<( "&$ $ " && & % & & $& $ $ % ) ? 8@ @" $0 0 , #& $ "+ $ 0 ... " & , 24
2 bbacxa -±-= - ? 8@ #& + & & +" $ $ " && % . ? 8@ #0 & %0 '9(% ? && 0 $ $ & &% #&& D:%0 'C-)CC( 2 ? 8@ # quadratic.py # A program that computes the real roots of a quadratic equation. # Illustrates use of the math library. # Note: This program crashes if the equation has no real roots. import math # Makes the math library available. def main(): print "This program finds the real solutions to a quadratic" print a, b, c = input("Please enter the coefficients (a, b, c): ") discRoot= math.sqrt(b* b -4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b -discRoot) / (2 * a) print print "The solutions are:", root1, root2 main() 5 ? 8@ This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 3, 4, -1
The solutions are: 0.215250437022 -1.54858377035
1 4 This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 1, 2, 3
Traceback(most recent call last):
File "", line 1, in -toplevel-
main() File "C:\Documents and Settings\Terry\My Documents\Teaching\W04\CS 120\Textbook\code\chapter3\quadratic.py", line 14, in main
discRoot= math.sqrt(b* b -4 * a * c) ValueError: math domain error
>>> ) 6 8@
$::: +0 $ " , ? 0 $ $$ CC%3&CC &&0 4 / & D& = & & $ " &%3 9&4
2/BB2/ $ &$. ' " .,( = & $ ,:'B('B(...'( .,:.C-C)CCC:2/ & D& = & 3& 4 +$ &$ $ &$$ $ & D& = & 3 &&.,4 .C-:/ #+//C):/ #+//C:./ #+././C:2/ #+2/2/C:2/ & D& = & 1" && 4 1" & & " + +$ % # & +& " & & " &+ & " &% ) & D& = & # &$ $& & &+& + & ;& " & @ &$ & & "&$& " & - - & D& = & &+& +"&&&, fact = 1 for factor in [6, 5, 4, 3, 2, 1]: fact = fact * factor @" " $ +, . & D& = & 1 & ;$4 # & ... E & " "&$$ &&9 "&$$%A & ; +$ &&""&$ % $$ "& 4 2 & D& = & & & " " fact = 1 for factor in [2, 3, 4, 5, 6]: fact = fact * factor F ,A $$ $ &$ 44 5 & D& = & 1 '( 4 /...B & , ' ( >...B A ,# " , ' ( >...B 6 & D& = & @" 9&, >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5,10) [5, 6, 7, 8, 9] >>> range(5,10,2) [5, 7, 9] / & D& = & ? B $ & & $$ % 1$ range(2, n+1) '1 ">4( 1$ range(n, 1, -1) . & D& = & &$ & # factorial.py # Program to compute the factorial of a number # Illustrates for loop with an accumulator def main(): n = input("Please enter a whole number: ") fact = 1 for factor in range(n,1,-1): fact = fact * factor print "The factorial of", n, "is", fact main() #@ $ 1 //,4 >>> main() Please enter a whole number: 100
The factorial of 100 is
93326215443944152681699238856266700490715968264
38162146859296389521759999322991560894146397615
65182862536979208272237582511852109168640000000
00000000000000000
1,#" , #@ $ " $& ... Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] onwin32 Copyright 1991-1995 StichtingMathematischCentrum, Amsterdam >>> import fact >>> fact.main() Please enter a whole number: 13
13 12 11 10 9 8 7 6 5 4 Traceback(innermost last):
File "", line 1, in ?
fact.main() File "C:\PROGRA~1\PYTHON~1.2\fact.py", line 5, in main fact=fact*factor OverflowError: integer multiplication
) #@ $ 1" 4 1 & $ $ $ $ % # $ & ? "&%# & % - #@ $ # & # & "& /% # -B%1 $ $ /% 1 &" $ % . #@ $ Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32 Copyright 1991-1995 StichtingMathematischCentrum, Amsterdam >>> 2**30 1073741824
>>> 2**31 Traceback(innermost last):
File "", line 1, in ?
2**31 OverflowError: integer pow()
>>> 2 2 #@ $ &/ 9%&& B4 >>> 2**31-1 Traceback(innermost last):
File "", line 1, in ?
2**31-1
OverflowError: integer pow()
14 "& $ , 5 #@ $ 1 &" , :/>/ B:/B>/ 1" $ , >>> 2**30-1+2**30 2147483647
>>> 2147483647+1 Traceback(innermost last):
File "", line 1, in ?
2147483647+1
OverflowError: integer addition
>>> 6 #@ $ 1"& 4 #& "& )2)5.)2 3 " $& 4 Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> 2**40 1099511627776L
)/ 3& @ @ * $& & $ 4 $ & ;& %/ >>> main() Please enter a whole number: 15
The factorial of 15 is 1.307674368e+012
1& 9 , ) 3& @ @ 7 & " && 9 $ 9 & % %/2.2).5>/%/2.2).5C / 3 &" && & &6 $ "&% ) 3& @ @ =& 9 =&&& & $"& & % & & , @ $ 9 ; 9&" "& &% 5 ) 3& @ @ # & "@"$ & &% - $$ " -@ & $$ " >>> 2L 2L >>> 2L**31 2147483648L
>>> type(2L) >>> 100000000000000000000000000000000000L + 25 100000000000000000000000000000000025L
)) 3& @ @ && "&" & & &% " $ && " & & " $&% >>> x = 2147483647 >>> x = x + 1 >>> x 2147483648L
>>> type (x) >>> print x 2147483648
)- 3& @ @ 1 9 & "&% 9 && & % 1 & '@" 1& && 4- @$$ & ). #" 1+ $& $& $&% 1 9 $& 9 4 9:-%/
1 +&4 )2 #" = "& 9 " -%/- " " %/ $& " % " $& &&& $ " $& "%/" )5 #" 9B9
&& " $&% & " %# && 9& % " : $ " )- .2 ) --%-, 6 )6 #" #$ 9 &&& $"&$& " :$&'( 1&" && && " % -/ #" 1" +4 " :$&'( ::-:) $&'(:)%/, & " '(& '( $ " & % - #" >>> float(22/5) 4.0 >>> int(4.5) 4 >>> int(3.9) 3 >>> long(3.9) 3L >>> float(int(3.9)) 3.0 >>> int(float(3.9)) 3 >>> int(float(3)) 3 - #" # $ $& & % >>> round(3.9) 4.0 >>> round(3) 3.0 >>> int(round(3.9)) 4