Python 基礎語法複習

因爲選修了《人工智能模式識別》的課程,要求用phthon來實現算法,乘着週三晚上沒課,就來回顧一下python的主要語法。python

環境:   Anaconda算法

      Python3.6app

1.變量:less

  • 在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查看變量類型
 1 #轉換  2 str_eight = str(8)  3 eight = 8  4 str_eight_two = str(eight)  5  6 str_eight = "8"  7 int_eight = int(str_eight)  8  9 print(int_eight) 10 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.程序的結構oop

  • 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 = 0 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)
  • 判斷語句
1 #if statements 2 sample_rate = 700 3 greater = (sample_rate > 5) 4 if greater: #也能夠爲表達式 5 print(sample_rate) 6 else: #不寫else也能夠 7 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)

 

  • 字典的應用----統計個數
 1 #統計  2 pantry = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"]  3 pantry_counts = {}  4  5 for item in pantry:  6 if item in pantry_counts:  7 pantry_counts[item] = pantry_counts[item] + 1  8 else:  9 pantry_counts[item] = 1 10 print(pantry_counts)

5.文件處理spa

  • 文件的讀與寫
1 f = open("test_write.txt","w") #不存在的文件自動新建 2 f.write("123456") 3 f.write("\n") 4 f.write("234567") 5 6 f.close()
 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文件的構成也有了深入的瞭解。code

6.函數的操做blog

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++不一樣,傳入參數不須要申明類型。

相關文章
相關標籤/搜索