标签:python
C:\Python27;C:\Python27\Tools\Scripts
print ‘Hello,python!‘
>>> type(5)
<type ‘int‘>
>>> type(‘hello‘)
<type ‘str‘>
>>> type([1,2,3])
<type ‘list‘>
>>> type({‘1‘:‘red‘,‘2‘:‘green‘})
<type ‘dict‘>
>>> type([1,‘python‘])
<type ‘list‘>
>>> type({1,‘python‘})
<type ‘set‘>
>>> type((1,2,3))
<type ‘tuple‘>
>>>                2)序列---一组按顺序排列的值,顺序很重要。Python中有三种序列类型:字符串(str),元组(tuple),>>> string[2:3] #返回第2个 ‘t‘ >>> string[2:4] #返回第2,3个 ‘th‘ >>>
string=‘python‘
>>> string
‘python‘
>>> string[0]=‘P‘
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    string[0]=‘P‘
TypeError: ‘str‘ object does not support item assignment              
字符串可以用,单引号,双引号,三引号来表示‘Python‘ "python" ‘‘‘i love python ‘‘‘len(‘python‘) #返回字符串的长度
>>> len(‘python‘) 6
>>> ‘hello‘+" Python" ‘hello Python‘4)元组
item=(1,‘cat‘,(1,2))空元组用() 表示
>>> type((1)) <type ‘int‘> >>> type((1,)) <type ‘tuple‘>由于元组的不可变性,这意味着创建后不能被修改。而list却可以的,所以list应用的更普遍。
>>> pets=(‘dog‘,‘cat‘,‘bird‘,‘dog‘) >>> ‘bird‘ in pets True >>> ‘cow‘ in pets False >>> len(pets) 4 >>> pets.count(‘dog‘) 2 >>> pets.count(‘cat‘) 1 >>> pets.index(‘dog‘) 0
>>> pets=[‘dog‘,‘cat‘,‘bird‘,‘dog‘] >>> type(pets) <type ‘list‘>
>>> color={‘red‘:1,‘blue‘:2,‘green‘:3}
>>> color[‘red‘]
1               注意,键是唯一的。若出现了两个相关的键,那么只会存储第后使用的那一对。>>> color={‘red‘:1,‘blue‘:2,‘green‘:3,‘red‘:4}
>>> color
{‘blue‘: 2, ‘green‘: 3, ‘red‘: 4}>>> color={‘red‘:1,‘blue‘:2,‘green‘:3}
>>> color.items()
[(‘blue‘, 2), (‘green‘, 3), (‘red‘, 1)]                       
d.keys()                #返回一个由字典的键组成的视图>>> color.keys() [‘blue‘, ‘green‘, ‘red‘]d.values() #返回一个由字典的值组成的视图
>>> color.values() [2, 3, 1]d.get(key) #返回与key相关联的值
>>> lst=[1,1,2,2,3,3,4,5,6] >>> s=set(lst) >>> s set([1, 2, 3, 4, 5, 6])
Python学习(一):入门篇:python中的一些数据结构,布布扣,bubuko.com
Python学习(一):入门篇:python中的一些数据结构
标签:python
原文地址:http://blog.csdn.net/jxlijunhao/article/details/24872581