SoFunction
Updated on 2024-11-13

Python Function Advance and File Manipulation Details

I. Review of assignments

1. Formatted output with % percent sign

In the following results, the statement "50% pass" can be output normally (B)

A, print("%d% pass" % (50)) => answer most of the results (Python is incorrectly written this way)

B. print("%d%% pass" % (50)) => Correct Result

2、String slicing

Define a string str1 = 'abcdefg', use slicing to intercept the string str1[3::-2], find the return result: (C)

C、‘db’

3. Definition of dictionary

Actually, the key in a dictionary can be many data types (immutable data types => integer, float, string, tuple)

my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 3
print(my_dict[1] + my_dict['1'] + my_dict[1.0])

II. Reference Variables and Variable and Non-Variable Types

1. Reference variables

In most programming languages, the passing of values can be divided into two forms "value passing and reference passing", but in Python the passing of variables is basically reference passing.

Talk about the form in which variables are stored at the bottom of memory

a = 10

Step one:First create a value 10 in the computer's memory (taking up a block of memory space)

Step two:Declare a variable in stack space, such as a

Step Three:Assigning the memory address of the value 10 to the variable small a creates what is known as a =="referential relationship"==

How to verify the referential relationship of variables in Python

A: You can use the built-in method id(), whose argument is the information about the variable to be displayed =>id (variable name)

a = 10
print(id(a))

The effect of assigning a variable to another variable

a = 10
b = a
print(id(a))
print(id(b))

Run results:

Description:From the results of the above run, it is clear that when we assign one variable to another, both of them point to the same memory address.

It means that a and b are pointing to the same memory space, as shown in the schematic below:

Reflections:If we change the value of variable a after b = a, ask if variable b will be affected?

# a = 10
# print(id(a))

a = 10
b = a

a = 100
print(b)  # 10 or 100

print(id(a))
print(id(b))

Schematic:

Summary:When immutable data types (numeric values) are assigned, a change in one value does not affect the other variable because they point to different space addresses.

2. Variable and non-variable data types in Python

Question 1: How many data types are there in Python?

A: 7 types, numeric (int integer, float float), bool (True and False), string (str), tuple (tuple 1,2,3), list (list [1, 2, 3]), dictionary (dict {key:value}), set (set {1, 2})

In Python, we can categorize the seven data types into two main groups: mutable types + non-mutable types

① Non-variable type

Numeric (int integer, float floating point type)

bool type (True and False)

String type (str)

Tuples (tuples 1,2,3)

② Variable type

List (list [1, 2, 3])

Dictionary (dict {key:value})

Set (set {1, 2})

Question 2: How do you determine whether a data type is mutable or non-mutable?

In Python, mutable and non-mutable types are defined primarily by how this data type is represented in memory.

① A variable type is one whose value can be changed in memory once its memory address is fixed.

a = [1, 2, 3]
print(id(a))

# Append new data to memory (changes to data can only be made by the data type. Method())
(4)
print(id(a))

Schematic:

A non-mutable type is one whose value cannot be changed once the memory address is fixed in memory.

a = 10
print(id(a))

a = 'hello'
print(id(a))

Schematic:

3. Variable and non-variable types in functions

variable type

# Define a function
def func(names):
    print(names)
    
# Define a global variable
names = ['Zhang San', 'Li Si', 'Wang Wu']
# Calling functions
func(names)

Schematic:

To summarize:Variable types are in functions that are affected both externally and internally if you add, delete, or change variable types globally or locally.

invariant type

# Define a function
def func(num):
    num += 1
    print(num)
    
# Define a global variable
a = 10
# Calling functions
func(a)
# Print a in the global scope
print(a)

To summarize:Immutable types are in functions where local or global changes have no effect either externally or internally.

III. Function recursion (key points)

1. Preamble

Programming ideas: how to use the mathematical model, to solve the corresponding demand problem; and then use the code to implement the corresponding data model

Algorithm:Implement the corresponding mathematical model using code to solve the corresponding business problem

Programs = Algorithms + Data Structures

There are two very common algorithms that we use on a regular basis:Recursive Algorithms + Recursive Algorithms, which is specialized in solving programs that are more complex but have a very high degree of similarity after splitting.

2. Recursive algorithms

Recursive Algorithms:Recursive algorithms are simple algorithms that work through known conditions and use specific conditions to draw intermediate inferences until a result is obtained. Recursion is further categorized into forward and backward.

Smooth push:Go through the simplest conditions and then extrapolate the results step by step

