Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset
Comments Off

Python: Evaluer une expression avec la fonction ‘eval()’

 list, list
{filelink=16896}

def test(expression):
    result = eval(expression)
    print expression, "=>", result, type(result)
 
test("1")
test("[14, 8]")
test("1.0")
test("[14,]")
test("'string'")
test("1.0 + 2.0")
test("'*' * 10")
test("len('mesexemples.com')")
 
"""
1 => 1 <type 'int'>
[14, 8] => [14, 8] <type 'list'>
1.0 => 1.0 <type 'float'>
[14,] => [14] <type 'list'>
'string' => string <type 'str'>
1.0 + 2.0 => 3.0 <type 'float'>
'*' * 10 => ********** <type 'str'>
len('mesexemples.com') => 15 <type 'int'>
"""

Livres Sur ce Sujet

----------------------------------------------------------------------------

Comments are closed.