#原因python
由於想讀 算法圖解 這本書,入門瞭解下算法,但又由於這本書的示例代碼是用Python寫的,因此就找了個實驗室的小姐姐借了本 Python編程——從入門到實踐,學一下Python的基礎語法。算法
那麼就開始吧。編程
#第一章 起步app
##1.1 搭建編程環境 官網下載Python 3 +文本編輯器Geanyless
#第二章 變量和簡單數據類型編輯器
##2.1 變量 在程序中可隨時修改變量的值,而Python將始終記錄變量的最新值。ide
message=「Hello World!」 print(message)
關於變量的命名和使用,應遵循:函數
##2.2 字符串 字符串就是一系列字符。在python中,用引號(單雙都行)括起的都是字母串。ui
‘I told my friend:"python is my favorite language!"’ "one of python's strengths is its diverse and supportive community."
下面來看一些使用字符串的方式idea
修改大小寫
name="ada lovelace" print(name.title()) #以首字母大寫的形式顯示每一個單詞 print(name.upper()) #將字符串所有大寫 print(name.lower()) #將字符串所有小寫
Ada Lovelace
ADA LOVELACE
ada lovelace
儲存數據時,能夠先將字符串所有轉換爲小寫,再儲存。待使用時,再轉換爲合適的大小寫方式。
python使用加號(+)來合併字符串。
在編程中,空白泛指任何非打印的字符,如空格、製表符和換行符。
要在字符串中添加 製表符:\t 換行符:\n
print("Language:\n\tPython\n\tC\n\tJavaScript")
Language:
Python
C
JavaScipt
刪除空白
favorite_language=' python ' favorite_language.rstrip() #剔除末尾空白 favorite_language.lstrip() #剔除開頭空白 favorite_language.strip() #剔除兩端空白
‘ python’
'python '
'python'
##2.3 數字 ###2.3.1 整數
##2.4 註釋
The Zen of Python, by Tim Peters
Beautiful is better than ugly. </br>Explicit is better than implicit. </br>Simple is better than complex. </br>Complex is better than complicated. </br>Flat is better than nested. </br>Sparse is better than dense. </br>Readability counts. </br>Special cases aren't special enough to break the rules. </br>Although practicality beats purity. </br>Errors should never pass silently. </br>Unless explicitly silenced. </br>In the face of ambiguity, refuse the temptation to guess. </br>There should be one-- and preferably only one --obvious way to do it. </br>Although that way may not be obvious at first unless you're Dutch. </br>Now is better than never. </br>Although never is often better than right now. </br>If the implementation is hard to explain, it's a bad idea. </br>If the implementation is easy to explain, it may be a good idea. </br>Namespaces are one honking great idea -- let's do more of those!
#第三章 列表簡介
##3.1 訪問列表元素
列表是有序集合,只需將該元素的位置或索引告訴python便可。
索引從0而不是1開始
索引指定爲-1爲訪問最後一個元素。以此類推
可像使用其餘變量同樣使用列表中的各個值
bicycles=['trek','cannondale','redline','specialized'] print("My first bicycle was a " + bicycles[-2].title() +'.')
My first bicycle was a Redline.
##3.2 修改、添加、刪除元素
修改列表元素 </br>可指定列表名和要修改的元素的索引,再指定該元素的新值。
添加元素 </br>末尾:append(值) </br>插入:insert(索引,值)
刪除元素 </br>1.若是知道要刪除的元素在列表中的位置,使用del。刪除後,沒法訪問。 </br>2.若刪除後要接着使用它的值,用pop(),括號中指定索引。 </br>3.若未知索引,可用remove(值).刪除時,也可接着使用它的值。
pet=[] pet.append('dog') pet.append('cat') pet.insert(0,'wang') print(pet) del pet[-1] too_expensive=pet.pop() pet.remove('wang') print(too_expensive + str(pet))
['wang', 'dog', 'cat'] </br>dog[]
##3.3 組織列表 ###3.3.1 使用sort()對列表進行永久性排序 python方法sort()可讓列表按字母順序排列。若想字母順序相反,則可傳遞參數reverse=True.
###3.3.2 使用sorted()對列表進行臨時排序 要保留列表元素原來的排列順序,同時以特定的順序呈現它們,可以使用函數sorted().同理,也可傳入參數reverse=True.
###3.3.3 倒着打印列表 要反轉列表元素的排列順序,可以使用方法reverse()。這不是按與字母相反的順序排列元素,而只是反轉列表元素的順序。 ###3.3.4 肯定列表長度 使用函數len()可快速獲悉列表的長度。
car=['bmw','audi','toyota','subaru'] car.sort() print(car) car.sort(reverse=True) print(sorted(car)) print(car) car.reverse() print(car) print(len(car))
['audi', 'bmw', 'subaru', 'toyota'] </br>['audi', 'bmw', 'subaru', 'toyota'] #此時爲臨時排序結果 </br>['toyota', 'subaru', 'bmw', 'audi'] #實例列表的結果 </br>['audi', 'bmw', 'subaru', 'toyota'] #反轉當前順序後的結果 </br>4
#第四章 操做列表 ##4.1 遍歷整個列表 在for循環中,每一個縮進的代碼行都是循環的一部分。
magicians=['alice','david','carolina'] for magician in magicians: #注意冒號 print(magician)
alice </br>david </br>carolina
##4.2 建立數值列表 列表很是適合用於儲存數字集合。
Python函數range()能輕鬆生成一系列的數字。從指定的第一個值開始數,並在到達指定的第二個值後中止。所以不包含第二個值。
使用函數range()時,還可指定步長。
要建立數字列表,可以使用函數list()將range()的結果直接轉換爲列表。
有幾個專門用於處理數字列表的Python函數:min() max() sum()
列表解析:將for循環和建立新元素的代碼合併成一行,並自動附加新元素。
numbers=list(range(2,11,2)) #第三個參數爲步長 print(sum(numbers)) #此時列表元素:2,4,6,8,10 squares=[value**2 for value in range(1,11)] #列表解析,此處沒有冒號 print(squares)
30
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
##4.3 使用列表的一部分 切片:處理列表的部分元素 </br>複製列表:可建立一個包含整個列表的切片
my_foods=['pizza','falafel','carrot cake'] friend_foods=my_foods[:] for food in friend_foods[:2]: print(food.title())
Pizza </br>Falafel
##4.4 元組
Pyhton將不能修改的值成爲不可變的,而不可變的列表被成爲元組。
元組使用圓括號來標識
雖然不能修改元組的元素,但能夠給儲存元組的變量賦值。
dimensions=(200,50) print("Original dimensions:") for dimension in dimensions: print(dimension) dimensions=(400,100) print("\nModified dimensions:") for dimension in dimensions: print(dimension)
Original dimensions: </br>200 </br>50
</br>Modified dimensions: </br>400 </br>100
2018/7/13 18:38:22