Reverse thrust:Find the pattern through the results and then deduce the known conditions

The case of recursive algorithms:Fibonacci sequence

1 1 2 3 5 8 13 21 …

① ② ③ ④ ⑤ ⑥ …

The 1st digit is 1, the 2nd digit is 1, the 3rd digit is 2 = 1 + 1, the 4th digit is 3 = 2 + 1, and so on...what is the nth result?

f(n) = f(n-1) + f(n-2)

Ask a question:Find the result of the 15th place in the Fibonacci series?

Analysis:f(15) = f(14) + f(13)

​ f(14) = f(13) + f(12)

​ f(13) = f(12) + f(11)

​ …

​ f(4) = f(3) + f(2) = 3 + 1

​ f(3) = f(2) + f(1) = 2

​ f(2) = 1

​ f(1) = 1

Recursive Algorithms:Using while loops or for loops

# Recursive algorithms: find the result based on known conditions (or find the unknown conditions based on the result)
def recusive(n):
    """ Return to a place in the Fibonacci series(n>=1)results """
    if n == 1 or n == 2:
        return 1
    # Start recursion f(3) = f(2) + f(1) f(4) = f(3) + f(2) ... f(15) = f(14) + f(13)
    dict1 = {1:1, 2:1}
    for i in range(3, n+1):
        # f(3) = f(2) + f(1)
        # f(i) = f(i-1) + f(i-2)
        dict1[i] = dict1[i-1] + dict1[i-2]
    return dict1[n]

# Function calls
print(recusive(15))

3, what is a recursive algorithm

The programming technique where a program calls itself is called recursion. Recursion as an algorithm is widely used in programming languages, which usually transforms a large and complex problem into a smaller problem similar to the original problem, recursion strategy requires only a small number of programs to describe the process of solving the problem requires many repetitive calculations, which greatly reduces the amount of code in the program.

