SoFunction
Updated on 2024-10-30

Commonly used built-in functions in Python

First of all, a little trick, True can be regarded as 1, False can be regarded as 0 , and can participate in the operation!

The main article begins!!!

I. map()

map(func,iterable), where func is the name of the function, which can be a lambda anonymous function, and iterable is an iterable object. This function will pass each element of the iterable object as a parameter to func, and the results of func's calculations will be added to the new list, map() returns a new list containing all the results.

II. filter()

filter(func,iterable), where func is the function name, can be lambda anonymous function, iterable for iterable object. This function will be an iterable object in each element as an argument to func, if the result is True, the element will be added to the result list, filter () returns the result is a new list.

III. all()

(iterable) function is used to determine whether all the elements in the given iterable parameter iterable are True, and returns True if they are, otherwise it returns False. iterable can be generative.

all() is often used in conjunction with filter(), e.g., the effect of the following code is to output a number with an even number in each of the digits 1000-3000.

def check(element):
    return all(
        ord(i) % 2 == 0 for i in element
    )  # all returns True if all digits i is even in element
 
 
lst = [
    str(i) for i in range(1000, 3001)
]  # creates list of all given numbers with string data type
lst = filter(check, lst)
   # filter removes element from list if check condition fails
print(",".join(lst))

It can also be written like this

s=[str(n) for n in range(1000,3001)]
res=filter(lambda n:all(int(i)%2==0 for i in n),s)
print(','.join(res))

IV. int()

  • 1. int(x,y), where x can be a numeric string or a number, and y is a number, meaning that x is converted to an integer in the y-digit system.
  • (f), where f is a floating-point number that is rounded down by default, i.e., it is not rounded up, only the integer portion is retained

V. ord()

ord(c), where c is a character, returns the corresponding ASCII code

VI. chr()

Corresponding to ord(), the chr() argument is a number from 0 to 255, returning the corresponding character

VII. isalpha()

character.isalpha(), determine whether the character is an uppercase or lowercase letter, is a letter return True, otherwise return False

VIII. isnumeric()

character.isnumeric(), determine whether the character is a numeric character, is to return True, otherwise return False

IX. UPPER()

(), which makes the lowercase letters in the string str uppercase

x. isupper()

(), returns True if all letters in str are uppercase, even if they contain other characters, and False if any letter is lowercase.

xi. islower()

(), returns True if all letters in str are lowercase, even if they contain other characters, and False if any letter is uppercase.

XII. sum()

sum(iterable[,n]), where iterable is an iterable object and the contents of [] represent optional arguments. What this function does is, it calculates the sum of the elements in iterable, and then adds n to the result to get the final result.

Attention:Iterable objects can only be used once and become empty the second time they are used.

to this article on Python commonly used built-in function of the article is introduced to this, more related Python built-in function content please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!