一口气python ABC
1 数据类型:
大括号-Dictionary,它定义了键和值之间一对一的关系。
中括号-List,类似java中的ArrayList 类,它可以保存任意对象,并且可以在增加新元素时动态扩展。
小括号-Tuple 是不可变的 list。一旦创建了一个 tuple,就不能以任何方式改变它。
han@han-desktop:~$ python 进入python环境
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d={name:eric han,age:30,favorate:soccer} 定义一个Dictionary类型的变量d
>>> d 显示变量d
{age: 30, favorate: soccer, name: eric han}
>>> l=[1,2,3] 定义一个List类型的变量l
>>> l
[1, 2, 3]
>>> t=(1,2,3) 定义一个Tuple类型的变量t
>>> t
(1, 2, 3)
>>> d[name] 根据key显示value
eric han
>>> d.values() 显示所有value
[30, soccer, eric han]
>>> d.items() 显示所有项
[(age, 30), (favorate, soccer), (name, eric han)]
>>> [myNum*2 for myNum in l] 遍历l,对每一项进行赋值
[2, 4, 6]
>>> l
[1, 2, 3]
>>> l=[myNum*2 for myNum in l]
>>> l
[2, 4, 6]
>>> l.extend([1,2,3])
>>> l
[2, 4, 6, 1, 2, 3]
>>> l.append(range(7)) range是内置函数,生成从0到6的一个list
>>> l
[2, 4, 6, 1, 2, 3, [0, 1, 2, 3, 4, 5, 6]]
>>> l.pop()
[0, 1, 2, 3, 4, 5, 6]
>>> l
[2, 4, 6, 1, 2, 3]
>>> l.remove(2)
>>> l
[4, 6, 1, 2, 3]
>>> l.remove(2)
>>> l
[4, 6, 1, 3]
>>> l[:-1] :左边省略了0,意为左边从0开始取,右边从1开始取
[4, 6, 1]
>>> type(d)
<type dict>
>>> type(l)
<type list>
>>> type(t)
<type tuple>
>>> l=range(10)
>>> getattr(l,"remove")(9)
>>> getattr(l,"append")(100)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 100]
补充:Web开发 , Python ,