本文转载自"狂龙ing”的Blog,地址为“http://blog.csdn.net/kuanglong2016/article/details/18369529"
#include
class Person{
public:
int foo(int num);
private:
void no(){
}
};
int Person::foo(int num){
//example function to show
printf("Hello World!\n");
for(int i = 0; i < 10; ++i){
printf("%d\t", i);
}
printf("\n\n");
if(num > 3){
printf("%d > 3\n", num);
}else if(num <3){
printf("%d < 3\n", num);
}else{
printf("%d == 3\n", num);
}
num = num >= 3 ? 10: -3;
while(num > 3){
--num;
}
switch(num){
case 1:
printf("The number is %d at the end!\n", num);
break;
case 2:
printf("The number is %d at the end!\n", num);
break;
case 3:
printf("The number is %d at the end!\n", num);
break;
default:
printf("The number is %d at the end!\n", num);
}
/*
Test try...catch
*/
try{
throw 20;
}catch(int e){
printf("An exception occurred. Exception Nr. %d\n", e);
}
}
int main(int argc, const char *argv[]){
Person *p = new Person();
p->foo(10);
delete p;
p = NULL;
return 0;
}
#!/usr/bin/env python
class Person:
def foo(self, num):
"""docstring for foo
example function to show
"""
print 'Hello World!'
for i in range(0,10):
print "%d\t" %i
else:
print "There is no element"
print "\n\n"
if num > 3:
print "%d > 3\n" % num
elif num < 3:
print '%d < 3\n' % num
else:
print "%d == 3\n" % num
while num > 3:
num -= 1
else:
print "%d <= 3" % num
#Test try...except
try:
raise Exception(20)
except Exception as e:
print "An exception occurred. Exception Nr. %d" % e.message
def no(self):
pass
if __name__ == '__main__':
p = Person()
p.foo(10)
p.foo(num = -2)
del p
p = None
语句 | 类C语言 | Python |
---|---|---|
逻辑与 | && | and |
逻辑或 | || | or |
逻辑非 | ! | not |
空值 | NULL | None |
while循环 | while | while..[else..] |
for in循环 | for..in | for..in..[else..] |
条件判断 | if..else if..else | if..elif..else |
抛出异常 | throw | raise |
捕捉异常 | catch | except |
类C语言:返回类型 函数名(参数列表){函数语句},比如int foo(int num){return num*2;}
Python:def 函数名(参数列表):函数语句