Still walking slowly, learning is also slowly input
Recursive functions
Recursion: function calls itself nested
The formation condition of recursive function
# 1. The function calls itself
# 2. The function must have a termination condition
def get_age(num) :
Param num: :return: "" param num: :return: ""
if num == 1:
return 18
The age of num-1 is the age of num +2
age = get_age(num-1) + 2
return age
print(get_age(4)) # 24
Copy the code
Anonymous functions
Functions defined using the lambda keyword are anonymous functions
# lambda argument list expression
# 1. No parameter, no return value
def func1() :
print('hello')
(lambda: print('hello lambda'))()
func1()
f1 = lambda: print('hello lambda')
f1()
# 2. No parameter has return value
def func2() :
return 1 + 2
f2 = lambda: 1 + 2
print(f2())
# 3. No return value for an argument
def func3(name) :
print(name)
f3 = lambda name: print(name)
f3('hello')
# 4. There are arguments with return values
def func4(*args) :
return args
f4 = lambda *args: args
print(f4(1.2.3.4.5))
Copy the code
The application scenarios of anonymous functions
def my_calc(a, b, func) :
""" Perform four operations :param A: the first data :param b: the second data :param func: the operation to perform :return: the result of the operation """
print('Other function code')
num = func(a, b)
print(num)
def add(a, b) :
return a + b
# call
my_calc(10.20, add)
my_calc(10.20.lambda a, b: a - b)
my_calc(10.20.lambda a, b: a * b)
my_calc(10.20.lambda a, b: a / b)
Copy the code
Lambda functions as function arguments
List sort, the type of data in the list should be consistent
my_list = [1.3.5.4.2.1]
my_list.sort()
print(my_list)
list1 = [{'name': 'd'.'age': 19},
{'name': 'b'.'age': 16},
{'name': 'a'.'age': 16},
{'name': 'c'.'age': 20}]
# list1.sort() error
The parameter of the anonymous function is each data in the list
list1.sort(key=lambda x: x['name'])
print(list1)
list1.sort(key=lambda x: x['age'])
print(list1)
list2 = ['aghdd'.'bc'.'ghli'.'def'.'ab']
list2.sort()
# print(list2)
The list is sorted by the length of the string in the list
# list2.sort(key=len)
list2.sort(key=lambda x: len(x))
print(list2)
# sort(key= lambda parameter :(sort regulation 1, sort regulation 2,.....) )
# When the first rule is the same, the second rule is sorted
list1.sort(key=lambda x: (x['age'], x['name']))
list1.sort(key=lambda x: (x['age'], x['name']), reverse=True)
print(list1)
Copy the code
5. List derivation
Each time through the loop, one data is created
my_list = [i for i in range(5)]
print(my_list) # [0, 1, 2, 3, 4]
my_list1 = ['hello' for i in range(5)]
print(my_list1) # ['hello', 'hello', 'hello', 'hello', 'hello']
my_list2 = [f'num:{i}' for i in my_list]
print(my_list2) # ['num:0', 'num:1', 'num:2', 'num:3', 'num:4']
my_list3 = [i + i for i in range(5)]
print(my_list3) # [0, 2, 4, 6, 8]
# 2. Variable = [generate data for temporary variable in XXX if XXX]
# each loop, if the condition is True, generates one data
my_list = [i for i in range(5) if i % 2= =0]
print(my_list) # [0, 2, 4]
# 3. Variable = [generate data regulation for temporary variable in XXX for j in XXX]
# second for loop, loop once, generate one data
my_list4 = [(i, j) for i in range(3) for j in range(3)]
print(my_list4)
# Supplement: dictionary derivation
# variable = {generate dictionary planning for temporary variable in xx}
# my_dict = {key: value for i in range(3)}
my_dict = {f'name{i}': i for i in range(3)}
print(my_dict) # {'name0': 0, 'name1': 1, 'name2': 2}
my_dict = {f'name{i}': j for i in range(3) for j in range(3)}
print(my_dict) # {'name0': 2, 'name1': 2, 'name2': 2}
my_dict = {f'name{i}{j}': j for i in range(3) for j in range(3)}
print(
my_dict) # {'name00': 0, 'name01': 1, 'name02': 2, 'name10': 0, 'name11': 1, 'name12': 2, 'name20': 0,
# 'name21': 1, 'name22': 2}
Copy the code
Set — set
# set set definition using {}, {data, data}
# 1. The data in the collection must be of immutable type
my_set = {1.3.14.False.'hello', (1.2)}
print(my_set, type(my_set))
# 2. Collections are mutable types
my_set.remove(3.14)
print(my_set) # {False, 1, 'hello', (1, 2)}
my_set.pop()
print(my_set) # {1, (1, 2), 'hello'}
my_set.add(100)
print(my_set) # {1, (1, 2), 100, 'hello'}
# change data 100 --> 200
my_set.remove(100)
my_set.add(200)
print(my_set)
my_set.clear()
print(my_set)
# 3. The collection is unordered and does not support subscript operations
# 4. The data in the collection is not duplicated (de-duplicated)
my_list = [1.2.3.4.2.5.1.0]
my_list = list(set(my_list))
print(my_list)
Copy the code
7. Operation of files
7.1 read the file
# 1. Open the file, save the file from hard disk to memory
# open(file, mode='r', encoding)
# file Specifies the name of the file to operate on, of type STR
# mode, the file opening mode, r (read) read-only open, w (write) write open a (appen) append open
# encoding Specifies the file encoding format. GDK and UTF-8 are common encoding formats
# return value, file object, through which all subsequent file operations will be performed
TXT file in the current directory in read-only mode
f = open('a.txt'.'r')
# 2. Read the file object. Read ()
buf = f.read()
print(buf)
Close () synchronizes the three main memory files to the hard disk
f.close()
Copy the code
7.2 Writing Files
If the file does not exist, it will create a file. If the file does exist, it will overwrite and empty the original file
f = open('a.txt'.'w', encoding='utf-8')
# 2. Write the file object.
f.write('hello world! \n')
f.write('hello python! \n')
f.write('Hello China')
# 3. Close files
f.close()
Copy the code
Today is here to pull, have a question please a lot of advice