1.环境配置
安装anaconda
1.1 anaconda prompt操作
1.1.1 展示安装的包
conda list
1.1.2 安装新包
install pandas
1.2 jupyter notebook
和传统的IDEA区别
2.基础操作
2.1类型转换
2.1.1 整型转换成字符
str(8)
2.1.2 字符型转化成整型
str_eight = "8"
int_eight = int(str_eight)
2.1.3 整型转换成浮点型
float(8)
2.1.4 字符型转换成浮点型
str_eight = "8"
float(str_eight)
2.2 计算
**表示幂
2.3 list
months =[]
print(type(months))
months.append("Jan")
months.append("Feb")
print(months)
输出结果
<class 'list'>
['Jan', 'Feb']
append一个个添加元素,可以添加不同类型的值
months =[]
print(type(months))
months.append("Jan")
months.append(1)
months.append("Feb")
months.append(2)
print(months)
输出结果
<class 'list'>
['Jan', 1, 'Feb', 2]
2.4 list索引
2.4.1 [ ]
countries = []
temperatures =[]
countries.append("China")
countries.append("India")
countries.append("US")
temperatures.append(30.5)
temperatures.append(25.0)
temperatures.append(15.1)
print(countries)
print(temperatures)
a = countries[0]
print(a)
输出结果
['China', 'India', 'US']
[30.5, 25.0, 15.1]
China
2.4.2 len计算list长度
ls = [1,2,3,4,5,6,7,11]
print(len(ls))
print(ls[len(ls)-1])
print(ls[-1])
输出结果
8
11
11
2.4.3 切片
ls = [1,2,3,4,5,6,7,11,23,34]
ls_1 = ls[5:9]
print(ls_1)
ls_2 =ls[6:]
print(ls_2)
输出结果
[6, 7, 11, 23]
[7, 11, 23, 34]
2.5 循环
2.5.1 for
cities =["Austin","Dallas","Houston"]
for a in cities:
print(a)
输出结果
Austin
Dallas
Houston
2.5.2 while
i = 0
while i < 3:
i += 1
print(i)
输出结果
1
2
3
2.5.3 for… in range…
for i in range(5):
print(i)
输出结果
0
1
2
3
4
2.5.4 list of list
cities =[["Austin","Dallas","Houston"],["Shanghai","Beijing","Nanjing"]]
print(cities)
for c in cities:
print(c)
输出结果
[['Austin', 'Dallas', 'Houston'], ['Shanghai', 'Beijing', 'Nanjing']]
['Austin', 'Dallas', 'Houston']
['Shanghai', 'Beijing', 'Nanjing']
cities =[["Austin","Dallas","Houston"],["Shanghai","Beijing","Nanjing"]]
print(cities)
for c in cities:
for j in c:
print(j)
输出结果
[['Austin', 'Dallas', 'Houston'], ['Shanghai', 'Beijing', 'Nanjing']]
Austin
Dallas
Houston
Shanghai
Beijing
Nanjing
2.6 判断语句
不等号!=
2.7 if… else
a =-12
if a > 0:
print(a)
else:
print(-a)
输出结果
12
animals = ["cat","dog","rabbit"]
if "cat" in animals:
print("cat found")
输出结果
cat found
2.8 字典
key value
写循环可以找到list1里对应在list2里的值,比如list1是姓名,list2 是分数。但是太麻烦了。
scores = {}
scores["jim"]=80
scores["sue"]=85
scores["ann"]=87
scores["mike"]=90
print(scores)
print(scores["mike"])
输出结果
注意scores的输出是有大括号的,说明是字典
{'jim': 80, 'sue': 85, 'ann': 87, 'mike': 90}
90
另一种定义字典的方法:
students = {
"jim":80,
"ann":87
}
print(students)
{'jim': 80, 'ann': 87}
字典的值可以修改
students = {
"jim":80,
"ann":87
}
print(students)
students["jim"] = 88
print(students)
students["jim"] = students["jim"] + 5
print(students)
{'jim': 80, 'ann': 87}
{'jim': 88, 'ann': 87}
{'jim': 93, 'ann': 87}
判断在不在字典中
students = {
"jim":80,
"ann":87
}
print("jim" in students)
True
2.9 文件处理
2.9.1 文件读取
f = open("C:/Users/Administrator/Desktop/test.csv","r")
g = f.read()
print(g)
f.close()
2.9.1 文件写
f = open("C:/Users/Administrator/Desktop/test2.csv","w")
#运行前该路径下没有test2.csv文件,运行完新建了test2.csv
f.write('12345')
f.write('\n')
#换行
f.write('23456')
f.close()
split切分
2.10 定义函数
def printhello():
print("hello")
print(printhello())
hello
None
def add(a,b):
print(a+b)
add(3,5)
8