Python基础
Posted noralee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础相关的知识,希望对你有一定的参考价值。
1. eval() : the eval function evaluates the “String” like a python expression and returns the result as an integer
Syntax: eval(expression, [globals[, locals]])
The arguments or parameters of eval function are strings, also optionally global and locals can be used as an argument inside eval function, but the globals must be represented as a dictionary and the locals as a mapped object.
Difference between the input() and eval(): input() takes the user input, but when the user enters an integer as an input the input function returns a string, but in the case of eval it will evaluate the returned value from a string to an integer. E.g:
input = input("Enter any number of your choice:")
print(input)
print(type(input))
--------------------------------------------------------------------Enter any number of your choice: 10 + 10
10 + 10
<class ‘str‘>
eval = eval(input("Enter any number of your choice"))
print(eval)
print(type(eval))
--------------------------------------------------------------------Enter any number of your choice: 10 + 10
20
<class ‘int‘>
闷了请做题
以上是关于Python基础的主要内容,如果未能解决你的问题,请参考以下文章