Number(数字) 包括int,long,float,complex
String(字符串) 例如:hello,"hello",hello
List(列表) 例如:[1,2,3],[1,2,3,[1,2,3],4]
Dictionary(字典) 例如:{1:"nihao",2:"hello"}
Tuple(元组) 例如:(1,2,3,abc)
Bool(布尔) 包括True、False
i=100
int i = 100;
>>> i = 10 >>> type(i) <type 'int'> >>> i=10000000000 >>> type(i) <type 'long'>
>>> 2**31-1 2147483647L >>> sys.maxint 2147483647
>>> type(2147483647) <type 'int'> >>> type(2147483648) <type 'long'>
>>> i = 10000.1212 >>> type(i) <type 'float'>
>>> str1 = 'hello world' >>> str2 = "hello world" >>> str3 = '''hello world''' >>> str4 = """hello world""" >>> print str1 hello world >>> print str2 hello world >>> print str3 hello world >>> print str4 hello world
>>> str1 = "hello" >>> print str1 hello >>> str2 = u"中国" >>> print str2 中国
u = u'汉'
print repr(u) # u'\u6c49'
s = u.encode('UTF-8')
print repr(s) # '\xe6\xb1\x89'
u2 = s.decode('UTF-8')
print repr(u2) # u'\u6c49'
f = codecs.open("d:/test.txt")
content = f.read()
f.close()
print content
# -*- coding: utf-8 -*-
import codecs
f = codecs.open("d:/test.txt")
content = f.read()
f.close()
if isinstance(content,unicode):
print content.encode('utf-8')
print "utf-8"
else:
print content.decode('gbk').encode('utf-8')
>>> nums = [1,2,3,4] >>> type(nums) <type 'list'> >>> print nums [1, 2, 3, 4] >>> strs = ["hello","world"] >>> print strs ['hello', 'world'] >>> lst = [1,"hello",False,nums,strs] >>> type(lst) <type 'list'> >>> print lst [1, 'hello', False, [1, 2, 3, 4], ['hello', 'world']]
>>> lst = [1,2,3,4,5] >>> print lst[0] 1 >>> print lst[-1] 5 >>> print lst[-2] 4
nums = [1,2,3,4,5] print nums[0:3] #[1, 2, 3] #前三个元素 print nums[3:] #[4, 5] #后两个元素 print nums[-3:] #[3, 4, 5] #后三个元素 不支持nums[-3:0] numsclone = nums[:] print numsclone #[1, 2, 3, 4, 5] 复制操作 print nums[0:4:2] #[1, 3] 步长为2 nums[3:3] = ["three","four"] #[1, 2, 3, 'three', 'four', 4, 5] 在3和4之间插入 nums[3:5] = [] #[1, 2, 3, 4, 5] 将第4和第5个元素替换为[] 即删除["three","four"]
lst1 = ["hello","world"] lst2 = ['good','time'] print lst1+lst2 #['hello', 'world', 'good', 'time'] print lst1*5 #['hello', 'world', 'hello', 'world', 'hello', 'world', 'hello', 'world', 'hello', 'world']
>>> [x for x in dir([]) if not x.startswith("__")]
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
def compare(x,y):
return 1 if x>y else -1
#【append】 在列表末尾插入元素
lst = [1,2,3,4,5]
lst.append(6)
print lst #[1, 2, 3, 4, 5, 6]
lst.append("hello")
print lst #[1, 2, 3, 4, 5, 6]
#【pop】 删除一个元素,并返回此元素的值 支持索引 默认为最后一个
x = lst.pop()
print x,lst #hello [1, 2, 3, 4, 5, 6] #默认删除最后一个元素
x = lst.pop(0)
print x,lst #1 [2, 3, 4, 5, 6] 删除第一个元素
#【count】 返回一个元素出现的次数
print lst.count(2) #1
#【extend】 扩展列表 此方法与“+”操作的不同在于此方法改变原有列表,而“+”操作会产生一个新列表
lstextend = ["hello","world"]
lst.extend(lstextend)
print lst #[2, 3, 4, 5, 6, 'hello', 'world'] 在lst的基础上扩展了lstextend进来
#【index】 返回某个值第一次出现的索引位置,如果未找到会抛出异常
print lst.index("hello") #5
#print lst.index("kitty") #ValueError: 'kitty' is not in list 出现异常
#【remove】 移除列表中的某个元素,如果待移除的项不存在,会抛出异常 无返回值
lst.remove("hello")
print lst #[2, 3, 4, 5, 6, 'world'] "hello" 被移除
#lst.remove("kitty") #ValueError: list.remove(x): x not in list
#【reverse】 意为反转 没错 就是将列表元素倒序排列,无返回值
print lst #[2, 3, 4, 5, 6, 'world']
lst.reverse()
print lst #[2, 3, 4, 5, 6, 'world']
#【sort】 排序
print lst #由于上面的反转 目前排序为 ['world', 6, 5, 4, 3, 2]
lst.sort()
print lst #排序后 [2, 3, 4, 5, 6, 'world']
nums = [10,5,4,2,3]
print nums #[10,5,4,2,3]
nums.sort(compare)
print nums #[2, 3, 4, 5, 10]
lst = [1,2,3,4,5]
lstiter = iter(lst)
print [x for x in dir(numiter) if not x.startswith("__")]
>>>['next']
lst = [1,2,3,4,5] lstiter = iter(lst) for i in range(len(lst)): print lstiter.next() #依次打印 1 2 3 4 5
lst = (0,1,2,2,2)
lst1=("hello",)
lst2 = ("hello")
print type(lst1) #<type 'tuple'> 只有一个元素的情况下后面要加逗号 否则就是str类型
print type(lst2) #<type 'str'>
dict1 = {}
print type(dict1) #<type 'dict'> 声明一个空字典
dict2 = {"name":"kitty","age":18} #直接声明字典类型
dict3 = dict([("name","kitty"),("age",18)]) #利用dict函数将列表转换成字典
dict4 = dict(name='kitty',age=18) #利用dict函数通过关键字参数转换为字典
dict5 = {}.fromkeys(["name","age"]) #利用fromkeys函数将key值列表生成字典,对应的值为None {'age': None, 'name': None}
#【添加元素】
dict1 = {}
dict1["mykey"] = "hello world" #直接给一个不存在的键值对赋值 即时添加新元素
dict1[('my','key')] = "this key is a tuple" #字典的键可以是任何一中不可变类型,例如数字、字符串、元组等
#【键值对个数】
print len(dict1)
#【检查是否含有键】
print "mykey" in dict1 #True 检查是否含有键为mykey的键值对
print "hello" in dict1 #False
#【删除】
del dict1["mykey"] #删除键为mykey的键值对
>>> [x for x in dir({}) if not x.startswith("__")]
['clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues',
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
print bool(0) #False
print bool(1) #True
print bool(-1) #True
print bool([]) #False
print bool(()) #False
print bool({}) #False
print bool('') #False
print bool(None) #False
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有