Python built-in functions (iv)

Source: Internet
Author: User

Python provides a number of built-in functions internally. Let's start with A-Z to learn about Python's built-in functions


1.1

ID (object)

Returns the ID (identity) of the object, which is the only integer that is unique, and the ID is unique throughout the lifetime of the object.

That means that two different two numbers that are present can have the same ID. This is the address of the object in memory.

An example:

A = 0

Print (ID (a))

The result of the output is:

1582282384


1.2

Input ([prompt])

If the prompt parameter is present, it will be written in standard output without a newline, and the input function reads a line from the inputs and converts it to a string

Returns the string. Standard input stream.

Instance:

A = input (' Enter your Name: ')

Print ("Hello,", a)

Run the previous section of code:

You will see this paragraph on the controller waiting for the user's input:


Enter your name:


Enter the string as prompted:

Python


And hit the ENTER key. The result of the output is:


Hello, Python


Repl closed***

More detailed information will be found in the standard output stream print ().


1.3

int (x,base = 10)

Returns a number or string constructed of an integer object, and if no parameter x is given, it will return 0 if X is a number,

Then returns X.__INT__ (), the floating-point number is converted to an integer, base is converted to a number of digits, and 10 is converted to decimal

If x is not a number, or if the base value is given, then x must be a string, or byte bytes or byte array ByteArray

An example:

A= ' 101101 '

Print (int (a,base=2))

The result of the output is:

45


1.4

ITER ()

This function said in my previous generator that I was interested in flipping through my previous blog


1.5

Len ()

Returns the length of the object, or, if it is items, the number of elements returned, the object can be a string, a byte, a list,range,dictionary,set,frozen set

A= ' Hey,python. '

Print (Len (a))

a=[1,2,4,3,5]

Print (Len (a))

a={0,2,4,5}

Print (Len (a))

The result of the output is:

11

5

4


1.6

Locals ()

Update and return local symbol table

Example: Print (Locals ())

{' __doc__ ': None, ' __cached__ ': None, ' __name__ ': ' __main__ ', ' __loader__ ': <_frozen_importlib. Sourcefileloader object at 0x00000000023226a0>, ' __spec__ ': None, ' __package__ ': None, ' __builtins__ ': <module ' Builtins ' (built-in), ' __file__ ': ' d:\\python\\xode\\try.py '}


1.6

Map ()

The map () function accepts two parameters, one is the function, and the other is the Iterable,map function to the sequence.

Each element and take the result as a new iterator

See an example:

def func (x):

return x*x

l=[1,2,3,4,5,6]

Print (List (map (func,l)))

The result of the output is:

[1, 4, 9, 16, 25, 36]


1.7

Max () returns the maximum value

1.8

MIN () returns the minimum value

1.9

Next (Iterator[,default])

Retrieves the next element in the iterator, if the default is given when the iteration of an object in the iterator is complete, then the call will play default, otherwise the error

Instance

def func ():

X=1

while (x<5):

yield X

X+=1

L=func ()

Print (Next (L))

Print (Next (L))

Print (Next (L))

Print (Next (L))

Print (Next (L))

The result of the output is:

1

Traceback (most recent):

2

3

4

File "D:\Python\xode\try.py", line one, in <module>

Print (Next (L))

Stopiteration

In the last error,

If you make a slight change to the last step of printing

def func ():

X=1

while (x<5):

yield X

X+=1

L=func ()

Print (Next (L))

Print (Next (L))

Print (Next (L))

Print (Next (L))

Print (Next (l,5))

The result of the printing is:

1

2

3

4

5

When next exceeds the scope of the iteration, it prints his default value, and if there is no default, the error


1.11

Oct (x)

Converts an integer to an octal number, the result is a valid Python expression, and if X is not of type int, it must be converted to type int

Print (Oct (8))

The result of the output is: 0o10

Zhongzhiyuan Nanjing 904727147, Jiangsu

Python built-in functions (iv)

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.