Home  >  Article  >  Backend Development  >  The difference between eval and int in python

The difference between eval and int in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-24 14:51:1714465browse

The difference between eval and int in python

What is the difference between eval and int in python? Let me introduce it to you below:

1.eval() function

eval(f7e83be87db5cd2d9a8a0b8117b38cd4) can be used as a Python expression The method parses and executes the string and returns the result output. The eval() function will remove the two quotes from the string and interpret it as a variable.

Function:
a. Processing numbers

Single quotes, double quotes, and the eval() function interpret them as int type; triple quotes are interpreted as str type.

b. Processing string type strings

For strings (non-numbers) in eval() brackets, if the string is in single quotes Or double quotes will cause NameError, because the eval() function will remove the two quotes when processing the string. Correctly, you should use a triple quote consisting of a single quote and a double quote to enclose the string.

Related recommendations: "Python Video Tutorial"

2.int() function

The int() function can Convert a number to an integer

>>> int('12',16) 
18

There are two things to note here:

1) 12 must be entered in the form of a string, if it is with the parameter base

2) This is not about converting 12 into a hexadecimal number, but that 12 is a hexadecimal number, and the int() function represents it as a decimal number, as follows

>>> int('0xa',16) 
10 
>>> int('10',8) 
8

The above is the detailed content of The difference between eval and int in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn