python 基础

来源:互联网 发布:维生素c 知乎 编辑:程序博客网 时间:2024/06/05 00:45

python basic

1、 python module

模块是用来实现一定功能的python脚本集合
举个例子

math模块

引入模块

//在脚本模式下>>> import module_name 
//实例化引用math module>>>import math 

查看模块内容

>>>help(module_name)
查看模块内容中的具体内容
>>> help(module_name.内容) 
//举个例子>>>help(math.sin)
调用内容函数
>>> math.sin(2/π)

2、标准控制台输入/输出

(加不加括号是一样的)
①将多个数据输出到一行:数据与数据之间用逗号隔开
②将一个字符串输出到多行,采用转义字符

print 'Hello \n World!'

常用转义字符如下:
\t Tab
\ 一个\
\’ 单引号
\” 双引号

raw_input函数(读取键盘输入,将所有输入作为字符串看待)

如:

radius= float(raw_ipnut('Radius:')

(float 把字符串强制转换成浮点型)
运行后

Radius:_

等待输入

3、选择结构

else:    if

等价于

elif

要注意缩进对应(由于这语言没有大括号这概念),如:

if    if    elif    else:

4、循环结构

break 结束的是当前的整个循环体而 continue结束当次的循环,其下面的语句不会执行了

常见循环结构

while n!=1:     n++
for i in rang (i1,in):

“与”语句用 “and”连接,”或”语句用“or”

5、函数

定义函数

def function_name(形参1,形参2,...):    函数体

调用函数与其他编程语言一致

函数内部对函数外全局变量进行修改要记得声明

global variable

6、注释

python单行注释符号(#)

多行注释是用三引号”’

'''comment thisand those'''

7、列表和元组

列表就是数组[]

元组()就是封装好,不能修改的数组

8、字典

“键-值(key- value)对”

通过“键”来查找对应的”值”

9、属性函数(property)

作用一:将类方法转换为只读属性

class Person(object):    """"""    #----------------------------------------------------------------------    def __init__(self, first_name, last_name):        """Constructor"""        self.first_name = first_name        self.last_name = last_name    #----------------------------------------------------------------------    @property    def full_name(self):        """        Return the full name        """        return "%s %s" % (self.first_name, self.last_name)
>>> person = Person("Mike", "Driscoll")>>> person.full_name'Mike Driscoll'>>> person.first_name'Mike'>>> person.full_name = "Jackalope"//报错了,因为只读Traceback (most recent call last):  File "<string>", line 1, in <fragment>AttributeError: can't set attribute

作用二:重新实现一个属性的setter和getter方法

from decimal import Decimal########################################################################class Fees(object):    """"""    #----------------------------------------------------------------------    def __init__(self):        """Constructor"""        self._fee = None    #----------------------------------------------------------------------    def get_fee(self):        """        Return the current fee        """        return self._fee    #----------------------------------------------------------------------    def set_fee(self, value):        """        Set the fee        """        if isinstance(value, str):            self._fee = Decimal(value)        elif isinstance(value, Decimal):            self._fee = value
//要使用这个类,我们必须要使用定义的getter和setter方法​​:>>> f = Fees()>>> f.set_fee("1")>>> f.get_fee()Decimal('1')
from decimal import Decimal########################################################################class Fees(object):    """"""    #----------------------------------------------------------------------    def __init__(self):        """Constructor"""        self._fee = None    #----------------------------------------------------------------------    @property    def fee(self):        """        The fee property - the getter        """        return self._fee    #----------------------------------------------------------------------    @fee.setter    def fee(self, value):        """        The setter of the fee property        """        if isinstance(value, str):            self._fee = Decimal(value)        elif isinstance(value, Decimal):            self._fee = value#----------------------------------------------------------------------if __name__ == "__main__":    f = Fees()
//用一个名为@fee.setter的装饰器来装饰第二个方法名也为fee的方法来实现>>> f = Fees()//setter被调用>>> f.fee = "1"
原创粉丝点击