-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
345 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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。 | ||
|
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters