angrykola

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

为什么选择python?
软件质量、开发效率、程序的可移植性、标准库的支持、组件集成,质量和效率是人们选择python的主要原因。

python的成功案例:

  • Google 在其网络搜索系统中广泛应用了Python ,并且聘用了Python 的创作者
  • YouTube 视频分享服务大部分是由Python 编写的。
  • 流行的P2P 文件分享系统Bittorrent 是一个Python 程序。
  • Intel 、Cisco 、Hewlett-Packard 、Seagate 、Qualcomm 和IBM 使用Python 进行硬件测试。
  • Industrial Light & Magic 、Pixar 等公司使用Python 制作动画电影。
  • 在经济市场预测方面:JPMorgan Chase 、UBS 、Getco 和Citadel 使用Python 。
  • NASA 、Los Alamos 、Fermilab 、JPL 等使用Python 实现科学计算任务。
  • IRobot 使用Python 开发了商业机器人真空吸尘器。
  • ESRI 在其流行的GIS 地图产品中使用Python 作为终端用户的定制工具。
  • NSA 在加密和智能分析中使用Python 。
  • IronPort 电子邮件服务器产品中使用了超过100 万行的Python 代码实现其作业。
  • OLPC 使用Python 建立其用户界面和动作模块。

 

python的核心数据类型:

数字、字符串、列表、字典、元组、文件、集合。核心数据本身就属于python语言的一部分。其中数字、字符串、元组具有“不可变性”,其对象在创建后是不可改变的,但你可以通过运行一个表达式创建一个新的对象。

1 #列表
2 list = [1,2,3,4,5]
3 
4 #元组
5 tuple = ('a','b','c')
6 
7 #字典
8 dict = {'one':'abc','two':'defg','three':'xyz'}

 

 

二进制、八进制、十六进制转换:

内置函数oct(I),hex(I),会将整数以八进制数和十六进制数字符串的形式返回。btn(I)会返回一个二进制数字字符串。

八进制:0o10;十六进制:0x1F;二进制0b10

#十进制数字
a = 123
#八进制数字
b = 0o12
#十六进制数字
c = 0x12
#二进制数字
d =0b10

 

字符串

string = 'test str'

#切片
>>> string = 'test str'
>>> string[3:6]
't s'

#分割
>>> x
'fdasfsdfsdaf'
>>> x.split('a')
['fd', 'sfsdfsd', 'f']

 

列表

#新建一个列表
list_test = ['a','b','c','d']
#使用append()方法向列表中添加'aaa'
>>> list_test.append('aaa')
>>> list_test
['a', 'b', 'c', 'd', 'aaa']
#使用extend()方法添加
>>> list_test.extend('bbb')
>>> list_test
['a', 'b', 'c', 'd', 'aaa', 'b', 'b', 'b']
>>> a=[3,1,2,3,5,32]
#列表排序方法sort(),正向排序
>>> a.sort()
>>> a
[1, 2, 3, 3, 5, 32]
#列表排序方法reverse(),逆向排序
>>> a.reverse()
>>> a
[32, 5, 3, 3, 2, 1]
#remove方法,从列表中移除某元素
>>> a.remove(32)
>>> a
[5, 3, 3, 2]
#pop方法,移除列表某位置某元素
>>> a.pop[1]
#insert方法,在列表某位置插入某元素
>>>a.insert(1,1)
>>> a
[5, 1, 3, 2]
#del方法,删除
>>> del(a[2])
>>> a
[5, 1, 2]

 

字典:

#创建字典的几种方式
dic = {'a':'first','b':'second','c':'third'} #直接创建
>>> dic1 = dict(a='first',b='second',c='third')
 #通过dict关键字创建
>>> dic
{'a': 'first', 'b': 'second', 'c': 'third'}
>>> dic1
{'a': 'first', 'b': 'second', 'c': 'third'}
>>> dic2 = {k:0 for k in 'abcc'}
#python3 的新方式,字典解析
>>> dic2
{'a': 0, 'b': 0, 'c': 0}

del(dic['a'])
#字典中元素的删除
>>> dic
{'b': 'second', 'c': 'third'}
>>> dic.pop('b')
#pop方法,删除并且打印该元素
'second'
>>> dic
{'c': 'third'}

>>> dic['d']= 'fourth'
>>> dic
{'d': 'fourth', 'c': 'third'}
>>> dic1
{'a': 'first', 'b': 'second', 'c': 'third'}
>>> dic.update(dic1)
#update 将一个字典合并到另一个字典
>>> dic
{'d': 'fourth', 'a': 'first', 'b': 'second', 'c': 'third'}

 对字典进行排序

 1 >>> dic={'a':4,'b':3,'c':2,'d':1}  #建立一个字典
 2 >>> dic
 3 {'c': 2, 'b': 3, 'a': 4, 'd': 1}
 4 >>> sorted(dic.items())    #调用sorted函数进行排序
 5 [('a', 4), ('b', 3), ('c', 2), ('d', 1)]
 6 >>> sorted(dic.keys())  
 7 ['a', 'b', 'c', 'd']
 8 >>> sorted(dic.items(),key=lambda d:d[1])  
 9 #通过key这个参数,指定排序是按照value,也就是第一个元素d[1]的值来排序
10 [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
11 >>> sorted(dic.items(),key=lambda d:d[1],reverse=True) 
12 #reverse = True表示是需要翻转的,默认是从小到大,翻转的话,那就是从大到小。
13 [('a', 4), ('b', 3), ('c', 2), ('d', 1)]

元组

#建立一个元组
>>>tup =(2,3,4,5)
>>> tup
(2, 3, 4, 5)
#元组不能修改,所以要更改只能重新建立新的元组
>>> tup=(10,) + tup[1:]
>>> tup
(10, 3, 4, 5)
>>> tup=tup[:1]+(100,)+tup[2:]
>>> tup
(10, 100, 4, 5)

 

 

 

 

posted on 2013-10-31 20:42  kolaman  阅读(217)  评论(0)    收藏  举报