目录
Python注释的合理使用
注释符号#
age = 25 #liming age
print age
如果代码中存在中文,则需要在开始位置进行注释说明。
#-*-coding:UTF-8-*-
Python空行的合理使用
通常情况下为了区分每一段代码的具体功能,在函数之间或者类之间空一行。
#!/user/bin/python
#-*-coding:UTF-8-*-
class A:
def funx (self):
print "funx()"
def funy (self):
print "funY()"
class B:
def funm (self):
print "funm()"
def funn (self):
print "funn()"
...
空行分为逻辑行和物理行
物理行是在编程的过程中肉眼可以可见的,而逻辑行是区分程序语句的,用;隔开,例如:
#!/user/bin/python
#-*-coding:UTF-8-*-
a=4;d=6;#这是一个逻辑行,后面一个;也可以省略
print a+d
#这是一个物理行
print 5+a
换行符\
#!/user/bin/python
#-*-coding:UTF-8-*-
a='dfd ffdfdg dfgdf \ #这就是换行符号\
dgdfg dfgdfgdfgdfgdfghdfh'
print a
模块
主要作用是减少重复性的工作,提高效率。
比如在ABAQUS软件中,包含的模块有:Part模块、Interaction模块、Step模块等等。
类或者函数的集合就是模块,他可以实现某种特殊的功能,例如:
import math
import sys
import cv2
还可以使用from ...import...语句
第一个省略号用于表示模块,第二个省略号表示模块中的某种方法,如:
from sys import path
from math import * #导入math模块中的所有方法
from sys import path as p #导入模块中的函数并取别名
***.***
.前面表示模块,.后面表示方法,如:
import sys
print sys.path
查看某一模块下的所有属性名和方法名:
import sys
print dir(sys)
---------- Python ----------
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
Python数据类型
Python内置的数据类型主要有:数字类型、字符串类型、列表类型list、元祖类型tuple、字典类型dic等。
数字类型又可以分为:整数类型【长整型、短整形】、浮点类型、复数类型。Python语言可以自动的对数据类型进行分类转换,不需要申明变量的类型。比如:在C语言中需要先指定数据的类型,int a=100;而Python中直接用a=100。
Python语言可以根据数值的大小自动转换数据的类型,Python中的浮点型数据类型只有双精度,而没有单精度。
Python作为一种面向对象的编程语言,任何一种数据类型都是一个对象。
type是__builtin__模块的一个类。
--builtin--模块中定义的都是Python的内置函数,这些不需要导入模块,Python解释器将会自动导入。
示例:
#!/user/bin/python
#-*-coding:UTF-8-*-
a = 5
print type(a)
b = 2.221
print type(b)
d = 4+55j
print type(d)
f = False
print type(f)
输出结果如下:
---------- Python ----------
<type 'int'>
<type 'float'>
<type 'complex'>
<type 'bool'>
Output completed (0 sec consumed) - Normal Termination
字符串类型
字符串类型使用单引号、双引号或者三引号,这些引号都是在英文输入的状态下输入。
在Python中单双引号的作用相同,但是三引号的作用是用来定义