In the process of learning programming, I will record the following:

In the process of learning programming, I will record the following:

  1. Commonly used code snippets: I will record some commonly used code snippets, such as file reading and writing, list operations, string processing, etc. These code snippets can be quickly reused in daily programming and improve coding efficiency.
# 文件读取
with open('file.txt', 'r') as f:
    content = f.read()

# 列表操作
numbers = [1, 2, 3, 4, 5]
squared_numbers = [x**2 for x in numbers]

# 字符串处理
text = 'Hello, World!'
lowercase_text = text.lower()
  1. Usage of specific functions and libraries: For some commonly used functions and libraries, I will record their usage and sample code. This way you can quickly check and understand how to use it when you need to use it.
# 使用NumPy计算数组的平均值
import numpy as np

numbers = [1, 2, 3, 4, 5]
mean = np.mean(numbers)

# 使用Matplotlib绘制折线图
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
  1. Explanations and examples of complex concepts: For some complex concepts, I will record their explanations and example codes for better understanding and application.
# 递归函数示例:计算阶乘
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

result = factorial(5)  # 5的阶乘为120

# 面向对象编程示例:定义一个矩形类
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def area(self):
        return self.width * self.height

rect = Rectangle(4, 5)
print(rect.area())  # 输出20
  1. Methods for implementing specific functions: When I encounter some specific functional requirements, I will record the methods and techniques for implementing these functions for future reference and use.
# 在列表中查找最大值
numbers = [1, 5, 2, 9, 3]
max_number = max(numbers)

# 判断一个字符串是否为回文串
def is_palindrome(s):
    return s == s[::-1]

result = is_palindrome('radar')  # 返回True

These notes can be saved in the form of text files, Markdown documents, or Jupyter Notebooks. I will classify them according to different topics or concepts for easy reference and review. At the same time, I will continue to update and supplement these notes in order to record and learn new knowledge.

Guess you like

Origin blog.csdn.net/qq_40379132/article/details/132769184