日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python中的基本數(shù)據(jù)類型介紹_python

作者:小旭2021 ? 更新時(shí)間: 2022-08-25 編程語言

Python 中主要有8種數(shù)據(jù)類型:number(數(shù)字)、string(字符串)、list(列表)、tuple(元組)、dict(字典)、set(集合)、Boolean(布爾值)、None(空值)。

其中Python有六個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)類型:

1、字符串

字符串的聲明有三種方式:單引號(hào)、雙引號(hào)和三引號(hào)(包括三個(gè)單引號(hào)或三個(gè)雙引號(hào))

>>> 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

2、數(shù)字

Python3 支持三種不同的數(shù)值類型:

整型(int):通常被稱為是整型或整數(shù),是正或負(fù)整數(shù),不帶小數(shù)點(diǎn)。Python3 整型是沒有限制大小的,可以當(dāng)作 Long 類型使用,所以 Python3 沒有 Python2 的 Long 類型。

浮點(diǎn)型(float):浮點(diǎn)型由整數(shù)部分與小數(shù)部分組成,浮點(diǎn)型也可以使用科學(xué)計(jì)數(shù)法表示 。

復(fù)數(shù)( (complex)):復(fù)數(shù)由實(shí)數(shù)部分和虛數(shù)部分構(gòu)成,可以用a + bj,或者complex(a,b)表示, 復(fù)數(shù)的實(shí)部a和虛部b都是浮點(diǎn)型。

3、列表

列表是一種可修改的集合類型,其元素可以是數(shù)字、string等基本類型,也可以是列表、元組、字典等集合對(duì)象,甚至可以是自定義的類型。其定義方式如下:

>>> 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']]

4、元組

元組類型和列表一樣,也是一種序列,與列表不同的是,元組是不可修改的。元組的聲明如下:

lst = (0,1,2,2,2)
lst1=("hello",)
lst2 = ("hello")
print type(lst1) #<type 'tuple'> 只有一個(gè)元素的情況下后面要加逗號(hào) 否則就是str類型
print type(lst2) #<type 'str'>

5、字典

字典是另一種可變?nèi)萜髂P停铱纱鎯?chǔ)任意類型對(duì)象。字典的每個(gè)鍵值 key=>value 對(duì)用冒號(hào) : 分割,每個(gè)鍵值對(duì)之間用逗號(hào) , 分割,整個(gè)字典包括在花括號(hào) {} 中 ,格式如下所示:

>>>dict = {'a': 1, 'b': 2, 'b': '3'}
>>> dict['b']
'3'
>>> dict
{'a': 1, 'b': '3'}

6、集合

集合(set)是一個(gè)無序的不重復(fù)元素序列。可以使用大括號(hào) { } 或者 set() 函數(shù)創(chuàng)建集合。

注意:創(chuàng)建一個(gè)空集合必須用 set() 而不是 { },因?yàn)?{ } 是用來創(chuàng)建一個(gè)空字典。創(chuàng)建格式:

a={'a','b','c','d'}
b=set('abcdefabcd')
c=set({'a':1,'b':2})
d=set(['a','b','c','a'])
print(a,type(a))
print(b,type(b))
print(c,type(c))
print(d,type(d))
 
#運(yùn)行結(jié)果
{'c', 'd', 'b', 'a'} <class 'set'>
{'f', 'e', 'b', 'c', 'd', 'a'} <class 'set'>
{'b', 'a'} <class 'set'>
{'c', 'b', 'a'} <class 'set'>

原文鏈接:https://www.cnblogs.com/chenyablog/p/15164800.html

欄目分類
最近更新