或 |
or |
\ |
\ |
\ |
\ |
\ |
\ |
与 |
and |
&& |
&& |
&& |
非 |
not |
! |
! |
! |
条件语句 |
if 条件 : 制表符 elseif 或elif: 制表符 else : 制表符 |
if (条件){ } else if() { } else { } |
if(布尔表达式){ 如果布尔表达式的值为true}else{ 如果布尔表达式的值为false} |
if r == c A(r,c) = 2; elseif abs(r-c) == 1 A(r,c) = -1;else A(r,c) = 0;end |
循环语句 |
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(条件) |
|
|
语句之间 |
制表符 |
{} |
{} ; |
; end |
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’ |
|
|