Python beginners must master 25 built in functions in detail
- 2021-12-04 10:30:08
- OfStack
input()
Function: Let the user input a string of characters from the console, press Enter to end the input and return the string
Note: Many beginners think it can return numbers, but it is wrong!
>>> line = input(" Input 1 Numbers: ")
Input 1 Numbers: 1
>>> line
'1' # <-- See clearly, this is not a number, just a string
# If you add directly...
>>> line + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
# The correct way is to put line Convert to numbers
>>> int(line) + 1
2
print()
Function: Convert the parameters into strings and output them to the console
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
set()
Function: Construct the collection. A common method is to pass the list into set (), and then turn it into a list to realize the rearrangement of the list.
>>> set([1, 2, 3, 3])
{1, 2, 3}
# In this way, the weight discharge is realized
>>> list(set([1, 2, 3, 3]))
[1, 2, 3]
str()
Function: Converts an object to a string. Commonly used for splicing strings and numbers.
For example, this will report an error:
>>> 'My Score is: ' + 100
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
So use str to convert 1:
>>> 'My Score is: ' + str(100)
'My Score is: 100'
chr(i)
Function: Returns the characters corresponding to the integer i, which is often used to generate alphabets.
>>> chr(20013)
' Medium '
>>> chr(97)
'a'
# And ord() Match to generate an alphabet
>>> [chr(ord('a') + i) for i in range(26)]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ord()
Function: Returns the corresponding decimal value of characters in the encoding table
>>> ord(' Medium ')
20013
>>> ord('a')
97
# And chr() Match to generate an alphabet
>>> [chr(ord('a') + i) for i in range(26)]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
bool()
Function: Judge the Boolean value of 1 object and return True or False
bool(1) => True
bool(0) => False
bool([]) => False
This function is rarely used in the actual project, just as a test tool, so that beginners can understand the Boolean state of each object.
int()
Function: Converts any binary string into an integer.
int('2') => 2
int('1010', 2) => 10 # 2 Binary system 1010 Convert to an integer 10
Description: Pass in the second parameter to specify the binary type of parameter 1.
bin()
Function: Converts an integer to a binary string
bin(2) => '0b10'
bin(10) => '0b1010'
Explanation: Why is there a 0b in front of the string? Because this is the standard way of writing, the number that begins with 0b for the next number is binary.
oct()
Function: Converts a decimal to octal string
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
0
hex()
Function: Converts a string from decimal to hexadecimal
>>> hex(11)
'0xb'
>>> hex(16)
'0x10'
abs()
Function: Take absolute value
>>> abs(-1)
1
divmod()
Function: Returns the quotient and remainder in the division operation at the same time, which is equivalent to one operation, and obtains the results of a//b and a% b at the same time.
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
3
round()
Function: 4 rounding and 5 inputting for 1 floating point number
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
4
pow(x, y[, z])
Function: If only x and y parameters are filled in, the y power of x is returned. If the z parameter is filled in, the module is taken again, which is equivalent to pow (x, y)% z.
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
5
sum(iterable)
Function: Sums all elements of the array iterable.
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
6
min (x, y, z, …)
Function: Returns the minimum number of all parameters
>>> min(1, 2, 3)
1
# Incoming arrays can also
>>> min([1, 2, 3])
1
max (x, y, z, …)
Function: Similar to min (), returns the maximum number of all parameters
list()
Function: When the passed parameter is empty, create a list; Convert the parameter to a list when the passed parameter is not empty
>>> list()
[]
# When not empty
>>> list('hello world')
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
# Try passing into the dictionary
>>> list({'a': 1, 'b': 2})
['a', 'b']
tuple()
Function: It is almost like list, except that list returns an array and tuple returns a tuple.
dict()
Function: Construct a dictionary
>>> print("hello", "world")
hello world
# Many people don't know that other characters can be inserted between parameters
>>> print("hello", "world", sep="~")
hello~world
# You can even let each parameter occupy 1 Row
>>> print("hello", "world", sep="\n")
hello
world
9
len()
Function: Returns the length of the object, or the number of elements
>>> len([1, 2])
2
>>> len({'a': 1, 'b': 2})
2
>>> len('hello')
5
reversed()
Function: Reverse the list.
Note: The return is not a list, but an iterator.
>>> reversed([1, 2, 3])
<list_reverseiterator object at 0x1016190a0>
# Need to be converted to list
>>> list(reversed([1, 2, 3]))
[3, 2, 1]
# String also 1 Sample
>>> reversed('abc')
<reversed object at 0x1015ffd90>
>>> list(reversed('abc'))
['c', 'b', 'a']
enumerate()
Function: Used for traversing objects, normal traversal, such as for el in array, can only get elements, can not get subscripts, with enumerate ().
>>> for i, el in enumerate('abc'):
... print(i, el)
...
0 a
1 b
2 c
How to use this subscript? For example, it can be used to modify the elements in the array in reverse:
>>> alphabet = ['a', 'b', 'c']
>>> for i, el in enumerate(alphabet):
... alphabet[i] = el.upper()
...
>>> alphabet
['A', 'B', 'C']
filter(func, iterable)
Function: Filter and return eligible elements
Note: 1 iterator is returned.
>>> alphabet = ['a', 'b', 'c', 'E', 'F', 'G']
>>> filter(lambda e: e.isupper(), alphabet)
<filter object at 0x1016190a0>
>>> list(filter(lambda e: e.isupper(), alphabet))
['E', 'F', 'G']
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!