编程语言语法
程序 类型 语法 | python | C++/C | java | matlab | ||
---|---|---|---|---|---|---|
或 | or | \ | \ | |||
与 | and | && | ||||
非 | not | ! | ||||
条件语句 | if 条件 : 制表符 elseif 或elif: 制表符 else : 制表符 | if (条件){ } else if() { } else { } | ||||
循环语句 | 1.for for x in [1,2,3,…] : 制表符 2.while while 循环条件: 制表符 | 1.for for(int i=0;i\<10;i++){} 2.while while(条件){} do{ }while(条件) | ||||
语句之间 | 制表符 | {} | ||||
dict表 | d = {‘Michael’: 95, ‘Bob’: 75, ‘Tracy’: 85} | |||||
set | s1 = set([1, 2, 3]) s2 = set([2, 3, 4]) print(s1 & s2) print(s1 \ | s2) s1.add(4) s1.remove(1) | ||||
平方 | import math math.sqrt(2) | |||||
函数定义 | def wan_find(list, col, list2): col_max = 0 col_y = 0 col=col # a = np.linspace(0, 5, 5); for i in [0, 1, 2, 3, 4, 5]: if col_max \< list[i, col] and (i not in list2): col_y = i col_max = list[i, col] return col_y, col_max | int wan_find(float list[][], int col,int list2){ } | ||||
默认参数 | def product(x, y=1, z=1, k=1): return x * y * z * k | |||||
递归函数 | def fact(n): return fact_iter(n, 1) def fact_iter(num, product): if num == 1: return product return fact_iter(num - 1, num * product) | |||||
map | r =map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) list(r) | |||||
reduce | reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4) | |||||
filter | def is_odd(n): return n % 2 == 1 r=filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]) print(list(r)) | |||||
快速排序 | sorted([36, 5, -12, 9, -21], key=abs) key=abs按照绝对值排序 | |||||
面向对象 | class Student(object): def _init_(self, name, score): self.name = name self.score = score def print_score(self): print(‘%s: %s’ % (self.name, self.score)) def get_grade(self): if self.score >= 90: return ‘A’ elif self.score >= 60: return ‘B’ else: return ‘C’ |
我很可爱,请给我钱
- 本文链接:http://www.codekp.cn/2020/11/20/%E7%A8%8B%E5%BA%8F%E8%AF%AD%E6%B3%95/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。
您可以点击下方按钮切换对应评论系统。
GitHub Issues