Skip to content

Commit

Permalink
feat:add some python chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsfdtz committed Sep 28, 2024
1 parent 435c2bf commit bf4fe4b
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 12 deletions.
2 changes: 1 addition & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["MR-Addcit"]
language = "zh-CN"
src = "src"
title = "技术部培训文档"
title = "电子组培训文档"

[build]
build-dir = "book"
Expand Down
2 changes: 1 addition & 1 deletion src/Intro.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 技术部培训文档
# 电子组培训文档

本文档内容主要由 markdown 写成,使用[mdbook](https://rust-lang.github.io/mdBook)作为文档框架~~,网页部署及 CI 由**卢老前辈**完成~~

Expand Down
56 changes: 56 additions & 0 deletions src/Python/Chapter10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# lambda 函数

lambda 函数是python中的匿名函数,它可以用来创建小型的、一次使用的函数,lambda 函数通常只包含一行代码,这使得它们适用于编写简单的函数。

使用lambda函数可以让你的代码更加简洁,更加优雅。

## 语法

```
lambda arguments: expression
```

-`arguments`:函数的参数,可以有多个参数,用逗号分隔。
-`expression`:函数的表达式,可以是任意有效的Python表达式。

下面是一个例子:
```py
a = lambda: "Hello, world!"
print(a()) # 输出: Hello, world!
```

**lambda函数也可以传入参数**

```py
b = lambda x: x**2
print(b(2)) # 输出: 4
```
**lambda函数也可以有多个参数**

```py
c = lambda x, y: x + y
print(c(2, 3)) # 输出: 5
```

**同时lambda函数也可以作为函数的返回值,以及函数传入的参数。**

```py
def add(x, y):
return lambda: x + y

d = add(2, 3)
print(d()) # 输出: 5
```

## 应用

lambda函数可以用来简化代码,提高代码的可读性。
```py
# 传统方式
def add(x, y):
return x + y
add(2, 3) # 输出: 5

# 使用lambda函数
print((lambda x, y: x + y)(2, 3)) # 输出: 5
```
50 changes: 50 additions & 0 deletions src/Python/Chapter11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 模块导入

Python 允许在一个程序中导入多个模块。导入模块的语法如下:

## import 语句

```python
import module1, module2, module3
```

可以新建一个`fibo.py`文件,内容如下:

```python
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
```

然后可以在主文件`main.py`中导入`fibo.py`模块:

```python
import fibo
print(fibo.fib(10))
```
运行`main.py`文件,输出结果为`55`

## from...import 语句

如果只需要导入模块中的特定函数,可以使用`from...import`语句:

```python
from fibo import fib
print(fib(10))
```

## from … import * 语句

如果要导入模块中的所有函数,可以使用`from...import *`语句:

但是不推荐使用,因为会导致命名空间污染,容易造成命名冲突。

```python
from fibo import *
print(fib(10))
```

90 changes: 89 additions & 1 deletion src/Python/Chapter7.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,94 @@ while(condition):print ("Hello World!")

## 6. for 循环

## 6.1 for 循环的基本语法
python 中 for 循环可以遍历一个任何一个序列,例如一个字符串或者一个数组。

for 循环的语法格式如下
for 循环的语法格式如下:

```
for 变量 in 序列:
语句块
```

![python中for循环原理](./Images/python中for循环原理.png)

尝试一下:

```py
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
```
输出结果:apple banana cherry

同时整数范围值可以配合 range() 函数使用:

```py
for i in range(5):
print(i)
```
输出结果:0 1 2 3 4

### 6.2 for...else 语法

在 Python 中,for...else 语句用于在循环结束后执行一段代码。

```py
for num in range(10, 20):
if num % 2 == 0:
print(num)
else:
print("Odd number")
else:
print("Loop is over")
```

## 循环中的break和continue

`break`:用于跳出当前循环,直接执行循环后的语句。

![python中break语句的执行流程](./Images/python中break运行原理.png)

在while中使用break语句:

```py
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('循环结束。')
```

`continue`:用于跳过当前循环,直接开始下一轮循环。

![python中continue语句的执行流程](Images/python中continue执行流程.png)

在while中使用continue语句:

```py
n = 5
while n > 0:
n -= 1
if n == 2:
continue
print(n)s
print('循环结束。')
```

从上面两个代码可以很明显的看出,`break``continue`的执行流程。

试一试:

1. 输出ptsfdtz的字母,到第二个t时停止输出。

```py
s = 'ptsfdtz'
for i in s:
if i == 't':
break
print(i)
```

74 changes: 69 additions & 5 deletions src/Python/Chapter8.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
# for 循环
# 小测验

## for 语句
你已经学习了python中的一些基本语法,下面请你回答以下问题,来练一练手。

for 循环可以迭代一个可迭代对象,例如一个列表或者一个字符串
如果遇到了不会的题目,可以参考答案`答案在评论区`,有疑问可以在评论区提问

格式一般如下:
**1. 制作一个斐波那契数列生成器,要求用户输入一个整数n,然后生成n个斐波那契数列的元素。例如,用户输入n=5,则生成的斐波那契数列为:0,1,1,2,3。**
```python
zhihu = 0
l = [0, 1]
n = int(input("请输入一个整数:"))
for i in range(2, n):
zhihu = l[i-1] + l[i-2]
l.append(zhihu)
print(l)
```

**2. 编写一个代码,输入一个整数n,判断n是否为素数。如果n是素数,则输出“是素数”,否则输出“不是素数”。**
```python
n = int(input("请输入一个整数:"))
if n < 2:
print("不是素数")
else:
for i in range(2, n):
if n % i == 0:
print("不是素数")
break
else:
print("是素数")
```

**3. 编写一个代码,输入一个整数n,判断n是否为回文数。如果n是回文数,则输出“是回文数”,否则输出“不是回文数”。**
```python
n = int(input("请输入一个整数:"))
m = n
reverse = 0
while m > 0:
reverse = reverse * 10 + m % 10
m //= 10
if n == reverse:
print("是回文数")
else:
print("不是回文数")
```
for

**4. 水仙花数:一个3位数,其各位数字的立方和等于该数本身。例如,153是一个水仙花数,因为153=1的立方+5的立方+3的立方。编写一个程序,输入一个3位数,判断它是否为水仙花数。**
```python
n = int(input("请输入一个3位数:"))
if n < 10 or n > 99:
print("不是水仙花数")
else:
a = n // 100
b = (n % 100) // 10
c = n % 10
if a ** 3 + b ** 3 + c ** 3 == n:
print("是水仙花数")
else:
print("不是水仙花数")
```

**5. 编写一个程序,能够随机生成100以内的`加减法`,并且在`前面有题号``后面有正确答案`**
```python
import random

for i in range(1, 101):
a = random.randint(1, 100)
b = random.randint(1, 100)
operator = random.choice(['+', '-'])
if operator == '+':
answer = a + b
else:
answer = a - b if a >= b else b - a

print(f"{i}. {a} {operator} {b} = {answer}")
```
71 changes: 71 additions & 0 deletions src/Python/Chapter9.md
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
# 函数

前面已经学习了很多基础的语法,现在,我们需要学会封装代码,将代码封装成函数,这样可以提高代码的复用性,降低代码的复杂度。

## 1. 语法
```
def 函数名(参数列表):
函数体
```

让我们用`def`来输出`"Hello, World!"`
```py
def say_hello():
print("Hello, World!")

say_hello()
```
## 2. 参数传递

在python中可以传递多种参数,可以是**不可变参数****可变参数****关键字参数****默认参数**

**传递不可变参数**

```py
def add(a, b):
return a + b

print(add(1, 2)) # 3
```

**传递可变参数**

```py
def add_list(lst):
total = 0
for num in lst:
total += num
return total

print(add_list([1, 2, 3, 4, 5])) # 15
```
**传递关键字参数**

```py
def greet(name, age):
print("Hello, " + name + ", you are " + str(age) + " years old.")

greet(name="John", age=25) # Hello, John, you are 25 years old.
```
**传递默认参数**

```py
def greet(name, age=25):
print("Hello, " + name + ", you are " + str(age) + " years old.")

greet("John") # Hello, John, you are 25 years old.
```

在python开发中,函数的参数传递是很重要的一部分,会让你的代码更加灵活,更加易于维护。

## 3. return语句

函数的return语句用于返回函数的结果,如果没有return语句,函数的返回值是None。

```py
def add(a, b):
return a + b

print(add(1, 2)) # 3
```
如果函数没有return语句,则默认返回None。

Binary file added src/Python/Images/python中break运行原理.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Python/Images/python中for循环原理.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Summary

[技术部培训文档](./Intro.md)
[电子组培训文档](./Intro.md)

- [Arduino 基础](Arduino/Intro.md)

Expand Down Expand Up @@ -42,9 +42,13 @@
- [数据类型](Python/Chapter3.md)
- [数据类型转换](Python/Chapter4.md)
- [运算符](Python/Chapter5.md)
- [条件语句](Python/Chapter6.md)
- [循环语句](Python/Chapter7.md)

- [if条件语句](Python/Chapter6.md)
- [while和for循环语句](Python/Chapter7.md)
- [小测验](Python/Chapter8.md)
- [def函数](Python/Chapter9.md)
- [lambda 表达式](Python/Chapter10.md)
- [import 模块导入](Python/Chapter11.md)

- [通信专题](MCU-Communication/Intro.md)

- [One-Wire](MCU-Communication/Serial/One-Wire/Intro.md)
Expand Down

0 comments on commit bf4fe4b

Please sign in to comment.