Several important built-in functions of Python

Several important built-in functions of Python

 

The so-called built-in functions are functions that are automatically loaded in Python and can be used at any time. Built-in function, which means we don't have to import the module in order to use the function. You don't have to do anything for Python to recognize built-in functions. In the process of learning Python, there are several important functions:
(1) help() function
(2) dir() function
(3) input() and raw_input() functions
(4) print() function
(5) type() function

 

help function:

There are two types of parameters for the help() function:

  • If you pass a string as a parameter, it will automatically search for modules, methods, etc. named after this string.
  • If an object is passed in, the help for that object's type is displayed.

For example, if you enter help('print'), it will look for modules, classes, etc. named 'print', and if it is not found, you will see a prompt message. And print is a reserved word in python, equivalent to pass and return, not an object, so help(print) will also report an error.

for example:

1 >>>help('sys') #The help of the sys module will be listed 
2 >>>a = [1,2,3]
3 >>>help(a) #The help of the list will be displayed
4 >>>help (a.append) #will display the help of the append method of the list

 

dir function:
The dir() function returns a list of properties and methods of any object, including module objects, function objects, string objects, list objects, dictionary objects, etc. While finding and importing modules is relatively easy, remembering what each module contains is not so simple. You don't always have to look at the source code to find out. Fortunately, Python provides a way to inspect the contents of modules (and other objects) using the built-in dir() function. When you supply a module name to dir(), it returns a list of properties defined by the module. If no arguments are provided, it returns a list of properties defined in the current module. The dir() function works on all object types, including strings, integers, lists, tuples, dictionaries, functions, custom classes, class instances, and class methods.

for example:

1 >>>dir() #List the attribute list of the current module
2 ['__builtins__', '__doc__', '__name__', '__package__'] #The attribute list of the current module 


input and raw_input functions:
both input() and raw_input() functions are used to read user input. The difference is that the input() function expects the user to input a valid expression, while the raw_input() function converts the user's input The input is wrapped into a string.

for example:

1 >>>input('please input:')     
2 please input:2+3
3 5 #The result is 5, not '2+3', because Python thinks you entered the expression
4 >>>raw_input(' please input:')     
5 please input:2+3
6 '2+3' #The result is '2+3', because Python thinks you are entering a native string 

print function:
print was used as a Python statement before Python3 Yes, in Python3 print is used as a function.

for example:

1 >>>print 'hello world'
2 >>>print('hello world')

type function:
The type() function returns the data type of any object. Possible data types are listed in the types module, which is useful for helper functions that deal with multiple data types. It does this by returning a type object, which can be compared to the types defined in the types module.

for example:

1 >>>type('hello')     
2 #String data type str
3 >>>type(10)     
4 #Integer data type int 

 
 
Category: Python 2.7

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681386&siteId=291194637