由于选修了《人工智能模式识别》的课程,要求用phthon来实现算法,乘着周三晚上没课,就来回顾一下python的主要语法。
环境: Anaconda
Python3.6
1.变量:
- 在python中,变量是不需要提前声明类型的
1 #data type 2 str_test = "China" 3 int_test = 123 4 float_test = 122.5 5 6 print(str_test) 7 print(int_test) 8 print(float_test)
- 类型可以转换,用type查看变量类型
#转换 str_eight = str(8) eight = 8 str_eight_two = str(eight) str_eight = "" int_eight = int(str_eight) print(int_eight) print(type(int_eight))
2.list类型
- list类型里的元素类型可以不同,添加元素时可用list.apppend(),添加时自动为其设置下标index,可使用下标访问list中的元素
1 countries = []
2 temperatures = []
3
4 countries.append("China")
5 countries.append("India")
6 countries.append("United States")
7
8 temperatures.append(30.5)
9 temperatures.append(25.0)
10 temperatures.append(15.1)
11
12 print(countries)
13 print(temperatures)
14
15 china = countries[0]
16 china_temperature = temperatures[0]
17 print(china)
18 print(china_temperature)- 计算list长度,切片操作,值得注意的是list[-1] == list[length-1], 即下标是循环的
1 int_months = [1,2,3,4,5,6,7,8,9,10,11,12] 2 length = len(int_months) 3 print(length) 4 index = len(int_months)-1 5 last_value = int_months[index] 6 print(last_value) 7 print(int_months[-1]) #倒数也可 8 #切片 9 two_four = int_months[2:4] #取头不取尾 10 print(two_four) 11 tree_last = int_months[3:] 12 print(tree_last)
3.程序的结构
- loop>>for, range()的用法值得注意
1 #loop 2 cities = ["Austin","Dallas","Houston"] 3 for city in cities: 4 print(city) 5 for i in range(10): 6 print(i)
- loop>>while
1 i =2 while i < 3: 3 i += 1 4 print(i)
- 双重循环
1 cities = [["Austin","Dallas","Houton"],["Haerbin","Shanghai","Beijing"]] 2 print(cities) 3 #for city in cities: 4 #print(city) 5 6 for i in cities: 7 for j in i: 8 print(j)
- 判断语句
#if statements
sample_rate = 700
greater = (sample_rate > 5)
if greater: #也可以为表达式
print(sample_rate)
else: #不写else也可以
print('less than')- list中查找的简写
1 #find a value
2 animals = ["cat","dog","rabbit"]
3 for animal in animals:
4 if animal == "cat":
5 print("Cat found")
6 if "cat" in animals:
7 print("2 is also right")4.dictionary类型
- 字典的创建、初始化和赋值
1 students = {}
2 students["Tom"] = 60
3 students["Jim"] = 70
4 print(students)
5
6 students = {}
7 students = {
8 "Tom": 60,
9 "Jim": 70
10 }
11 print(students)- 字典的应用----统计个数
#统计
pantry = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"]
pantry_counts = {}
for item in pantry:
if item in pantry_counts:
pantry_counts[item] = pantry_counts[item] + 1
else:
pantry_counts[item] = 1
print(pantry_counts)5.文件处理
- 文件的读与写
1 f = open("test_write.txt","w") #不存在的文件自动新建
2 f.write("123456")
3 f.write("\n")
4 f.write("234567")
5
6 f.close()<br />1 #File
2 #打开
3 f = open("test.txt","r")
4
5 #处理
6 g = f.read()
7 print(g,type(g))
8
9 #关闭
10 f.close() #不要忘了关闭- 文件csv的操作举例以及split()的使用
1 weather_data = []
2 f = open("weather.csv",'r')
3 data = f.read()
4 #print(data)
5 rows = data.split("\n")
6 #print(rows)
7 for row in rows:
8 split_row = row.split(",")
9 print(split_row)
10 weather_data.append(split_row[0])
11 print(weather_data)
12 f.close()操作该部分时,我先建立了一个xlsx文件,随后将名称改为了csv文件,打不开文件,使用“rb”操作后显示乱码。随后发现另存为csv文件可以很好的解决这个问题,对csv文件的构成也有了深刻的了解。
6.函数的操作
1 def printHello():
2 print("hello python")
3
4 def printNum():
5 for i in range(0,10):
6 print(i)
7 return
8 def add(a,b):
9 return a+b
10 printHello()
11 printNum()
12 add(1,2)与C/C++不同,传入参数不需要申明类型。