1. 程式人生 > >從 Python到Tensorflow 學習之路(一)

從 Python到Tensorflow 學習之路(一)

從 Python到Tensorflow 學習之路(一)


最近畢業設計題目是研究對抗樣本,要用tensorflow來搭建神經網路,因此python必不可少,這個不是一個傳統的Python學習教程只是把學習Python過程中遇到的問題和經驗記錄下來(基於Python2.7),如果想要一步一步學習Python建議看下面的網站。

Python學習教程


python字串

與C和C++不同,單引號和雙引號在括起字串的時候均可以。

print ('hello python')
print ("hello python")

可以在字串前輸入“`實現多行效果

print('''hello python
      python2
      pytnon3'''
)

變數賦值問題,請執行下面程式碼,理解賦值實際上是將一個變數指向另另一個變數所指向的資料

a = '123'
b = a
a = '456'
print a,b

執行結果實際上是456,123

Python中的list和tuple

list

  • len()函式可以獲取list的長度
friendlist = ['Alice','Bob','Clark']
print len(friendlist)
  • list的索引依舊是從0開始,可以用負數 n 來取倒數第 |
    n|
    個元素
friendlist = ['Alice','Bob','Clark']
print friendlist[-1], friendlist[-2], friendlist[-3]
  • list可以通過append將新元素加入末尾,也可以通過insert方法插入指定位置,還可以通過pop進行刪除(可加索引),也可以直接賦值給指定的索引位置
#append
friendlist = ['Alice','Bob', 'Clark']
friendlist.append('David')
print friendlist

#insert
friendlist.insert(1,'Evil') print friendlist #pop friendlist.pop(3) print friendlist
  • list中的元素可以是相同的資料型別也可以是不同的資料型別
my_list = ['Apple', 123, 3.2]
print my_list

tuple

  • 另外一種有序列表叫做tuple(元組),但是tuple初始化後不能進行修改,tuple是用小括號。
my_tuple = ('apple', 4, 3.14)
print my_tuple
  • tuple的不變是指每個元素的指向不變,但是tuple的每個元素可以發生變化,但是如果改變下面的整數或者浮點數將會報錯
my_tuple = (['apple','banana'], 4, 3.14)
my_tuple[0][0] = 'orange'
my_tuple[0][1] = 'lemon'
print my_tuple

Python中的條件判斷和迴圈

與C和C++不同沒有else if只有elif

age = 12
if age >= 18:
    print 'adult'
elif age >= 6:
    print 'teenager'
else:
    print 'kid'

for…in迴圈

sum = 0
for x in range(101):
    sum += x
print sum

Python中的dict和set

dict類似於C++中的map,使用鍵和值儲存,使用大括號(list用中括號,元組用小括號,dict則用大括號)

dictionary = {'Son':20, 'Father':50,'Mother':48}
print dictionary['Son']

當不確定鍵對應的值是否存在時,有下面兩種方法:

#Use in
dictionary = {'Son':20, 'Father':50,'Mother':48}
print 'Sister' in dictionary 

#Use get method
print dictionary.get('Sister')

前者會輸出False,而後者會輸出None.可以在get函式引數指定想要得到的value(如何找不到對應的value,則輸出預設的值)

可以利用pop方法刪除一個key,其對應的value也將從dict中刪去(dict的key是不可變物件)

dictionary = {'Son':20, 'Father':50,'Mother':48}
print dictionary
dictionary.pop('Father')
print dictionary 

set也是key的集合,但是不儲存value.set中沒有重複的key,重複的元素在set中會被自動過濾,這一點很方便.建立set需要使用一個list作為輸入集合

my_set = set([1,2,2])
print my_set

set可以看作是數學上無須且無重複元素的集合,因此兩個set可以做數學上的交和補操作

set_1 = set([1,2,3])
set_2 = set([2,3,4])

print set_1&set_2
print set_1|set_2

###list

Python中的函式

cmp函式,cmp(a,b)如果 a>b 返回1,如果 a==b 返回0 如果 a<b 返回-1

print cmp(2,1)
print cmp(1,1)
print cmp(1,2)

Python中的函式可以起別名,函式名就是指向一個函式的引用,可以把函式名賦給一個變數

a = abs
print a(-1.5)

Python中的空函式,利用pass語句佔位,讓程式碼可以執行起來

def judge_age(age):
    if age >=18:
        pass
    elif age >= 12:
        print 'teenager'
    else:
        print 'kid'

judge_age(18)

Python中函式返回多個元素,實際上是返回一個tuple

#calculate a circle's area and length
def calculate_circle_parameters(radius):
    area = 3.14*radius*radius
    length = 6.28*radius
    return area,length
print calculate_circle_parameters(4)

使用預設引數降低呼叫函式的難度

#calculate a circle's area and length
def calculate_circle_parameters(radius,pi = 3.14):
    area = pi*radius*radius
    length = pi*radius
    return area,length
print calculate_circle_parameters(4)
print calculate_circle_parameters(4, pi=3.1415926)

使用預設引數呼叫函式時,預設引數必須指向不變的物件,因為Python函式在定義的時候預設引數已經被計算出來,當不斷使用預設引數時,就會使用上一次的結果。

def add_list(L = []):
    L.append('0')
    return L
print add_list()
print add_list()

Python中的可變引數,在函式定義時,在引數前面加號,即可讓引數接收任意個引數(接收一個tuple).如果本身就有一個tuple或者list,可以在list或者tuple前面加號來把其中的元素變成可變引數呼叫函式.

def calculate_sum(*num):
    result = 0
    for x in num:
        result += x
    return result
print calculate_sum()

print calculate_sum(1,2,3)

L = [2,3,4]
print calculate_sum(*L)

T = (4,5,6)
print calculate_sum(*T)

Python中關鍵字引數允許傳入0個或者任意個含引數名的引數,這些關鍵字引數在函式內部自動組裝成一個dict

def person(name,age,**kw):
    print 'name:',name, 'age:',age, 'others',kw

person('Bauer', 30)

person('Bauer', 30, city='Guangzhou')

kw = {'city':'Guangzhou','job':'teacher'}
person('Bauer',30,**kw)

Python中定義函式,有必選引數,預設引數,可變引數和關鍵字引數,這四種引數可以混用.但是*引數定義的順序必須是:必選引數、預設引數、可變引數和關鍵字引數*

def func(a,b,c=0,*args,**kw):
    print 'a = ', a, 'b = ',b, 'c = ',c ,'args = ',args, 'kw = ',kw

func(1,2)

func(1,2,c=3)

func(1,2,3,'a','b')

func(1,2,3,'a','b',x=99)

我們下期見!~