1. Python介绍
1.1Python基础语法
定义变量
x=1
y=2
z=x+y
判断语句
score=90
if score>=90:
print(“优秀”)
elif score>=80
print(“良好”)
elif score>60
print(“及格”)循环语句
For i in range(0,3)
print(i)
print(“end”)
定义函数def
def HelloCNBlogs():
print(“Hello cnblogs”)
def GetMax(x,y):
if x>y:
return x
else:
return y
HelloCNBlogs()
print(GetMax(3,9))
面向对象class:
class FirstTest:
def init(self,name):
self._name=name
def SayFirst(self):
print(“Hello {0}”.format(self._name))
F = FirstTest(“CNBlogs”)
F.SayFirst()
继承
class FirstTest:
def init(self,name):
self._name=name
def SayFirst(self):
print(“Hello {0}”.format(self._name))
class SecondTest(FirstTest):
def init(self,name):
FirstTest.init(self,name)
def SaySecond(self):
print(“Good {0}”.format(self._name))
S=SecondTest(“CNBlogs”);
S.SayFirst()
S.SaySecond();
7. 引入其他类
# 第一种引入的方法
import FirstWork
#
S=FirstWork.SecondTest(“CNBlogs”);
S.SayFirst()
S.SaySecond();
第二种引入方法
from FirstWork import SecondTest
ST=SecondTest(“CNBlogs”);
ST.SayFirst()
ST.SaySecond();
1.2Python 控制流和函数
python 基本数据
整数(int)
长整数(long)
浮点数(float)
布尔(Boolean)
虚数(complex)虚数的使用:
print(complex(1))
print(complex(‘3+89j’))
print(complex(4,5))
1.3Python模块
按需求类别将一些常用的功能(函数)组合在一起,形成模块。
常用Python模块,一些需要下载
Urllib
numpy\mkl
pandas
matpoltlib
scipy
stasmodels
Gensim
1. 模块安装
模块库下载地址
调用cmd安装
命令: pip install 模块库名.whl
2. 导入模块
例如:
import urllib
from urllib import request
data3=request.urlopen(“http://baidu.com“).read()
print( len(data3))
2. Python文件操作
import os
os.getcwd() #获取当前目录地址
file=open(‘test.txt’,w) #默认的mode是‘r’
open用于打开文件,如果没有该文件,那么新建一个文件
file.read()
3.Python异常值处理
try:
print(“My”)
Except Exception as er:
print(er)