


Detailed explanation of built-in functions and recursion in Python basics
1. Built-in functions
The following are briefly introduced:
1.abs() Find the absolute value
2. all() If all elements of iterable are true (or if the iterable is empty), returns <span class="pre">True</span>
3.any () If any element of iterable is true, returns <span class="pre">True</span>
. If iterable is empty, return <span class="pre">False</span>
4.callable() If the object parameter appears adjustable, return <span class="pre">True</span>
, otherwise return <span class="pre">False</span>
##5.divmod() with two takes as arguments a (non-complex) number and returns a pair of numbers consisting of the quotient and remainder when using integer division. For mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as (a<span class="pre">//<span class="pre">b,<span class="pre">a<span class="pre">%<span class="pre">b)<span class="pre"></span></span></span></span></span></span> same. For floating point numbers, the result is
(q,<span class="pre">a<span class="pre">%<span class="pre">b)<span class="pre"></span></span></span></span>, where
q Usually math.floor(a<span class="pre">/<span class="pre">b)<span class="pre"></span></span></span>, but can be less than 1
6.enumerate() parameters must be iterable objects. The function results in an iterator, which outputs elements and corresponding index values.
7.eval() Extract and execute
8.frozenset() immutable collection, the collection defined by frozenset() cannot add or delete elements
9.globals() Returns a dictionary representing the current global symbol table. This is always a dictionary of the current module (inside a function or method, this is the module in which it is defined, not the module it is called from)
10.round() on the arguments Rounding
11.sorted() Sort without changing the original list
l=[1,2,4,9,-1]print(sorted(l)) #从小到大print(sorted(l,reverse=True)) #从大到小
12.zip() Zipper function
Create an iterator that aggregates elements from each iterator. Returns an iterator of tuples, where thei-th tuple contains the ith element from each argument sequence or iteration. The iterator stops when the shortest input iterable is exhausted. Taking a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator
13.max() Returns the maximum item of the iterable or the largest of two or more arguments. If a positional argument is provided, it should be an iterable. Returns the largest item in the iteration. If two or more positional arguments are supplied, the largest positional argument is returned. max() can specify the key (that is, specify the part to be compared) 14.map() mapping Returns an iterator, which appliesfunction Each item to iterable produces the result
l=[1,2,3,4] m=map(lambda x:x**2,l)print(list(m)) ----->[1, 4, 9, 16]
15.reduce() merge
from functools import reduce res=0for i in range(100): res+=iprint(res)
names=['alex_sb','yuanhao_sb','wupeiqi_sb','egon']print(list(filter(lambda name:name.endswith('_sb'),names)))--->['alex_sb', 'yuanhao_sb', 'wupeiqi_sb']
def func(x):return x**2print(func(2))lambda x:x**2 #上边的函数就可以直接写成这种形式
# 1 文件内容如下,标题为:姓名,性别,年纪,薪资# # egon male 18 3000# alex male 38 30000# wupeiqi female 28 20000# yuanhao female 28 10000# # 要求:# 从文件中取出每一条记录放入列表中,# 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式# # 2 根据1得到的列表,取出薪资最高的人的信息# 3 根据1到的列表,取出最年轻的人的信息# 4 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式# 5 根据1得到的列表,过滤掉名字以a开头的人的信息# 6 使用递归打印斐波那契数列(前两个数的和得到第三个数)# 0 1 1 2 3 4 7...with open('b.txt',encoding='utf-8')as f: l=[{'name': line.split()[0], 'sex': line.split()[1], 'age': line.split()[2], 'salary': line.split()[3]} \for line in f]#2.print(max(l,key=lambda i:i['salary']))#3.print(min(l,key=lambda i:i['age']))#4.m=map(lambda x:x['name'].capitalize(),l)print(list(m))#5.print(list(filter(lambda x:not(x['name'].startswith('a')),l)))#6.def f(n):if n==0:return 0elif n==1:return 1else:if n==1000:return f(1000)else:return f(n-2)+f(n-1)for i in range(150):print(f(i))
The above is the detailed content of Detailed explanation of built-in functions and recursion in Python basics. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
