Python学习体会 |
author: 刘敏 EMAIL:liumin3000@gmail.com
python是属于面向对象编程语言,但是在语法格式上和C,C++,还有其他语言上有很多的不同
1,变量的声明
- 在python里没有数据类型,而且在语句结束时后面没有";",对比如下:
C/C++ int a = 10; 声明一个int变量,初始化为10 string str = "hello world"; Python a = 10 str = "hello world"
所以python里的函数也没有返回值类型
2,数据类型
- 怎样知道一个变量或一个函数的返回值,到底是什么类型呢? 如: pygame.display.load("pic.jpg") 返回 surface类型 是这样 screen = pygame.display.load("pic.jpg") 而不是 surface screen = pygame.display.load("pic.jpg") 这里的screen 就是surface类型的对象,所以就要知道screen 是什么类型,就要知道左边是什么类型
3,类和函数的声明
- 在python中声明类用"class"关键字,函数用"def"
- class Myname
- def name()
4,作用域的控制
- 在C/C++和其他语言中我们是用"{}"来控制作用域的,而在python中是用缩近和冒号“:” 在python中 类,函数,for,while,if--else等等有作用域范围的后面要加冒号“:”
C/C++ class Myname { void name() { cout << 'my name is liumin' << endl; cout << "what is your name?" << endl; } } python class Myname: def name(): print 'my name is liumin' print "what is your name?" 如写成这样 python class Myname: def name(): print 'my name is liumin' print "what is your name?" 就和C/C++中 class Myname { void name() { cout << 'my name is liumin' << endl; } cout << "what is your name?" << endl; } 一样的