Python (一)

来源:互联网 发布:暗影格斗2mac版存档 编辑:程序博客网 时间:2024/04/28 21:33

为什么使用Python?

http://reliscore.com/blog/why-every-programmer-should-learn-python-or-ruby/  给出了为什么每个程序员都要学习Python,并且各种当前主要语言进行对比。

下载python:

http://www.python.org/官网上下载。

安装Python:

在Windows下有exe自动安装 。在linux中可能需要手动安装推荐博文:http://www.cnblogs.com/yuechaotian/archive/2013/06/03/3115482.html

第一个Python程序:

经典的Hello World:

>>> print 'Hello World'Hello World
在输出字符串的时候需要使用单引号或者双括号,稍后介绍print函数。

内部数据类型:

在python中定义一个变量时不需要声明函数类型:

>>> l = True>>> type(l)<type 'bool'>>>> l = 's'>>> type(l)<type 'str'>>>> l = 1>>> type(l)<type 'int'>
 

     字典(Dictionary):Python中的字典类型酷似Java中的HashTable类的实例。 

     字典类型中 使用Key-Value 关系模型

>>> dic = {1:'a',2:'b',3:'c',4:'d'}>>> type(dic)<type 'dict'>

    查看关于字典类型的方法:

    在Python中万物皆对象。 这里使用dir() 内部函数:

>>> type(dic)<type 'dict'>>>> dir(dic)['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
   方法介绍:

    keys方法:返回字典类型所有的keys

>>> dic.keys()[1, 2, 3, 4]

        








原创粉丝点击