head first python(第一章)--學(xué)習(xí)流程圖
![](http://source1.godblessyuan.com/blog_headfirstpython_20150413.png)
1.安裝python
這里是用python3的,除了windows之外竞帽,linux和macos都自帶了程拭,只是版本沒有這么新议忽。
舉例:centos 6.5的python版本為2.6:
python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3和2主要是有些語法和功能有些微區(qū)別栈幸,但不影響本書閱讀速址。
python有一個自帶的idle環(huán)境由驹,如上面代碼,可以用來測試代碼和查看幫助文檔
例如:
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
查看list的幫助
>>> help(list)
>Help on class list in module __builtin__:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
| Methods defined here:
| __add__(...)
| x.__add__(y) <==> x+y
有一些術(shù)語需要了解,內(nèi)置函數(shù)BIF 就是build in function甥郑,就是python一般自帶的函數(shù),可以直接調(diào)用嗅钻,例如直接print 輸出
2.處理復(fù)雜數(shù)據(jù)
1.列表
movies = ["The holy grail","the life of brain","0.02,"["the second list","abc"]"]
python的變量標(biāo)識符沒有類型养篓,例如赂蕴,列表只是一個高層的集合,他不關(guān)心列表存的是什么數(shù)據(jù).
列表就像數(shù)組,有下標(biāo)嚣伐,例如print(movies[1]),有長度len(movies)
可以列表末尾增加數(shù)據(jù)movies.append()轩端,列表末尾刪除數(shù)據(jù)moveis.pop(),列表增加列表movies.extend(["abc","cde"]),指定刪除特定數(shù)據(jù)movies.remove(括號內(nèi)是值逝变,value),指定在特定位置增加數(shù)據(jù)movies.insert(1拱层,"aaa")
對于已有列表的情況下宴咧,考慮如何增加列表數(shù)據(jù)比較好?
答案是使用insert()函數(shù)烙肺,延伸思考刪除和管理列表數(shù)據(jù)方法柿冲。
2.for循環(huán),迭代數(shù)據(jù)
如果想處理每一個列表的數(shù)據(jù)項怎栽,就需要迭代數(shù)據(jù)了
for each in movies:
print(each)
經(jīng)典for循環(huán)宿饱,for 目標(biāo)標(biāo)識符 in 列表:
疑問:
1.Q:有些字符串用雙引號引起來,而有些用單引號
A: PYTHON中沒有規(guī)定要使用哪一種强饮,只有一個規(guī)則为黎,如果字符串前面使用了某個單引號或者雙引號,那么字符串后面也要使用同樣的剪廉,一般情況下炕檩,引號是為了創(chuàng)建字符串的。
2.Q:如果需要在一個字符串中嵌入一個雙引號改怎么做泉沾?
A: 用\進(jìn)行轉(zhuǎn)義,或者使用單引號引起這個姓迅,不過通常來說用\比較好看揭朝。
3.python是區(qū)分大小寫的。
3.在列表中存儲列表
python中列表是可以存儲任何東西的,哪怕是列表锋恬,所以列表嵌套列表是可以的,如果要訪問a列表中第二項(也是列表)的第三項數(shù)據(jù)彤悔,就是print(movies[2][3]),如此類推索守。可以根據(jù)多維數(shù)組來理解杨赤。
如果遇到列表嵌套列表的情況截汪,那么單純的for循環(huán)并不能很好的訪問數(shù)據(jù)項,所以需要利用if 和isinstance來判斷
for each_item in movies:
if isinstance(each_item,list):
for nested_item in each_item:
print(nested_item)
else:
print(each_item)
判斷是否是列表阳柔,是的話增加一個迭代蚓峦,不是的話就直接打印當(dāng)前值,嵌套越深就要增加越多判斷代碼
4.不重復(fù)代碼霍转,使用函數(shù)
因為上面遇到代碼越來越多的問題干茉,而且代碼重復(fù)的情況嚴(yán)重,所以需要使用函數(shù)def
def 函數(shù)名(參數(shù)):
函數(shù)代碼組
將上面的代碼 函數(shù)化之后
def print_lol(the_list):
for each_item in the_list:
if isinstance(each_tem,list):
print_lol(each_item)
else:
print(each_item)
如果處理的是列表則使用print_lol()沾谓,如果不是的話就使用普通的print
使用的時候就可以使用
print_lol(movies)
很靈活,而且代碼也規(guī)范了昏兆,如果需要修改的話就直接修改def內(nèi)的函數(shù)體妇穴。
知識點(diǎn)補(bǔ)充:
1.python里列表是"打了激素"的數(shù)組,意味著列表比數(shù)組更厲害跑筝,更好用瞒滴。
2.python的語句的縮進(jìn)是必須規(guī)范的。
原文鏈接:http://www.godblessyuan.com/2015/04/13/head_first_python_chapter_1_learning/