Python has a well-stocked box of programming tools, including a large number of functions and modules that are ready-made for you to use.

As you learned in previous article, modules need to be imported before they can be used. Python’s built-in functions don’t need to be imported first; they’re available as soon as the Python shell starts.

The abs Function

The abs function returns the absolute value of a number, which is the value of a number without its sign. For example, the absolute value of 10 is 10, and the absolute value of −10 is 10.To use the abs function, simply call it with a number or variable as its parameter, like this:

You might use the abs function to do something like calculate an absolute amount of movement of a character in a game, no matter in which the direction that character is traveling. For example, say the character takes three steps to his right (positive 3) and then ten steps to his left (negative 10, or −10). If we didn’t care about the direction (positive or negative)

The bool Function

The name bool is short for Boolean, the word programmers use to describe a type of data that can have one of two possible values, usually either true or false.The bool function takes a single parameter and returns either True or False based on its value. When using bool for numbers, 0 returns False, while any other number returns True. Here’s how you might use bool with various numbers:

The eval Function

The eval function (short for evaluate) takes a string as a parameter and runs it as though it were a Python expression. For example, eval(‘print(“wow”)’) will actually run the statement print(“wow”).The eval function works only with simple expressions, such as the following:

The exec Function

The exec function is like eval, except that you can use it to run more complicated programs. The difference between the two is that eval returns a value (something that you can save in a variable), whereas exec does not. Here’s an example:

In the first two lines, we create a variable with a multi line string containing two print statements, and then use exec to run the string.

The float Function

The float function converts a string or a number into a floating point number, which is a number with a decimal place (also called a real number). For example, the number 10 is an integer (also called a whole number), but 10.0, 10.1, and 10.253 are all floating point numbers (also called floats).You can convert a string to a float simply by calling float, like this:

The int Function

The int function converts a string or a number into a whole number (or integer), which basically means that everything after the decimal point is dropped. For example, here’s how to convert a floating-point number into a plain integer:

The len Function

The len function returns the length of an object or, in the case of a string, the number of characters in the string. For example, to get the length of this is a test string, you would do this:

Leave a Reply

Your email address will not be published. Required fields are marked *