head first 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主要是有些语法和功能有些微区别,但不影响本书阅读。
python有一个自带的idle环境,如上面代码,可以用来测试代码和查看帮助文档
例如:
>>> 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
有一些术语需要了解,内置函数BIF 就是build in function,就是python一般自带的函数,可以直接调用,例如直接print 输出
1.列表
movies = [\”The holy grail\”,\”the life of brain\”,\”0.02,\”[\”the second list\”,\”abc\”]\”]
python的变量标识符没有类型,例如,列表只是一个高层的集合,他不关心列表存的是什么数据.
列表就像数组,有下标,例如print(movies[1]),有长度len(movies)
可以列表末尾增加数据movies.append(),列表末尾删除数据moveis.pop(),列表增加列表movies.extend([\"abc\",\"cde\"]),指定删除特定数据movies.remove(括号内是值,value),指定在特定位置增加数据movies.insert(1,\"aaa\")
对于已有列表的情况下,考虑如何增加列表数据比较好?
答案是使用insert()函数,延伸思考删除和管理列表数据方法。
2.for循环,迭代数据
如果想处理每一个列表的数据项,就需要迭代数据了
for each in movies:
print(each)
经典for循环,for 目标标识符 in 列表:
疑问:
1.Q:有些字符串用双引号引起来,而有些用单引号
A: PYTHON中没有规定要使用哪一种,只有一个规则,如果字符串前面使用了某个单引号或者双引号,那么字符串后面也要使用同样的,一般情况下,引号是为了创建字符串的。
2.Q:如果需要在一个字符串中嵌入一个双引号改怎么做?
A: 用\\进行转义,或者使用单引号引起这个,不过通常来说用\\比较好看。
3.python是区分大小写的。
python中列表是可以存储任何东西的,哪怕是列表,所以列表嵌套列表是可以的,如果要访问a列表中第二项(也是列表)的第三项数据,就是print(movies[2][3]),如此类推。可以根据多维数组来理解。
如果遇到列表嵌套列表的情况,那么单纯的for循环并不能很好的访问数据项,所以需要利用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)
判断是否是列表,是的话增加一个迭代,不是的话就直接打印当前值,嵌套越深就要增加越多判断代码
因为上面遇到代码越来越多的问题,而且代码重复的情况严重,所以需要使用函数def
def 函数名(参数):
函数代码组
将上面的代码 函数化之后
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)
很灵活,而且代码也规范了,如果需要修改的话就直接修改def内的函数体。
知识点补充:
1.python里列表是\"打了激素\"的数组,意味着列表比数组更厉害,更好用。
2.python的语句的缩进是必须规范的。
原文链接:http://www.godblessyuan.com/2015/04/13/head_first_python_chapter_1_lea…