python基础语法与操作

导入模块儿
import cgi
cgi.closelog()

#也可以用如下方法进行导入,这里就只是导入一个函数
from cig import closelog

模块儿的类别

  • 1、自带模块儿
  • 2、第三方模块儿
  • 3、自定义模块儿

第三方模块儿安装方法

  • 1、pip pip install 模块儿名
  • 2、whl(下载安装)百度lfd python,直接下载下来,后缀名whl 然后目录下 pip install 文件全名+后缀名(whl)
  • 3、直接复制的方式
python的io
open(fileaddress,''),w:读取,b:二进制,a:追加
读取中文加上encoding = "utf-8"
fh = open("C:/Users/10167/Desktop/test.txt", encoding= 'utf-8')
print(fh.read())
fh.close()
fh = open("C:/Users/10167/Desktop/test.txt",'a', encoding= 'utf-8')
fh.write("你好世界")
fh.close()
python的异常
#exception format
'''
try:
    //todo
except Exception as exceptionName:
    //todo...
'''
#example:
try:
    a = 1/0
except Exception as error:
    print(error)
print("finished")

python的类

构造方法
class myTestClass:
    #构造方法
    def __init__(self):
        print("hello python")

class myTestClass2:
    def __init__(self,name,year):
        print(name+"   "+str(year))

a = myTestClass()
print(a)
b = myTestClass2("william_x_f_wang",21)
print(b)
属性和方法
class myTestClass3:
    def __init__(self,name,sex):
        self.name = name
        self.sex = sex
    def testMethod1(self):
        print("this is my testMethod1 myName is"+self.name)

c = myTestClass3("william",'男')
print(c.name)
print(c.sex)
c.testMethod1()
单继承和多继承
class Father:
    def speak(self):
        print("i am father")
class Mother:
    def listen(self):
        print("i am mother")
#单继承
class Son(Father):
    pass
d = Son()
d.speak()

#多继承
class dauther(Father,Mother):
    pass
f = dauther();
f.speak()
f.listen()
发布了157 篇原创文章 · 获赞 167 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/102806311