Python のデータ型には 8 種類あります。数値型 (int およびlong)、float 型、複合型 (complex)、string 型、list 型、tuple 型、dictionary 型、Boolean 型 (True および False) )。
#Python のデータ型には次の 8 種類があります
#数値型
intとlong
intとlongを一緒にする理由は、python3.x以降はintとlongの区別がなくなり、一律にintが使用されるためです。 python2.x はまだ異なります。 Python2.7 を例に挙げます:>>> 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'>
float型
float型になります。浮動小数点数は端的に言えば小数点を持つ数値であり、その精度はマシンに依存します。例:>>> i = 10000.1212 >>> type(i) <type 'float'>
complex: 複数形。 の具体的な意味と使用法については、関連ドキュメントを確認してください。
文字列型
文字列を宣言するには、一重引用符、二重引用符、三重引用符 (3 つの一重引用符または 3 つの二重引用符を含む) の 3 つの方法があります。例:>>> 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' 解释:声明unicode字符串”汉“,它的unicode编码为”\u6c49“,经过utf-8编码转换后,它的编码变成”\xe6\xb1\x89“。
コーディング経験の要約:
1. Python ファイルのヘッダーでエンコード形式を宣言します;#-*- coding: utf-8 -*-
f = codecs.open("d:/test.txt") content = f.read() f.close() print content
Change 次の方法を使用します (中国語でのみ機能します):
# -*- 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')
List type
リストは変更可能なコレクション型であり、その要素は数値や文字列などの基本型、リスト、タプル、辞書などのコレクション オブジェクト、さらにはカスタム型にすることができます。次のように定義されます:>>> 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] >>> print lst[-1] >>> print lst[-2]
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'>
辞書型
辞書型は、Dictionary