一、包管理器Anaconda与环境管理器Jupyter Notebook。
Anaconda可通过官网下载安装包:https://www.continuum.io/downloadswww.continuum.io
按照对应系统的版本安装即可,其中win10系统需要管理员权限才能安装。下面是常用包管理和环境管理命令:
conda upgrade --all 更新所有包
conda upgrade 包名 更新指定包
conda install 包名 安装包
conda remove 包名 卸载包
conda list 显示所有安装的包
conda create -n 环境名 包名
activate 环境名 进入环境
deactivate / source deactivate 离开环境Win/linux
conda env list 显示所有环境
conda env remove -n 环境名 卸载环境
之后可在anaconda终端安装jupyter notebook和环境自动关联包
conda install jupyter notebook
conda install nb_conda
安装完成后可在终端输入jupyter notebook启动。
二、数据
1、基本的数据类型有数字,字符串,布尔,None。
严格说来,字符串是python内建序列的一种。而数字类型包括整型int,浮点型float,复数complex。布尔型有两种值True和False,和空值None一样,首字母应该大写。
#数字和字符串
NameStr='麻花藤';
AgeNum=50;
print(NameStr,AgeNum);
PiFloat=3.14;
Nothing=None;
print(PiFloat);
print(Nothing);
#布尔型和None
Boolean=True;
print(Boolean);
Boolean=False;
print(Boolean);
Boolean=None;
print(Boolean);
2、列表、元祖、字典
其中列表符号是中括号,元祖符号是小括号,字典的符号是大括号。
#序列-基本的数据结构,元祖和列表
#定义元祖
Tuple=(3,1,4,1,5,9);
print(Tuple);
#len函数查询元祖长度
print(len(Tuple));
#查询元祖的第2个元素
print(Tuple[1]);
#列表的定义与增删改查
List=[];
print(List);
List.append(1);
#一次拓展多个元素
List.extend(List);
print(List);
List.append(2);
print(List);
#list函数将字符串(列表)转换为单个元素的列表
ListStr=list(['ab','cd','ef','gh']);
print('ListStr= %s'%(ListStr));
del ListStr[2];
print(ListStr);
#改
ListStr[0]='dd';
print(ListStr);
#切片
Listslice=ListStr[1:];
print(Listslice);
#取奇数个
Listslice=ListStr[::2];
print(Listslice);
#查询
print('dd' in Listslice);
print(Listslice.index('gh'));
字典
#数据结构-字典
#创建
Dic={};
#更改与增加
Dic[42]='Foolish';
print(Dic);
#查询
Dic["name"]='马云';
print(Dic);
#get方法防止报错
Value=Dic.get('name');
print('Value is %s'%Value);
Value=Dic.get('nam')
print('Value is %s'%Value);
三、判断
若if或者elif后的表达式为True时,执行对应的代码块,若所有if或者elif均不满足,则执行else后的代码。
Score=100;
if Score<=60:
print('不及格')
elif Score>60 and Score<90:
print('良')
else :
print('优秀')
四、循环
#循环遍历
Dic1={'name':'JackMa','age':20,'Weight':70,'ID':'SD1234321'};
for i in Dic1:
print('Key=%s Values=%s'%(i,Dic1[i]))
#一般循环
for i in range(0,10,1):
print('i= %s'%(i));
for循环适用于知道运行次数的循环,若对次数不确定,可用while。
x=0;
while True:
x=x+1;
if x==10:
break #break终止当前的循环
print('x= %s'%x);
print(x);
在创建循环时,应该注意不要使循环运行的条件永远为真。
五、函数
函数可以完成固定的功能,在python中用def 定义函数,return返回函数的值
def Plus(a,b):
return a+b;
print(Plus(2,3));
其中a,b为形参,其作用域函数内部。
六、数据结构
1)栈和堆
栈:后进先出,类似停车场,水桶中的水
from collections import deque #导入函数
DD=deque([1,2,3,4,5]) #定义队列
DD.append(6); #入栈
print(DD);
DD.popleft(); #出栈
print(DD)
堆:先进先出,类似排队
Stack=deque(['马老师','麻花藤','马捷克']); #创建队列
print(Stack);
Stack.append('雷老板'); #进栈
print(Stack);
Stack.pop(); #出栈
print(Stack);
2)排序字典
两个元素键和值为一组,进行数据的存储。类似生活中的字典,名字和人。
from collections import OrderedDict; #导入函数
GatafaOD=OrderedDict({'谷歌':'GOOG','亚马逊':'AMZN','Facebook':'FB',
'苹果':'AAPL','阿里巴巴':'BABA','腾讯':'0700'});
print(GatafaOD);
3)计数器Counter
from collections import Counter #导入函数
ConDic=Counter('文人作文,农人掘锄,本是平平常常的') #创建计数器
print(ConDic['文']); #查询文 出现次数
print(ConDic.most_common(3)); #调用方法,查询出现频率最高的3个字
七、包和模块
在python中,一个后缀为.py的文件就代表一个模块,内涵变量和函数,具有一定的功能。而一般将功能具有关联的模块放在一个文件夹中,这个文件夹就称为包。
模块的使用
import 包名称
import 包名称 as 包别名
from 包名称 import 函数名