"28" python 12 common built-in functions

Source: Internet
Author: User

1.abs function
ABS (x,/)
Return the absolute value of the argument.
The ABS function returns the absolute value of a number, which is the minus sign of the number.

>>>print(abs(1))>>>print(abs(-10))

Role Example: You can imagine the movement of a game character. Forward and backward, forward means positive, backward means negative. If you don't care about the positive or negative direction, the number of steps that move is absolute.

>>>steps=-2>>>If abs(steps) >0  print("moving")

If there is no ABS function

>>>steps=-2>>>If steps<0 or steps >0:  ###steps !=0  print("moving")

2.bool function
BOOL is a shorthand for a Boolean Boolean type that the programmer uses to represent one of two possible values, usually true or false.

>>>print(bool(0))False>>>print(bool(1))True>>>print(bool(None))False>>>print(bool(" "))True

The BOOL function returns false for empty dict, list, and tuple, otherwise it returns true.
Function Example: can be used to determine whether a value has been set.

>>>today=input("What day is today?")What day is today?  ##不做任何输入,直接回车>>>if not bool(today.rstrip()):  print("You need to enter a value for your today")

the bool (Today.rstrip ()) result is False,not false=true, so the print result is output.

3.dir functions (Directory directory)
It just tells you in alphabetical order what functions are available on top of that value.
The Dir function can basically be used for anything, including numbers, strings, functions, modules, objects, and classes. But sometimes the value it returns may be of little use. For example, if you call Dir on a number, it will show several special functions that Python uses (two underscores before and after), which is not useful (usually you don't have to relate to most of them).
4.eval function (Evaluate valuation)
Takes a string as an argument and returns it as a result of a Python expression.

>>>eval(‘print("WOW")‘)WOW

An expression that splits into multiple lines, such as an if statement, is generally not operational.
Examples of functions: often used to convert user input into Python expressions. such as calculators

>>>T=input("Input num:")Input num:5*5>>>eval(T)25

Because user input is read as a string, Python needs to be converted into numbers and operators if it is to be computed. The Eval function makes this conversion simple.

5.exec function
exec is similar to eval, but exec can run more complex programs. The difference between the two is that eval returns a value (you can save it in a variable), and exec does not.
You can use Exec to run the applet that the Python program reads from the file, which in turn contains the program! This can be useful in writing very long, complex programs.

6.float function
Converts a string or number to a floating-point number, which is a digit with a decimal point (also called a real).

7.int function
You can convert a string or a number to an integer. But if you want to convert a string containing a floating-point number (int ("123.456")) to an integer, you get an error message.

8.len function
You can return the length of an object, or the number of characters in a string for a string.

9.max and Min functions
The Max function returns the largest element in a list, a tuple, or a string. Min instead

10.range function
The number generated by range starts with the given first argument to a number that is entering primary one than the second argument.

>>>for i in range(5):  print(i)01234

The range function can also have a third parameter, called Stride. If there is no step, the default is 1

>>>print(list(range(0,10,2)))02468

11.sum function
Add the elements of the list together and return the sum.

>>>print(list(range(5)))[0,1,2,3,4]>>>sum(list)10

"28" python 12 common built-in functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.