码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础

时间:2020-03-30 23:10:12      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:turn   local   ase   return   The   ide   sid   turned   res   

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基础

标签:turn   local   ase   return   The   ide   sid   turned   res   

原文地址:https://www.cnblogs.com/noralee/p/12601885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!