網(wǎng)站首頁 編程語言 正文
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
相關(guān)推薦
- 2022-12-08 C語言中-a++和-++a運(yùn)算順序?qū)嵗馕鯻C 語言
- 2023-01-29 Python?第三方庫?openpyxl?的安裝過程_python
- 2022-03-28 C++讀取wav文件中的PCM數(shù)據(jù)_C 語言
- 2022-10-30 淺析pytest?鉤子函數(shù)?之初始鉤子和引導(dǎo)鉤子_python
- 2022-09-01 Oracle?數(shù)據(jù)庫層級(jí)遍歷查詢功能的實(shí)現(xiàn)_oracle
- 2022-05-18 python?中賦值,深拷貝,淺拷貝的區(qū)別_python
- 2022-06-21 C#連接數(shù)據(jù)庫的幾種方法_C#教程
- 2022-07-18 RLS遞歸最小二乘法(Recursive Least Squares)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支