python初级
基于Python编程从入门到实践
conda
#### conda
查看conda虚拟 conda env list
\# 创建一个名为python34的环境,指定Python版本是3.4
conda create --name python34 python=3.4
\# 安装好后,使用activate激活某个环境
activate python34 **# for Windows**
source activate python34 **\# for Linux & Mac**
\# 激活后,会发现terminal输入的地方多了python34的字样,实际上,此时系统做的事情就是把默认2.7环境从PATH中去除,再把3.4对应的命令加入PATH
返回默认环境
deactivate python34 **# for Windows**
source deactivate python34 **# for Linux & Mac**
使用conda管理包
**使用conda管理包**
\# 安装scipy
conda install scipy
\# conda会从从远程搜索scipy的相关信息和依赖项目,对于python 3.4,conda会同时安装numpy和mkl(运算加速的库)
\# 查看已经安装的packages
conda list
\# 最新版的conda是从site-packages文件夹中搜索已经安装的包,不依赖于pip,因此可以显示出通过各种方式安装的包
\# 查看当前环境下已安装的包
conda list
\# 查看某个指定环境的已安装包
conda list -n python34
\# 查找package信息
conda search numpy
\# 安装package
conda install -n python34 numpy
\# 如果不用-n指定环境名称,则被安装在当前活跃环境
\# 也可以通过-c指定通过某个channel安装
\# 更新package
conda update -n python34 numpy
\# 删除package
conda remove -n python34 numpy
\# 更新conda,保持conda最新
conda update conda
\# 更新anaconda
conda update anaconda
\# 更新python
conda update python
\# 假设当前环境是python 3.4, conda会将python升级为3.4.x系列的当前最新版本
在linux中查找python安装包的路径
python -c "import 包名;print(包名)"
python -c "import httplib2;print(httplib2)"
缓存是把数据存在内存上,而不是硬盘,访问内存比访问硬盘速度快的多
列表
基于列表来修改,添加,删除
添加 append('honda')
插入 insert(0,'honda')
删除 del motor[1]
使用pop删除 motor.pop()
根据值来删除 motor.remove('honda')
列表排序
car =['bwm','audi','toyata','subaru']
car.sort() #按字母顺序排序
car.sort(reverse=Ture) #按字母反顺序排序
car.reverse() #反转列表排列元素
len(car) #获取列表长度
car[-1] #索引-1总是返回最后一个列表元素
遍历整个列表
cars =['bwm','audi','toyata','subaru'] #for in
for car in cars:
print(car)
#创建数值列表range()
for value in range(1,5)
print(value)
#range(1,5)=[1,2,3,4]
numbers=list(range(1,5)) #number=[1,2,3,4]
even_numbers=list(range(2,11,2)) #[2,4,6,8,10],不断加2
列表解析
squares=[value**2 for value in range(1,11)]
#[1,4,9,16,25,36,49,64,81,100]
切片
player =['charles','martina','michael','florence','eli']
player[0:3] #即0,1,2
player[:3]
player[1:]
复制列表
friends_food=my_food[:] #[:]表示全部复制
元组
特点:元素不可变
dimensions=(200,50)
dimensions[0]=234 #错误,无法给元组赋值
修改元组变量
#虽然不能修改元组的元素,但可以给存储元组的变量赋值
dimensions=(200,50)
dimensions=(300,50)#重新赋值
if语句
逻辑运算符
== != and
if ....:
.....
else:
.....
if.....:
xxxxxx
elif...:
xxxxxx
else:
xxxxxx当前
字典
alien_0={'color':'green',point:5}
添加键值对
alien_0['x']=0
alien_0['y']=25
删除键值对
del alien['point']
遍历字典键值对
for key,value in user.items() #items() 函数以列表返回可遍历的(键, 值) 元组数组
for name,language in languages.items()
....
#把key存储进变量name,把value存储进变量language
遍历字典key
for name in user.keys() #提取key
for language in user.values() #提取value
用户输入
使用int()来获取数值输入
函数
def make_pizza(*toppings):
xxxxxxxxxxxxxxxxxxxxxx
#*topping中的星号让Python能创建一个名为topping的空元组 ()
#**topping 两个星号是创建空字典
类
class Dog():
def __init__(self,name,age): #__init__()实例化时调用它
#初始化属性name和age
self.name=name
self.age=age
def sit(self):
xxxxxxx
def roll_over(self):
xxxxxxx
my_dog=Dog('Bob',6) #实例化一个名叫Bob的6岁小狗
继承super()
class Car(): #父类
xxxxxxxx
xxxxxxxxxx
xxxxxxx
class Elecar(Car):
def __init__(self,make,model,year):
super().__init__(make,model,year)
"super是一个特殊函数,帮助Python将父类和子类关联起来"
文件与异常
-
函数open()接受一个参数:要打开的文件名称
-
函数close()关闭文件
-
关键字with在不再需要访问文件后将其关闭
with open('pi.txt') as file_object: contents=file_object.read() print(contents)
文件路径
with open('text_files/filename.txt') as file_object:
#逐行读取
with open(filename) as file_object:
for line in file_object:
print(line)