第一部分.基礎知識
第1章 起步
主要是搭建編程環境
Python主頁
第2章 變量和簡單數據類型
- 編輯器使用python解釋器,有“語法突出”功能
變量
message = "Hello Python world!"
print(message)
//在程序中可隨時修改變量的值,而Python將始終記錄變量的最新值。
//如,可加上下面兩行
message = "Hello Python Crash Course world!"
print(message)
- 變量的命名規則同C語言 //慎用小寫字母l和大寫字母O,因為它們可能被人錯看成數字1和0。
注意 就目前而言,應使用小寫的Python變量名。
字符串
- 字符串:字符串就是一系列字符。在Python中,用單引號或雙引號括起的都是字符串。 //這種靈活性讓你能夠在字符串中包含引號和撇號
- 使用方法修改字符串的大小寫
name = "ada lovelace"
print(name.title()) //title()以首字母大寫的方式顯示每個單詞,即將每個單詞的首字母都改為大寫。
/*在print()語句中,方法
title()出現在這個變量的后面。方法是Python可對數據執行的操作。在name.title()中,name后
面的句點(.)讓Python對變量name執行方法title()指定的操作。每個方法后面都跟着一對括號,
這是因為方法通常需要額外的信息來完成其工作。這種信息是在括號內提供的。函數title()不
需要額外的信息,因此它后面的括號是空的。*/
還有其他幾個很有用的大小寫處理方法。例如,要將字符串改為全部大寫或全部小寫,可以
像下面這樣做:
name = "Ada Lovelace"
print(name.upper()) //全部大寫
print(name.lower()) //全部小寫
3. Python使用加號(+)來合並/拼接字符串。
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
//也可以
print("Hello, " + full_name.title() + "!")
//或者
message = "Hello, " + full_name.title() + "!"
print(message)
4. 使用制表符(/t)或換行符(/n)來添加空白(在編程中,空白泛指任何非打印字符)
5. 刪除空白
8.1 方法rstrip(); //暫時刪除字符串末尾的空白
favorite_language = 'python '
favorite_language.rstrip()
favorite_language
8.2 還可以剔除字符串開頭的空白,或同時剔除字符串兩端的空白。
方法分別為.lstrip()
和.strip()
6. 使用字符串時避免語法錯誤
因為Python將第一個單引號和撇號之間的內容視為一個字符串,所以要使撇號位於兩個雙引號之間message = "One of Python's strengths is its diverse community." print(message)
數字
- Python使用兩個乘號表示乘方運算
>>> 3 ** 2
9
Python還支持運算次序2 + 3*4
- 浮點數*:Python將帶小數點的數字都稱為浮點數。
//需要注意的是,結果包含的小數位數可能是不確定的,如0.2+0.1
- 使用函數 str()避免類型錯誤
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
- Python2 中的整數相除和 Python中得到的不同
注釋
- 在Python中,注釋用井號(#)標識。井號后面的內容都會被Python解釋器忽略
# 向大家問好
print("Hello Python people!")
Python之禪
import this
執行上面語句會得到的Python之禪
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
第3章 列表簡介2
3.1 列表是什么
列表由一系列按特定順序排列的元素組成。
在Python中,用方括號([])來表示列表,並用逗號來分隔其中的元素。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
//如果你讓Python將列表打印出來,Python將打印列表的內部表示,包括方括號:
// ['trek', 'cannondale', 'redline', 'specialized']
//訪問列表元素:索引從0開始;將索引指定為-1可返回最后一個列表元素(-2返回倒數第二個,以此類推;只有為空表時會報錯)
print(bicycles[0])
motorcycles[0] = 'ducati' //修改摩托車列表中的第一個元素為“ducati”
添加和刪除元素
1.添加元素
motorcycles.append('ducati') //1.在列表末尾添加元素
//在列表中插入元素
//使用方法insert()可在列表的任何位置添加新元素。為此,你需要指定新元素的索引和值。
motorcycles.insert(0, 'ducati')
2.刪除元素
del motorcycles[0] //1. 使用del語句刪除元素,知道索引可刪除任意位置的列表元素
//使用del語句將值從列表中刪除后,你就無法再訪問它了
//法pop()可刪除**列表末尾**的元素,並讓你能夠接着使用它。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
//3. 彈出列表中任何位置處的元素
//可以使用pop()來刪除列表中任何位置的元素,只需在括號中指定要刪除的元素的索引即可
first_owned = motorcycles.pop(0)
總結:如果你不確定該使用del語句還是pop()方法,下面是一個簡單的判斷標准:如果你要從列表中刪除一個元素,且不再以任何方式使用它,就使用del語句;如果你要在刪除元素后還能繼續使用它,就使用方法pop()。
4. 使用方法remove() 根據值刪除元素
motorcycles.remove('ducati') /*注意 方法remove()只刪除第一個指定的值。如果要刪除的值可能在列表中出現多次,就需要使用循環來判斷是否刪除了所有這樣的值。*/
3.3 組織列表
3.3.1 使用方法sort()對列表進行永久性排序
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() //按字母順序排列 print(cars) //按與字母順序相反的順序排列列表元素 //只需向sort()方法傳遞參數 reverse=True cars.sort(reverse=True) //同樣,對列表元素排列順序的修改是永久性的
3.3.2 使用函數sorted()對列表進行臨時排序
print(sorted(cars))
注意
1.調用函數sorted()后,列表元素的排列順序並沒有變。如果你要按與字母順序相反的順序顯示列表,也可向函數sorted()傳遞參數reverse=True。
2.在並非所有的值都是小寫時,按字母順序排列列表要復雜些。決定排列順序時,有多種解讀大寫字母的方式,要指定准確的排列順序,可能比我們這里所做的要復雜。
3.3.3 使用方法reverse() 倒着打印列表
注意reverse()不是指按與字母順序相反的順序排列列表元素,而只是反轉列表元素的排列順序
cars.reverse() //方法reverse()永久性地修改列表元素的排列順序
3.3.4 使用函數len()可快速確定列表的長度(元素的個數)
len(cars) //從1開始計數
索引錯誤
Traceback (most recent call last):
File "motorcycles.py", line 3, in <module>
print(motorcycles[3])
IndexError: list index out of range