The eval function and the exec function are one of the built-in functions. The character string given as an argument can be executed as an expression or statement.
Executes the expression entered as a character string in the argument. The execution result is returned as a return value.
ʻEval ("expression") ` └ Error if it is a sentence └ Abbreviation for evaluation
python
a = eval("5*2")
print(a)
#output
10
python
eval("print(5*2)")
print(eval("5*2"))
#output
10
10
String assignment
l=[]
s=".extend('abc')"
#Strings do not require quotation marks
eval("l"+s)
print(l)
#output
['a', 'b', 'c']
Quotation marks are not required when you put a string variable in the argument of eval **.
When using a character string, use a symbol different from the surrounding symbol. ·"" in:"' '" ·"' '"in:"" ""
Use strings in eval
a = eval("'aaa'")
b = eval('"bbb"')
print(a)
print(b)
#output
aaa
bbb
Same symbol is error
eval(""aaa"")
#output
SyntaxError: invalid syntax
--When you enter a sentence --For non-character strings (numerical values, etc.)
If you enter a sentence
eval("a = 5*2")
#output
SyntaxError: invalid syntax
Other than character strings
eval(5*2)
#output
TypeError: eval() arg 1 must be a string, bytes or code object
Other than character string (key name specified)
eval(a=5)
#output
TypeError: eval() takes no keyword arguments
Executes the expression or statement of the character string entered in the argument.
ʻExec ('statement or expression')` └ Abbreviation for execute └ There is no return value for exec itself
Enter a statement as an argument
exec("a=5*2")
print(a)
#output
10
Use strings
exec("a = 'hello'")
print(a)
#output
hello
Multiple sentences
exec("a=2; b=5; c=a*b")
print(c)
#output
10
・ ";" Agrees with line feed
Enter an expression as an argument
l=[1,2,3]
exec("l.extend([4,5,6])")
print(l)
#output
[1, 2, 3, 4, 5, 6]
** (2) exec has no return ** └ exec can also take an expression as an argument like eval. └ exec itself exists (returns with return) └ eval itself does not exist (None)
exec itself has no value
exec("2+5") is None #True
eval("2+5") is None #False
Illustration
#Expressions in exec can be executed
exec("print(2*5)")
#exec itself has no return value(None)
print(exec("2*5"))
#output
10
None
'''Code (with line breaks)'''
└ Surround with 3 quotation marks
└ If you start a new line immediately after''', the output will also be broken.
python
a='''
Hello,
It is a good weather today.
How is the weather there?
'''
print(type(a))
print(a)
#output
<class 'str'>
Hello,
It is a good weather today.
How is the weather there?
You can use backslashes to escape line breaks.
Line break escape
a='''\
Hello,
It is a good weather today.\
How is the weather there?
'''
print(a)
#output
Hello,
It is a good weather today. How is the weather there?
python
a ='''
#Function definition
def hello_func(name):
print(f'Hello{name}Mr.')
#Function execution
name="Todoroki"
hello_func(name)
'''
exec(a)
#output
Hello Todoroki's
Because you can't pass arguments to a string function in an exec Described in triple quotes until function execution.
Recommended Posts