① Simplify the problem: find the optimal subproblem (can't be any smaller) ② Functions call themselves

def func():
    # Calling myself
    func()
    
func()

4, recursion two important elements

Recursion has two very important concepts:

① Recursion point:Find an equivalent function that solves the current problem (first solve a function that is somewhat smaller than the current problem, and so on) => with recursive and reductive

② Recursive exit:By the time the problem is solved, the optimal problem has been reached (and must exist) and the function cannot be called again

Notes:If a recursive function has no recursive exit it becomes a dead loop

5. Writing a recursive three-step process

① Define what you want to do with this function

As:Find the Fibonacci series

② Finding the end condition of recursion

As:It's the conditions under which the recursion stops the loop and returns the result

③ Find the equivalence of the function

As:Fibonacci series, nth place f(n) = f(n-1) + f(n-2)

Case 1:Using recursion to find the Fibonacci series

Step one:Clarify what this function is trying to do (define it first and clarify how it is called)

# Fibonacci series 1 1 2 3 5 8 13 21 ...
def f(n):
    # Write recursive code to find the nth result

# Calling functions
print(f(15))  # 610

Step two:Finding the end condition of a recursion

# Fibonacci series 1 1 2 3 5 8 13 21 ...
def f(n):
    # Write recursive code to find the nth result
    if n == 1 or n == 2:
        return 1

# Calling functions
print(f(15))  # 610

Step Three:Find the equivalence equation of the function (the most critical step)

# Fibonacci series 1 1 2 3 5 8 13 21 ...
def f(n):
    # Write recursive code to find the nth result
    if n == 1 or n == 2:
        return 1
    # Find the equivalence with the Fibonacci series
    return f(n-1) + f(n-2)

# Calling functions
print(f(15))  # 610

Case 2:Use recursion to find the factorial of N (e.g. n=100)

What is the factorial? The factorial of a positive integer is the product of all positive integers less than and equal to that number, e.g., n!=1×2×3×...×(n-1)×n

1! = 1

2! = 1x2 = 2

3! = 1x2x3 = 6

4! = 1x2x3x4 = 24

n!=1×2×3×…×(n-1)×n

Step one:Clarify what this function is going to do as well as define the function and how it will be called

def f(n):
    # Write recursive conditions
    
print(f(100))

Step two:Finding the end condition of a recursion

def f(n):
    # Write recursive end conditions
    if n <= 2:
        return n
    # ... recursive equation
print(f(100))

Step Three:Write recursive equivalence formulas (you have to call yourself)

Equivalence formula = find the pattern

1! = f(1) = 1

2! = f(2) = 2

3! = f(2)x3 = 6

4! = f(3)x4 = 24

n!= f(n-1) * n

def f(n):
    # Write recursive end conditions
    if n <= 2:
        return n
    # ... recursive equation
    return f(n-1) * n
print(f(100))

Case 3:Interview Questions => Monkey Eating Peach Question

Monkey eating peaches problem. A monkey picks a number of peaches on day 1, eats half of them immediately, and, not being satisfied, eats one more. On the second morning, he ate half of the remaining peaches and ate one more. Every morning after that, he ate half of the remaining peaches from the previous day plus one more. When you tried to eat another peach on the morning of the 10th day, there was only one peach left. Find the total number of peaches picked on day 1.

Step one:Determine what the function is primarily intended to accomplish, what parameters need to be passed, and confirm how it is called

def f(n):
    # Write recursive code

# Call the f function
print(f(1))

Step two:Write end conditions for recursion (exit)

# The first step: determine the function function
def f(n):
    # Step 2: Write recursive end conditions (exit)
    if n == 10:
        return 1

# Calling functions
print(f(1))

Step Three:Find the equation relationship that is equivalent to this problem

Find the number of peaches left? Hypothetical method: Suppose there are 10 peaches

Day 1, 10 peaches half eaten, 10/2 = 5 + 1 = 6

Day 2, 4 peaches half eaten, 4/2 = 2 + 1 = 3

Day 3, trying to eat 1 more leftover

Day n, total number of peaches remaining = (number of peaches remaining from day (n+1) peaches + 1) * 2

# The first step: determine the function function
def f(n):
    # Step 2: Write recursive end conditions (exit)
    if n == 10:
        return 1
    # Step 3: Find equivalent formulas similar to this problem
    return (f(n+1) + 1) * 2

# Calling functions
print(f(8))

IV. lambda expressions

1、Ordinary function and anonymous function

In Python, a function is a named, stand-alone piece of code that accomplishes a specific function and may give a return value to the program that calls it.

So in Python, most of the functions are famous functions => ordinary functions. But there are cases where we can define anonymous functions in order to simplify the program code => lambda expressions

2, lambda expression application scenarios

If a function has a return value and only one line of code, it can be simplified using lambda.

3, lambda expression basic syntax

variant = lambda function parameter:displayed formula(function code + returnreturn value)
# Calling variables
variant()

4. Writing lambda expressions

Define a function that, after a series of operations, eventually returns 100

def fn1():
    return 100

# Call the fn1 function
print(fn1)  # Returns the address of the fn1 function in memory. #
print(fn1())  # Representatives findfn1address of the function and execute it immediately

lambda expressions for simplification:

fn2 = lambda : 100

print(fn2)  # Returns fn2's address in memory
print(fn2())

5, writing lambda expressions with parameters

Write a function to find the sum of two numbers

def fn1(num1, num2):
    return num1 + num2

print(fn1(10, 20))

lambda expressions for simplification:

fn2 = lambda num1, num2:num1 + num2

print(fn2(10, 20))

6. lambda expression related applications

lambda expressions with default arguments

fn = lambda a, b, c=100 : a + b + c
print(fn(10, 20))

Indefinite length parameters: variable parameters *args

fn1 = lambda *args : args

print(fn1(10, 20, 30))

Indefinite length parameters: variable parameters **kwargs

fn2 = lambda **kwargs : kwargs

print(fn2(name='Tom', age=20, address='Haidian District, Beijing'))

lambda expression with if judgment

fn = lambda a, b : a if a > b else b

print(fn(10, 20))

List data + dictionary data sorting (focus)

Knowledge Points:list.sort(key=sorted key index, reverse=True)

students = [
    {'name': 'Tom', 'age': 20},
    {'name': 'Rose', 'age': 19},
    {'name': 'Jack', 'age': 22}
]

# Sort by name value in ascending order
(key=lambda x: x['name'])
print(students)

# Sort by name value in descending order
(key=lambda x: x['name'], reverse=True)
print(students)

# In ascending order of age
(key=lambda x: x['age'])
print(students)

Execute the process:

students = [
    {'name': 'Tom', 'age': 20},
    {'name': 'Rose', 'age': 19},
    {'name': 'Jack', 'age': 22}
]

# Sort by name value in ascending order
(key=lambda x:x['name'])
print(students)

V. Higher-order functions in Python

1, what is a higher order function

Passing a function as an argument, such a function is called a higher-order function, and higher-order functions are the embodiment of functional programming. Functional programming refers to this highly abstract programming paradigm.

2. The origin of higher-order functions

In Python, theabs()function can be completed on the number to find the absolute value of the calculation.

① the absolute value of a positive number is itself ② the absolute value of a negative number is its opposite

abs() returns all positive numbers.

abs(-10) # 10

round()function can complete the rounding calculation of the number.

round(1.2)  # 1
round(1.9)  # 2

Demand:Any two numbers, organize the numbers according to the specified requirements (① absolute value ② rounding) and then calculate the sum.

def fn1(num1, num2):
    return abs(num1) + abs(num2)

print(fn1(-10, 10))
def fn2(num1, num2):
    return round(num1) + round(num2)

print(fn2(10.2, 6.9))

Request:Can we simplify the above and merge them into the same function => Design Ideas (Higher Order Functions)

def fn(num1, num2, f):
    # f for the parameter to be passed in (the parameter is a function name such as abs or round)
    return f(num1) + f(num2)

# Absolute value summation
print(fn(-10, 10, abs))
# Rounded up
print(fn(10.2, 6.9, round))

3, map () function

map(func, lst), which acts on the incoming function variable func into each element of the lst variable and returns the result as a new list (Python2)/iterator (Python3).

lst = [1, 2, 3]

func function: to find the square of a number, such as input 2 to return 4, input 3 to return 9

map(func, lst)Returns results [1, 4, 9].

# Define a function
def func(n):
    return n ** 2
# Define a list
list1 = [1, 2, 3]
# Use map for func function operations on lst
list2 = list(map(func, list1))
print(list2)

4, reduce () function

reduce(func,lst), where func must have two arguments. The result of each func calculation continues to be cumulative with the next element of the sequence. > Note: reduce() passes in func which must take 2 arguments.

list1 = [1, 2, 3]

def func(a, b):

​ return a + b

reduce(func,lst)Then each element of the list is put into the func for processing, and then the accumulation operation is performed

import functools
# Define a function
def func(a, b):
    return a + b
# Define a list
list1 = [10, 20, 30, 40, 50]
sums = (func, list1)
print(sums)

5. filter () function

The filter(func, lst) function is used to filter a sequence, filtering out elements that don't match the conditions, returning a filter object. If you want to convert to a list, you can use list() to convert.

# Define a function (get all even numbers)
def func(n):
   return n % 2 == 0
# Define a sequence
list1 = [1, 2, 3, 4, 5, 6, 7, 8]
# Call the filter function to perform filtering operations
result = filter(func, list1)
print(list(result))

VI. Concept of documentation

1. What is a document

The data stored in the memory disappears when the computer is turned off. To store data for a long time, devices such as hard disks, CD-ROMs, and USB flash drives are used. In order to facilitate data management and retrieval, the concept of =="file"== was introduced.

An article, a video, an executable program, can be saved as a file and given a filename. The operating system manages the data in the disk in terms of files. Generally, files can be categorized into various categories such as text files, video files, audio files, image files, and executable files.

2. Think: What does file operation entail?

In daily operations, we have the main operation of the file: create a file, open the file, read and write files, file backup and so on.

3. Role of document operations

The role of file operations is to store some of the content (data) stored, you can let the next execution of the program to use directly, without having to re-create a copy, saving time and effort.

VII. Basic operation of documents

1、Three steps of document operation

① Open a file

② Read and write files

③ Close the file

2, open function to open the file

In Python, you can open an already existing file or create a new file using the open() function with the following syntax:

f = open(name, mode)
classifier for sums of money:The result returned is afilefile object(Follow-up will learn,Just remember.,The follow-up methods are allf.methodologies())

name:is a string of the name of the target file to be opened (can contain the specific path where the file is located).

mode:Sets the mode (access mode) for opening the file: read-only r, write w, append a, etc.

r mode:It means to open an existing file in read-only mode, and then we can only read the file. If the file does not exist, we will get an error. In addition, the r mode places the cursor on a line when opening a file.

w mode:It means that if a file is opened in write-only mode and the file does not exist, it will be created automatically. w mode is mainly defined for file writing. However, it should be noted that when writing in w mode, the cursor is also placed on the first line and the contents of the original file will be cleared.

a model:It stands for opening a file in append mode and creating it automatically if the file does not exist. a mode is also defined mainly for file writing. However, unlike the w mode, the a mode does not empty the original contents of the file, but appends the contents at the end of the file.

3, write function to write the file

Basic Grammar:

('The content to be written, required to be a string type of data')

4, close function to close the file

()

5. Entry-level cases

# 1. Open the file
f = open('', 'w')
# 2. Write content
('Life is short, I'm learning Python!')
# 3. Close the file
()

Emphasis added:Chinese garbled problem, by default, the computer commonly used encoding ASCII, GBK, UTF-8

6, to solve the problem of writing Chinese garbled

# 1. Open the file
f = open('', 'w', encoding='utf-8')
# 2. Write content
('Life is short, I'm learning Python!')
# 3. Close the file
()

7. File reading operations

read(size) method: Mainly used for reading data in text type or binary files (images, audio, video, ...).

size indicates the length (in bytes) of the data to be read from the file, if size is not passed, then it means read all the data in the file.

()  # Read all the contents of the file
(1024)  # retrieve1024Byte-length file contents,Alphabetical or numerical,one who occupies1length of a byte。Chinese writingutf-8take possession of3length of a byte。
# 1. Open the file
f = open('', 'r', encoding='utf-8')
# 2. Use the read() method to read all the contents of a file.
contents = ()
print(contents)
# 3. Close the file
()

readlines() method: Mainly used for text type data reading

readlines can read the contents of the entire file at once, and returns a list, where each line of data is an element.

# 1. Open the file
f = open('', 'r', encoding='utf-8')
# 2, read the file
lines = ()
for line in lines:
    print(line, end='')
# 3. Close the file
()

8, talk about file operation mode mode

paradigm descriptive
r Open the file in read-only mode. The pointer to the file will be placed at the beginning of the file. This is the default mode.
rb Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ Opens a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb+ Opens a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file.
w Opens a file for writing only. If the file already exists then the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, creates a new file.
wb Opens a file in binary format for writing only. If the file already exists then the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, a new file is created.
w+ Opens a file for reading and writing. If the file already exists then the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, a new file is created.
wb+ Opens a file in binary format for reading and writing. If the file already exists the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, a new file is created.
a Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
ab Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
a+ Opens a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file is opened in append mode. If the file does not exist, a new file is created for reading and writing.
ab+ Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

Although there are many modes of mode file manipulation, we only need to remember 3 characters. r, w, a

r+, w+, a+, substitution of the plus sign, full-featured, both read and write (the difference is that the pointer points to a different point in the end)

rb, wb, ab, characters on behalf of b, representing the binary form of its operation, suitable for reading text or binary format files, such as pictures, audio, video and other formats

rb+, wb+, ab+, substitute plus sign, full functionality, both read and write (the difference is that the pointer points to a different point in the end)

9, seek function to move the cursor

Both file read operations and write operations. Its starting position is determined by the file cursor.

r => file header

w => clear the contents of the file, point to the file header

a => end of document

The cursor is fixed by default according to the r, w, a mode related when the file is just opened. But we can move the cursor artificially by certain methods. This can be achieved by the seek method.

(offset,whence=0)

offset:The starting offset, that is, the number of bytes representing the offset that needs to be moved
whence:Give the offset parameter a definition that indicates where to start the offset; 0 means counting from the beginning of the file, 1 means counting from the current position, and 2 means counting from the end of the file.

In practice, seek is mainly used to reset the cursor to the start position.

(0)
# or
(0, 0)

Other applications:

>>> f = open('workfile', 'rb+')
>>> (b'0123456789abcdef')
16
>>> (5)      # 5 bytes to the right from 0
5
>>> (1)
b'5'
>>> (-3, 2)  # Move 3 bytes from right to left
13
>>> (1)
b'd'

VIII. File backup cases

1. Case demand

Demand:User inputs any file name in the current directory to complete the backup function of the file (backup file name is xx[backup] suffix, e.g.: test[backup].txt).

Realization Ideas:

① Receive the file name input by the user

② Planning backup file names

③ Write data to the backup file

2、Code realization

# 1. Receive the file name entered by the user (the name of the file to be backed up)
oldname = input('Please enter the name of the file to be backed up:')  # 
# 2. Planning backup file names (python[backup].txt)
# Search point number
index = ('.')
# Returns the filename and file suffix
name = oldname[:index]
postfix = oldname[index:]
newname = name + '[Backup]' + postfix
# 3. Perform backup operations on files
old_f = open(oldname, 'rb')
new_f = open(newname, 'wb')

# Read the contents of the source file to write to a new file
while True:
    content = old_f.read(1024)
    if len(content) == 0:
        break
    new_f.write(content)
# 4. Close the file
old_f.close()
new_f.close()

to this article on the Python function advanced and file operation details of the article is introduced to this, more related Python file operation content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!