03 2021 档案
摘要:1、 非函数形式 #include <stdio.h> int main(void) { int a[4][3] = {{54,63,14},{65,85,78},{85,74,69},{25,65,78}}; int b[4][3] = {{25,65,74},{85,74,96},{25,87,
阅读全文
摘要:创建一个函数,将和有n个元素的数组中的key相等的所有元素的下标存储在另一个数组中,并返回和key元素相同的元素的个数。 1、数组元素的查找 #include <stdio.h> #define NUMBER 10 int func1(const int x[], int y[], int z, i
阅读全文
摘要:创建一个函数,将和有n个元素的数组中的key相等的所有元素的下标存储在另一个数组中,并返回和key元素相同的元素的个数。 1、数组元素的查找 #include <stdio.h> #define NUMBER 10 int func1(const int x[], int y[], int z, i
阅读全文
摘要:c语言中数组元素的哨兵查找法,用于查找数组中是否存在特定的元素,如果存在,就返回目标元素的索引。 像对于线性查找法,哨兵查找法可以减少程序进行判断的次数。 在数组末尾追加的数据称为哨兵。 1、 #include <stdio.h> #define NUMBER 7 #define FAILED -1
阅读全文
摘要:c语言中数组元素的线性查找。 1、再数组中查找特定的元素,并返回查找的索引。 #include <stdio.h> #define NUMBER 7 #define FAILED -1 int func1(const int x[], int y, int z) { int i = 0; while
阅读全文
摘要:创建一个函数,对元素个数为n的int型数组进行倒序排列,并将结果保存到另一个数组中。 1、 #include <stdio.h> #define NUMBER 9 void func1(int x[], const int y[], int z) { int i; for (i = 0; i < z
阅读全文
摘要:创建一个函数,对元素个数为n的int型数组进行倒序排列 1、 #include <stdio.h> #define NUMBER 9 void func1(int x[], int y) { int i; for (i = 0; i < y / 2; i++) { int tmp = x[i]; x
阅读全文
摘要:1、创建一个函数,返回元素个数为n的int型数组中的最小值 #include <stdio.h> #define NUMBER 7 int func1(const int x[], int y) { int i, min = x[0]; for (i = 0; i < y; i++) { if (x
阅读全文
摘要:c语言中函数的传递和const类型的修饰符。 c语言中函数的传递:对接受到的数组元素进行的修改,元素值的变化也会反映到再次调用时传入的数组中。 const类型的修饰符:在给函数传递数组时,如果担心传递给函数的数组的元素会被修改,只要在声明形参的时候加上被称为const的类型修饰符就可以了。 如果只是
阅读全文
摘要:c语言, 函数中数组的传递,形参和实参。 1、 #include <stdio.h> #define NUMBER 5 int func1(int x[], int y) ##函数中传递数组的形参 { int i, max = x[0]; for (i = 0; i < y; i++) { if (
阅读全文
摘要:1、 通过函数原型声明,可以指定函数的参数以及返回值的类型等信息,这样就可以放心地调用函数了。 库函数printf和puts等的函数原型声明就包含在<stdio.h>文件中,因此必须执行 #include <stdio.h> 固定指令,通过执行 #include <stdio.h>指令,就可以将库函
阅读全文
摘要:c语言中的文件作用域。 1、c语言中的文件作用域 #include <stdio.h> #define NUMBER 5 ## 对象式宏 int v[NUMBER]; ## 在函数外声明的变量,文件作用域,定义声明 int func1(void); ## 因为func1函数是在main函数之后创建的
阅读全文
摘要:c语言中编写函数求五个学生中的最高分。 1、 #include <stdio.h> #define NUMBER 5 int v[NUMBER]; int func1(void) { int i, max = v[0]; for (i = 0; i < NUMBER; i++) { if (v[i]
阅读全文
摘要:1、c语言中没有形参的函数 #include <stdio.h> int func1(void) ## 没有形参的函数 { int i; puts("please input an integer."); do { printf("i = "); scanf("%d", &i); if (i <=
阅读全文
摘要:c语言中将输入的正整数进行逆向输出。 1、do语句结合while语句 #include <stdio.h> int main(void) { int i; puts("please input an integer."); do { printf("i = "); scanf("%d", &i);
阅读全文
摘要:1、c语言中没有返回值的函数(使用函数输出等腰直角三角形) #include <stdio.h> void put_star(int n) ## 函数没有返回值,返回值类型设为void。 { int i; for (i = 1; i <= n; i++) putchar('*'); } int ma
阅读全文
摘要:1、将两次考试的分数存储在三维数组中。 #include <stdio.h> void mat_add(int x[2][4][3], int y[4][3]) { int i, j, k; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { y[
阅读全文
摘要:1、创建一个函数,将4行3列矩阵和3行4l.ie矩阵b的乘积,存储在4行4列矩阵c中。 #include <stdio.h> void mat_mul(int x[4][3], int y[3][4], int z[4][4]) { int i, j, k; for (i = 0; i < 4; i
阅读全文
摘要:1、c语言中计算4行3列矩阵和3行4列矩阵的乘积 #include <stdio.h> int main(void) { int i, j, k; int a[4][3], b[3][4]; puts("input the array a."); for (i = 0; i < 4; i++) {
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i, j, k; int a[4][3] = {{4,2,3},{1,5,4},{2,3,1},{4,2,6}}; ## 4行3列矩阵 int b[3][4] = {{4,2,3,6},{2,4,3,5},{2,3
阅读全文
摘要:1、 c语言中求三维数组元素的和(2、4、3) #include <stdio.h> int main(void) { int i, j, k; int v[2][4][3] = {{{74,58,96},{14,25,36},{45,65,74},{45,56,96}},{{74,58,36},{
阅读全文
摘要:1、c语言中求4行3列二维数组元素的和 #include <stdio.h> int main(void) { int i, j; int a[4][3] = {{74,85,69},{14,25,36},{45,65,96},{58,47,56}}; int b[4][3] = {{45,56,7
阅读全文
摘要:1、一维数组 #include <stdio.h> #define NUMBER 6 int main(void) { int i; int v[NUMBER]; for (i = 0; i < NUMBER; i++) { printf("v[%d] = ", i); scanf("%d", &v
阅读全文
摘要:#include <stdio.h> #define NUMBER 7 int search(const int v[], int idx[], int key, int n) { int i = 0, j = 0; while (i < n) { if (v[i] == key) { idx[j]
阅读全文
摘要:1、计算1到n之间的所有整数的和。 #include <stdio.h> int sum(int n) { int i; int sum = 0; for (i = 1; i <= n; i++) { sum += i; } return sum; } int main(void) { int x;
阅读全文
摘要:1、值传递,计算x的n次幂 #include <stdio.h> double power(double x, int n) { int i; double tmp = 1.0; for (i = 1; i <= n; i++) { tmp *= x; } return tmp; } int mai
阅读全文
摘要:1、计算int型整数的四次幂 #include <stdio.h> int sqr(int x) { return x * x; } int sqr2(int a) { return sqr(sqr(a)); } int main(void) { int n; puts("please input
阅读全文
摘要:1、c语言在函数中调用其他函数(计算四个数中的最大值) #include <stdio.h> int max2(int a, int b) { return (a > b) ? a : b; } int max4(int x, int y, int z, int p) { return max2(m
阅读全文
摘要:1、c语言中定义函数和调用函数(计算int型整数的立方) #include <stdio.h> int cube(int a) { return a * a * a; } int main(void) { int n1; puts("please input an integer."); print
阅读全文
摘要:1、将函数的返回值作为参数传递给其他函数,计算平方差 #include <stdio.h> int sqr(int x) { return x * x; } int diff(int a, int b) { return (a > b ? a - b : b - a); } int main(voi
阅读全文
摘要:1、c语言中定义函数和调用函数(计算三个数中的最小值) #include <stdio.h> int min3(int a, int b, int c) { int min = a; if (b < min) min = b; if (c < min) min = c; return min; }
阅读全文
摘要:1、 #include <stdio.h> int min2(int a, int b) { if (a < b) return a; else return b; } int main(void) { int n1, n2; puts("please input two integers!");
阅读全文
摘要:1、c语言中定义函数和调用函数(计算三个数中的最大值) #include <stdio.h> int max3(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }
阅读全文
摘要:1、求两个数中的较大值 #include <stdio.h> int main(void) { int i, j, max; puts("please input two integer!"); printf("i = "); scanf("%d", &i); printf("j = "); sca
阅读全文
摘要:1、函数定义和函数调用 #include <stdio.h> int max2(int a, int b) ## 函数头:返回类型, 函数名,形参声明a,形参声明b { if (a > b) return a; ## 函数体(复合语句) ## 函数定义 else return b; } int ma
阅读全文
摘要:1、c语言中main函数和库函数 #include <stdio.h> int main(void) { /*.......*/ return 0; } 其中绿色部分称为main函数。 c语言程序中,main函数是必不可少的。程序运行的时候,会执行main函数的主体部分。 main函数中使用了pri
阅读全文
摘要:1、虽然通过对象式宏来变更元素的数目非常方便,但是每次都需要对程序进行修改,然后重新编译执行。 我们可以定义一个比较大的数组,然后从头开始仅使用其中需要的部分。 #include <stdio.h> #define NUMBER 80 int main(void) { int i, j; int a
阅读全文
摘要:1、 #include <stdio.h> #define NUMBER 4 int main(void) { int i; int a[NUMBER]; int sum = 0; puts("please input the scores."); for (i = 0; i < NUMBER; i
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int num, i = 0, sum = 0, tmp; puts("please input the num."); printf("num = "); scanf("%d", &num); while (i < nu
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i = 1, j; puts("please input an integer!"); printf("j = "); scanf("%d", &j); for ( i = 1; i <= j; i++) { if
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i, j; puts("please input an integer!"); printf("j = "); scanf("%d", &j); for (i = 1; i <= j; i++ ) { if (j
阅读全文
摘要:1、 偶数: #include <stdio.h> int main(void) { int i,j; puts("please input an integer!"); printf("j = "); scanf("%d", &j); for (i = 1; i <= j; i++) { if (
阅读全文
摘要:c语言中程序的循环控制(while语句、for语句实现程序循环特定的次数) 1、while语句 #include <stdio.h> int main(void) { int i; puts("please input an integer!"); printf("i = "); scanf("%d
阅读全文
摘要:c语言中程序的循环控制,while语句和for语句实现正整数固定次数的递增。 1、while语句 #include <stdio.h> int main(void) { int i = 0, j; puts("please input an integer!"); printf("j = "); s
阅读全文
摘要:c语言中程序的循环控制,(do while, while语句实现正整数的逆向输出) 1、 #include <stdio.h> int main(void) { int i; do ## do语句实现程序的循环控制,限定用户的输入范围 { printf("i = "); scanf("%d", &i
阅读全文
摘要:1、 符合赋值运算符:a = a + 1, 表示将a增加1,并将结果赋值给a。 后置递增运算符:a++, 表示将a增加1,该表达式的值为递增之前的值。 前置递增运算符:++a,表示将a增加1,该表达式的值为递增之后的值。 符合赋值运算符: a = a - 1, 表示将a的值减少1,并将结果赋值给a。
阅读全文
摘要:1、 c语言中程序的循环控制,while语句,输入指定个数个整数并显示他们的和及平均数。 #include <stdio.h> int main(void) { int num, i = 0, temp, sum = 0; puts("please input the number of circu
阅读全文
摘要:c语言中程序的循环控制,do语句和while语句的区别: do语句是先执行循环体,然后对循环条件进行判断,如果循环条件为1(为真),则执行循环体,否则不执行循环体,也就是说do语句至少执行1次循环体;while语句在执行循环体之前对循环条件进行判断,如果循环条件为1(为真),则执行循环体,否则不执行
阅读全文
摘要:c语言中程序的循环控制,do语句;while语句;for语句; 多重循环; 1、c语言中中程序的循环控制;do语句如何实现了程序的循环控制? do语句中循环体至少执行一次,然后依据while表达式的判断结果决定是否继续执行循环体, 如果while表达式的判断结果为1(为真),则继续执行循环体,否则跳
阅读全文
摘要:1、printf后面只能跟双引号 #include <stdio.h> int main(void) { printf("a"); ## 正确写法 return 0; } #include <stdio.h> int main(void) { printf("abcd"); ## 正确写法 retu
阅读全文
摘要:c语言中程序的循环控制(while语句,在执行循环体之前对循环进行判断,限制循环的次数) 1、输出‘*’字符指定次数 #include <stdio.h> int main(void) { int i; puts("please input an integer!"); printf("i = ")
阅读全文
摘要:c语言中循环控制语句,while循环,在执行循环体之前对循环条件进行判断。 输出从0开始到指定整数的所有值。 1、 #include <stdio.h> int main(void) { int i = 0, j; puts("please input an integer"); printf("j
阅读全文
摘要:c语言中循环控制语句,在执行循环体之前对循环条件进行判断。输入从输入整数到0的所有整数,设置循环条件为 大于等于0,循环体中逐次递减。 1、 #include <stdio.h> int main(void) { int i; puts("please input an integer!"); pr
阅读全文
摘要:1、 复合赋值运算符: a = a + 1 a = a - 1 后置递增运算符和后置递减运算符: a++ a-- 2、区别 a = a + 1: 将a增加1的值赋值给a a++: 将a的值增加1 (表达式的值为增加前的值) a = a - 1; 将a减少1的值赋值给a a--:将a的值减少1 (表达
阅读全文
摘要:c语言中程序的循环控制(do语句,用户决定输入数字个数,求和及平均值) 1、 #include <stdio.h> int main(void) { int j; int sum = 0, cnt = 0; do { int i; puts("please input an integer!");
阅读全文
摘要:c语言中循环控制语句(do语句,限制用户输入数值的范围) 1、 #include <stdio.h> int main(void) { int i; do { puts("please input an integer! the range is 0-2!"); printf("i = "); sc
阅读全文
摘要:c语言中循环控制语句;(do语句,用户输入决定循环次数) 1、 int main(void) { int j; do { int i; puts("please input an integer!"); printf("i = "); scanf("%d", &i); if (i % 2) puts
阅读全文
摘要:c语言中分支结构程序,多分支情况。switch case / default; break; 1、 #include <stdio.h> int main(void) { int i; puts("please input an integer!"); printf("i = "); scanf("
阅读全文
摘要:c语言中程序的循环控制(while循环) 1、 #include <stdio.h> int main(void) { int i = 0, j; puts("please input an integer!"); printf("j = "); scanf("%d", &j); while (i
阅读全文
摘要:c语言中程序的循环控制,(while循环) 1、 #include <stdio.h> int main(void) { int i; puts("please input an integer!"); printf("i = "); scanf("%d", &i); while (i >= 0)
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i; int sum = 0; int cnt = 0; int j; do { puts("please input an integer!"); printf("i = "); scanf("%d", &i);
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i; do { puts("please input the i value."); printf("i = "); scanf("%d", &i); if (i < 0 || i > 2) puts("the s
阅读全文
摘要:1、 do + 循环体 + while语句;当while语句不为0时,会继续循环,当while语句为0时,跳出循环。 #include <stdio.h> int main(void) { int i, j; ##(仅在循环体使用的变量要在复合语句(程序块)中进行声明。???) do { puts(
阅读全文
摘要:1、c语言中switch语句 将程序分为多个分支的时候,可以使用switch语句。 #include <stdio.h> int main(void) { int i; puts("please input an integer."); printf("i = "); scanf("%d", &i)
阅读全文
摘要:1、求两个数中的较大值 #include <stdio.h> int main(void) { int i,j,max; puts("please input two integers"); printf("i = "); scanf("%d", &i); printf("j = "); scanf
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int a, b; puts("please input two integer."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b);
阅读全文
摘要:1、 #include <stdio.h> int main(void) { double i, j; puts("please input two float type value."); printf("i = "); scanf("%lf", &i); ## double类型的变量通过scan
阅读全文
摘要:1、获取变量的最后一位数字 #include <stdio.h> int main(void) { int i; puts("please input an integer."); printf("i = "); scanf("%d", &i); printf("The last number of
阅读全文
摘要:1、创建module1.py模块 def fun1(x): print(x.upper()) def fun2(x): print(x.title()) def fun3(x): print("--",x) 2、导入module1中的所有函数 >>> fun1("aaaa") Traceback (
阅读全文
摘要:1、将函数fun1、fun2和fun3存储在名称为module1.py的模块中 def fun1(x): print(x.upper()) def fun2(x): print(x.title()) def fun3(x): print(x) 2、导入module1.py模块,使用as为其指定别名为
阅读全文
摘要:1、将三个函数fun1\fun2\fun3存储在名称为module1.py的模块中 def fun1(x): print(x.upper()) def fun2(x): print(x.title()) def fun3(x): print(x) 2、给函数fun1指定别名abc. >>> abc(
阅读全文
摘要:1、将函数存储在模块里 def fun1(x): ## 在模块module1.py中定义三个函数 print(x.upper()) def fun2(x): print(x.title()) def fun3(x): print(" ",x) 2、测试能否直接调用函数 >>> fun1("aaa")
阅读全文
摘要:将函数存储在模块中,在主程序中调用模块。 1、将函数存储在模块中 def fun1(x): ## 函数1 print(x.upper()) def fun2(x): ## 函数2 print(x.title()) def fun3(x): ## 函数3 print(x) 保存为module1.py
阅读全文
摘要:import语句允许在当前运行的程序文件中使用模块中的代码。 要让函数是可导入的,得先创建模块。模块是扩展名为.py的文件。 1、创建module1,命名module1.py,定义三个函数fun1、fun2、fun3 def fun1(x): print("11111",x) def fun2(x)
阅读全文
摘要:有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况下,可将函数编写成能够接受任意数量的键值对--调用语句提供了多少就接受多少。 1、 >>> def a(**x): ## 在形参前面添加双星,可以在实参中添加任意数量的关键字参数,两个*让python创建一个名为x的
阅读全文
摘要:1、如果要让函数接受不同类型的实参,必须在函数定义中将接受任意数量实参的形参放在最后。python先匹配位置参数和关键字参数,再将余下的实参都收集到最后一个形参中(收集参数)。 >>> def a(*x,y): ## 将收集参数放在所有形参的最前面 print(f"- {x}") print(f"!
阅读全文
摘要:有时候, 预先不知道函数需要接受多少个实参,好在python允许函数从调用语句中收集任意数量的实参。 1、 >>> def a(*x): ## 在形参的前面加上*号,表示收集参数,可实现函数接受任意数量的实参 print(x) >>> a("aaa") ('aaa',) >>> a("aaa","b
阅读全文
摘要:1、原始函数 def a(x,y): while x: temp = x.pop() print(f"printing model:{temp}") y.append(temp) def b(x): print("\nThe following model had been printed!") f
阅读全文
摘要:1、不使用函数: list1 = ["aaaa","bbbb","cccc"] list2 = [] while list1: temp = list1.pop() print(f"printing model {temp}") list2.append(temp) print("\nThe fol
阅读全文
摘要:1、 python中向函数传递列表,提高处理列表的效率。 >>> def a(x): ## 定义函数的形参x为可迭代对象 for i in x: print(f"hello,{i.title()}") >>> b = ["aaa","bbb","ccc","ddd"] ## 实参定义为列表,实现向函
阅读全文
摘要:1、 >>> def a(first,meddle,last): b = f"{first} {meddle} {last}" return b.title() >>> a("aaa","bbb","ccc") 'Aaa Bbb Ccc' >>> a("aaa","bbb") ## 少一个实参报错
阅读全文
摘要:1、 >>> def a(x,y): b = {"key1":x,"key2":y} return b >>> a(111,222) {'key1': 111, 'key2': 222} >>> a(111,222,333) ## 多家实参报错 Traceback (most recent call
阅读全文
摘要:1、 def a(first,last): full = f"{first} {last}" return full.title() while True: ## 条件为真的话,一直循环 print("\n\nplease tell me your name:") ## 两个\n,每执行一次循环,空
阅读全文
摘要:python中函数可以返回任何类型的值,包含列表和字典等较复杂的数据结构。 1、 >>> def fun(first,last,age): full = {"first":first,"last":last,"age":age} ## 函数返回列表 return full >>> infor = f
阅读全文
摘要:1、 >>> def a(first,middle,last): ## 函数定义了三个形参 full = f"{first} {middle} {last}" return full.title() >>> name = a("aaa","bbb","ccc") >>> print(name) Aa
阅读全文
摘要:1、 >>> def a(x,y,z): print(x + y + z) >>> a(2,3,4) 9 >>> a(2,3) ## 缺少对应的实参 Traceback (most recent call last): File "<pyshell#317>", line 1, in <module
阅读全文
摘要:1、 >>> def a(x,y): """ function: sum author: xxxx date: 2021:03:08 """ print(x + y) >>> a(10,8) 18 >>> print(a.__doc__) ## 获取函数文档 function: sum author
阅读全文
摘要:1、 >>> result = 1 >>> for i in range(1,5): ## 4的阶乘 result = result * i >>> print(result) 24 >>> result = 1 >>> for i in range(1,6): ## 5的阶乘 result = r
阅读全文
摘要:1、利用迭代 def fun(n): ## 定义求阶乘函数 result = 1 for i in range(1,n + 1): result *= i return result n = int(input("please input the number:")) ## 指定用户输入 resul
阅读全文
摘要:1、map()内置函数有两个参数,为一个函数和一个可迭代对象,将可迭代对象的每一个元素作为函数的参数进行运算加工,直到可迭代对象的每一个元素都计算完毕。 >>> def a(x): ## 定义函数a() return 3 * x >>> a(5) 15 >>> temp = map(a,range(
阅读全文
摘要:1、 filter()函数是一个过滤器,它的作用就是在海量的数据里面提取出有用的信息。 filter()这个内置函数有两个参数:第一个参数可以是一个函数也可以是None, 如果是一个函数的话,则将第二个可迭代对象里的每一个元素作为函数的参数进行计算,把返回True的值筛选出来;如果第一个参数是Non
阅读全文
摘要:1、lambda关键字用于创建匿名函数,和三元操作符一样,匿名函数在很大程度上简化了函数的定义过程。 lambda的基本用法: lambda使用冒号分割函数的参数和函数的返回值,冒号的左边为函数的参数,如果参数为多个则使用逗号分割开,冒号的右边是函数的返回值。 lambda表达式的返回值为函数对象,
阅读全文
摘要:1、 匿名函数的作用在很大程度上简化了函数的定义过程。 使用lambda关键字来定义匿名函数,lambda关键字的使用方法,使用冒号分割函数的参数和函数的返回值,冒号的左边为函数的参数,冒号的右边为函数的返回值,如果有多个参数,中间使用逗号隔开即可。执行完lambda语句后实际上返回一个函数对象,如
阅读全文
摘要:m.2和sata指的是借口; sata3和pcie指的是通道; nvme和ahci指的是协议; 1、 M.2跟SATA其实可以说是插槽的形状,而PCI-E跟SATA3就是数据从硬盘到CPU或者内存走的通道,而NVME跟AHCI就是针对PCI-E跟SATA通道的“交通规则” 固态硬盘近年来也是随着计算
阅读全文
摘要:在定义函数的时候,若实参个数不确定,形参就可以使用收集参数来“搞定”,仅需要在参数前面加上星号(*)即可。 1、 使用收集参数来表示形式参数时,需要在参数前加上星号。 >>> def a(*x): print("total %d parameter!" % len(x)) print("second
阅读全文
摘要:1、demo >>> def a(x = 100, y = 200): print(x + y) >>> a() ## 这里x = 100, y = 200是默认参数。 300 >>> a(y = 500, x = 2000) 2500 >>> a(x = 1000, y = 800) 1800
阅读全文
摘要:1、 >>> def a(): print("begin eating!") >>> print("starting!") starting! >>> a() begin eating! >>> print("ending") ending ↓ >>> def a(): print("startin
阅读全文
摘要:1、 >>> def a(): x = 200 def b(): print("inner:",x) return b() >>> a() inner: 200 2、 >>> def a(): x = 100 def b(): x = 300 print("inner:",x) return b()
阅读全文
摘要:1、 内嵌函数中,内层函数可以调用外层函数的局部变量 >>> def a(): x = 8 def b(): print(x + 3) return b() >>> a() 11 >>> def a(): x = 10 def b(y): print(x + y) return b >>> a()(
阅读全文
摘要:1、 内嵌函数的内层函数的作用域在外层函数之内,不能在外层函数外调用内层函数。 >>> def a(): ## 外层函数a(),内层函数b(). print("hello world!") def b(): print("good morning!") return b() >>> a() hell
阅读全文
摘要:1、 >>> def a(): print("hello world!") >>> a <function a at 0x000002CE49DCB550> >>> a() hello world! 2、 >>> def a(): print("hello world!") def b(): pri
阅读全文
摘要:1、 >>> def a(): x = 10 def b(): x = x + 8 print(x) return b >>> a() <function a.<locals>.b at 0x000001B47F4D8040> >>> a()() Traceback (most recent cal
阅读全文
摘要:1、定义 如果在一个内部函数里,对在外部作用域但不是全局作用域的变量进行了引用,那么内部函数就被认为是闭包。简言之,就是嵌套函数的环境下,内部函数引用了外部函数的局部变量,这个内层函数就被认为是闭包。 or 在一个外函数中定义了一个内函数,内函数中运用了外函数的局部变量,并且外函数的返回值是内函数的
阅读全文
摘要:1、一般情况下,无法利用局部变量修改全局变量的值 demo: >>> x = 10 ## 首先定义全局变量 >>> def a(): x = 10000 ## 尝试利用局部变量修改全局变量 print(x) >>> a() ## 局部变量 10000 >>> x ## 全局变量仍然为10 10 2、
阅读全文
摘要:python中允许在函数内定义另一个函数,这种函数称为内嵌函数或者内部函数。 1、 >>> def a(): ## 外层函数 print("hello world!") def b(): ## 内层函数 print("xxxxxxx!") return b() >>> a() hello world
阅读全文
摘要:1、一般情况下,无法在函数内对全局变量进行修改 >>> x = 10 >>> def a(): x = 1000 print(x) >>> a() 1000 >>> x 10 2、利用global关键字,在函数内对全局变量进行修改 >>> x = 10 >>> def a(): global x x
阅读全文
摘要:1、一般情况下,在函数内不能修改全局变量 >>> x = 10 ## 全局变量 >>> def a(): x = 1000 ## 在函数内修改全局变量x print(x) >>> a() ## 仍然输出局部变量x 1000 >>> x ## 全局变量x依然为10 10 2、使用global关键字在函
阅读全文
摘要:1、 python中定义在函数内部的变量称为局部变量,局部变量只能在局部函数内部生效,它不能在函数外部被引用。 def discount(price,rate): price_discounted = price * rate return price_discounted sale_price =
阅读全文
摘要:python允许在函数内部定义另一个函数,这种函数称为内嵌函数或者内部函数。 1、例 >>> def a(): ## a外层函数,b为内层函数 print("hello world!") def b(): print("xxxxx!") b() >>> a() hello world! xxxxx!
阅读全文
摘要:1、 >>> def a(x): def b(y): return x * y return b >>> temp = a(5) ## 内嵌函数调用外部函数的变量 >>> temp(8) 40 2、 >>> def a(): x = 5 def b(): x = x + 1 ## x在内部函数是局部
阅读全文
摘要:1、 >>> def a(): print("fun a is running!") def b(): print("fun b is running!") b() >>> a() ## 示例中函数b是函数a的内嵌函数 fun a is running! fun b is running! >>>
阅读全文
摘要:1、 >>> x = 5 ## 全局变量 >>> def a(): x = 10 ## 局部变量 print(x) >>> a() 10 >>> x ## 函数内部修改全局变量,不能真正的修改全局变量 5 2、 >>> x = 5 >>> def a(): global x ## 在函数内部增加gl
阅读全文
摘要:1、 def discount(price,rate): ## 定义函数名discount,两个形式参数price和rate sell_price = price * rate return sell_price ## 函数返回售价 price = float(input("please input
阅读全文
摘要:1、 >>> def a(*xxx): print("total length is %d" % len(xxx)) print("the second element is :",xxx[1]) >>> a("aaa","bbb","ccc","ddd","eee","fff") total le
阅读全文
摘要:1、 >>> def a(x,y,z): ## x、y、z为形式参数 print(x * y / z) >>> a(20,2,5) ## 20,2,5为实际参数 8.0 >>> 2、 >>> def a(x,y,z): print(x * y / z) >>> a(20,2,5) ## 此处x,y,
阅读全文
摘要:1、 >>> def a(x = 100, y = 20): ## 在形式参数中指定默认参数 print(x / y) >>> a() 5.0 >>> a(x = 500, y = 10) ## 传递实参 50.0 >>> a(x = 10, y = 3) 3.3333333333333335
阅读全文
摘要:1、 >>> def a(x,y,z): print(x / y + z) >>> a(10,2,4) ## 默认位置参数 9.0 >>> a(x = 2,y = 10, z = 4) ## 指定关键字参数 4.2 >>> >>> a(x = 2, y = 10,4) SyntaxError: po
阅读全文
摘要:1、 >>> def a(x,y): ## 函数文档 """ function: sum author: xxxx date: 2021.3.4 """ print(x + y) >>> a(40,80) 120 >>> print(a.__doc__) ## 获取函数文档 function: su
阅读全文
摘要:1、 >>> def a(x): ## 形参为x, 实参为50。 print(x * 100) >>> a(x = 50) 5000
阅读全文
摘要:1、 >>> def a(x): ## 单个参数 print(x,"and xiao ming are good friends!") >>> a() Traceback (most recent call last): File "<pyshell#476>", line 1, in <modul
阅读全文
摘要:1、 >>> def a(): ## 创建函数 print("helloworld!") >>> a() helloworld! ## 调用函数 >>>
阅读全文
摘要:1、 >>> a = {1,2,3} >>> a {1, 2, 3} >>> type(a) <class 'set'> >>> a.add(4) >>> a {1, 2, 3, 4} >>> b = frozenset({1,2,3}) ## 不可变集合 >>> b frozenset({1, 2
阅读全文
摘要:1、集合单个增加元素 >>> a = {111,222,333,444} >>> a {444, 333, 222, 111} >>> type(a) <class 'set'> >>> a.add(555) >>> a {555, 333, 111, 444, 222} >>> a.add("aa
阅读全文
摘要:1、直接创建 >>> a = {111,"aaa",222,"bbb",555} >>> a {555, 111, 'aaa', 'bbb', 222} >>> type(a) <class 'set'> 2、字符串转换 >>> a = "helloworld" >>> a 'helloworld'
阅读全文
摘要:1、列表 -- (中括号 + 逗号) >>> a = ["aaa","bbb","ccc","ddd"] >>> a ['aaa', 'bbb', 'ccc', 'ddd'] >>> type(a) <class 'list'> 2、元组 -- (小括号 + 逗号) >>> a = ("a","b"
阅读全文