百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Python基础篇-数据类型(python数据类型8种)

off999 2024-10-31 13:58 20 浏览 0 评论

目录

一、 数据类型介绍

二、 简单结构

三、 多维结构

·【列表】、元组、集合、字典

·列表、【元组】、集合、字典

·列表、元组、【集合】、字典

·列表、元组、集合、【字典】


一、数据类型介绍

Python中的数据类型主要包括:

1.整数(int):表示整数,例如:x = 2。

2.浮点数(float):表示带有小数点的数值,例如:y = 1.23。

3.布尔值(bool):表示真(True)或假(False)的值,用于逻辑运算,例如:is_true = True。

4.字符串(str):表示文本数据,可以使用单引号或双引号表示,例如:text = "Welcome, Python!"。

5.列表(list):有序的可变容器,可以包含不同数据类型的元素,例如:colorlist = [99, 88, 'red']。

6.元组(tuple):有序的不可变容器,例如:my_tuple = (3, 2, 'orange')。

7.集合(set):无序的可变容器,不允许重复元素,例如:my_set = {1, 2, 3}。

8.字典(dict):无序的键值对集合,例如:my_dict = {'name': 'Jay', 'age': 35}。

9.复数(complex):包含实部和虚部的数值,例如:z = 4 + 4j。

10.字节串(bytes):以字节为单位的不可变序列,例如:b = b'python'。

11.字节数组(bytearray):以字节为单位的可变序列,例如:ba = bytearray(b'python')。


简单结构:整数、浮点数、布尔值、字符串

多维结构:列表、元组、集合、字典

其他类型特定情况下才使用到,暂不介绍。

二、简单结构

使用一个简单例子介绍,有一个学生姓名为Judy,年龄是22,成绩是89.5,判断是否大于90分可以根据此构建四个变量模型age/score/boolScore/name(格式:整数/浮点数/布尔值/字符串),并进行格式化输出,同步输出变量模型对应格式,如代码所示。

# 整数
age = 22
# 浮点数
score = 89.5
# 布尔值 分数是否大于等于90
boolScore = True if score >= 90 else False
# 字符串
name = "Judy"

print(f"age:{age},score:{score},boolScore:{boolScore},name:{name}")
print(f"age:{type(age)},score:{type(score)},boolScore:{type(boolScore)},name:{type(name)}")

>>>runfile('C:/xxx/learnvar.py',wdir='C:/xxx/learn')
age:22,score:89.5,boolScore:False,name:Judy
age:<class 'int'>,score:<class 'float'>,boolScore:<class 'bool'>,name:<class 'str'>

三、多维结构

·【列表】、元组、集合、字典

# --------------------------------------------

# 列表(list):有序的可变容器,可以包含不同数据类型的元素

# type(colorslist) <class 'list'>

# --------------------------------------------

列表基本操作,索引访问列表

>numbers = [1,2,3,4]
>print(numbers[1],numbers[0:2],numbers[0:],"Bob" in numbers)
2 [1, 2] [1, 2, 3, 4] False

列表迭代,比如输出偶数

for number in numbers:
if number % 2 == 0:
print(number) # 返回 2 4

列表修改

>colors = ['red','yellow','green','blue']
>print(colors)
>colors[0] = "burgundy" # 索引修改
>colors[1:3] = ["orange","magenta"] # 切片赋值
>print(colors)
>colors[1:3] = ["orange","magenta","aqua"]
>print(colors)
['red', 'yellow', 'green', 'blue']
['burgundy', 'orange', 'magenta', 'blue']
['burgundy', 'orange', 'magenta', 'aqua', 'blue']

列表增加list.insert

>colors = ['red','yellow','green','blue']
>print(colors)
>colors.insert(1,"orange") # 在索引1位置插入
>print(colors)
>colors.insert(10,"violet") # 超过索引位置插入,直接插到末尾
>print(colors)
>colors.insert(-1,"indigo") # 插入倒数第一个
>print(colors)
['red', 'yellow', 'green', 'blue']
['red', 'orange', 'yellow', 'green', 'blue']
['red', 'orange', 'yellow', 'green', 'blue', 'violet']
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

列表删除list.pop,append末尾增加

>colors.pop(3)
>print(colors)
>colors.pop(-1) # 负值索引
>print(colors)
>colors.pop() # 删除最后一个
>print(colors)
>colors.append("indigo")
>print(colors)
['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']
['red', 'orange', 'yellow', 'blue', 'indigo']
['red', 'orange', 'yellow', 'blue']
['red', 'orange', 'yellow', 'blue', 'indigo']

列表统计

>nums = [1,2,3,4,5]
>print(sum(nums),max(nums),min(nums))

列表扩展列表推导式

>numbers = (1,2,3,4,5)
>squares = [num**2 for num in numbers]
>print(numbers,squares)
15 5 1
(1, 2, 3, 4, 5) [1, 4, 9, 16, 25]

·列表、【元组】、集合、字典

# --------------------------------------------

# 元组(tuple):有序的不可变容器

# type(first_tuple) <class 'tuple'>

# --------------------------------------------

元组定义和输出,类型,长度,切片访问

>first_tuple = (1,2,3)
>print(first_tuple,type(first_tuple),len(first_tuple),first_tuple[0],first_tuple[0:3])
(1, 2, 3) <class 'tuple'> 3 1 (1, 2, 3)

元组遍历,迭代

> for item in first_tuple:
> print(item*2) # 返回 2 4 6

元组 打包解包 = 两端元素数量相等,批量赋值

coordinates = 5.21,8.29
x,y = coordinates
a,b,c,d = 1,2,3,4

元组使用in检查是否包含某值

coo = tuple('coordinates')
print("o" in coo) # True
print("a" in coo) # True
print("z" in coo) # False

·列表、元组、【集合】、字典

# --------------------------------------------

# 集合(set):无序的可变容器,不允许重复元素

# type(s) <class 'set'>

# --------------------------------------------

集合创建一个集合

>s = {1, 2, 3, 4, 5}
>print(s)
{1, 2, 3, 4, 5}

集合添加元素

>s.add(6)
>print(s)
{1, 2, 3, 4, 5, 6}

集合移除元素

>s.remove(3)
>print(s)
{1, 2, 4, 5, 6}

集合的并集操作

>s1 = {3, 4, 5}
>unions = s | s1
>print(s,s1,unions)
{1, 2, 4, 5, 6} {3, 4, 5} {1, 2, 3, 4, 5, 6}

集合的交集操作

>intersections = s & s1
>print(s,s1,intersections)
{1, 2, 4, 5, 6} {3, 4, 5} {4, 5}

集合的差操作

>differences = s - s1
>print(s,s1,differences)
{1, 2, 4, 5, 6} {3, 4, 5} {1, 2, 6}

集合的对称差操作(异或运算)

>symmetric_differences = s ^ s1
>print(s,s1,symmetric_differences)
{1, 2, 4, 5, 6} {3, 4, 5} {1, 2, 3, 6}

集合的成员检查

>print(2 in s) # 返回 True

集合的长度

>print(len(s),len(s1)) # 返回5,3

集合的遍历

for item in s1:
print(item) # 返回 3 4 5

·列表、元组、集合、【字典】

# --------------------------------------------

# 字典(dict):无序的键值对集合,列表不能作为字典的键

# type(capitals) <class 'dict'>

# --------------------------------------------

字典 创建字典

>capitals = {
> "California":"Sacramento",
> "New York":"Albany",
> "Texas":"Austin",
>}
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Austin'}

字典增加

>capitals["Colorado"] = "Denver"
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Austin', 'Colorado': 'Denver'}

字典修改

>capitals["Texas"] = "Houston"
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Houston', 'Colorado': 'Denver'}

字典删除

>del capitals["Texas"]
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Colorado': 'Denver'}

字典键判断

>print("Arizona" in capitals,"California" in capitals)
False True

字典通过key,value,items()访问读取

>for key in capitals:
> print(key)
California
New York
Colorado

>for state in capitals:
> print(f"The capital of {state} is {capitals[state]}")
The capital of California is Sacramento
The capital of New York is Albany
The capital of Colorado is Denver

>for state,capital in capitals.items():
> print(f"The capital of {state} is {capital}")
The capital of California is Sacramento
The capital of New York is Albany
The capital of Colorado is Denver


下篇预告:python基础篇-函数

相关推荐

推荐一款Python的GUI可视化工具(python 可视化工具)

在Python基础语法学习完成后,进一步开发应用界面时,就需要涉及到GUI了,GUI全称是图形用户界面(GraphicalUserInterface,又称图形用户接口),采用图形方式显示的计算机操...

教你用Python绘制谷歌浏览器的3种图标

前两天在浏览matplotlib官方网站时,笔者无意中看到一个挺有意思的图片,就是用matplotlib制作的火狐浏览器的logo,也就是下面这个东东(网页地址是https://matplotlib....

小白学Python笔记:第二章 Python安装

Windows操作系统的python安装:Python提供Windows、Linux/UNIX、macOS及其他操作系统的安装包版本,结合自己的使用情况,此处仅记录windows操作系统的python...

Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字

Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字一、项目功能利用Tkinter组件中的Canvas绘制图形和文字。二、项目分析要在窗体中绘制图形和文字,需先导入Tkinter组...

一文吃透Python虚拟环境(python虚拟环境安装和配置)

摘要在Python开发中,虚拟环境是一种重要的工具,用于隔离不同项目的依赖关系和环境配置。本文将基于windows平台介绍四种常用的Python虚拟环境创建工具:venv、virtualenv、pip...

小白也可以玩的Python爬虫库,收藏一下

最近,微软开源了一个项目叫「playwright-python」,作为一个兴起项目,出现后受到了大家热烈的欢迎,那它到底是什么样的存在呢?今天为你介绍一下这个传说中的小白神器。Playwright是...

python环境安装+配置教程(python安装后怎么配置环境变量)

安装python双击以下软件:弹出一下窗口需选择一些特定的选项默认选项不需要更改,点击next勾选以上选项,点击install进度条安装完毕即可。到以下界面,证明安装成功。接下来安装库文件返回电脑桌面...

colorama,一个超好用的 Python 库!

大家好,今天为大家分享一个超好用的Python库-colorama。Github地址:https://github.com/tartley/coloramaPythoncolorama库是一...

python制作仪表盘图(python绘制仪表盘)

今天教大家用pyecharts画仪表盘仪表盘(Gauge)是一种拟物化的图表,刻度表示度量,指针表示维度,指针角度表示数值。仪表盘图表就像汽车的速度表一样,有一个圆形的表盘及相应的刻度,有一个指针...

总结90条写Python程序的建议(python写作)

  1.首先  建议1、理解Pythonic概念—-详见Python中的《Python之禅》  建议2、编写Pythonic代码  (1)避免不规范代码,比如只用大小写区分变量、使用容易...

[oeasy]python0137_相加运算_python之禅_import_this_显式转化

变量类型相加运算回忆上次内容上次讲了是从键盘输入变量input函数可以有提示字符串需要有具体的变量接收输入的字符串输入单个变量没有问题但是输入两个变量之后一相加就非常离谱添加图片注释,不超过1...

Python入门学习记录之一:变量(python中变量的规则)

写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...

掌握Python的&quot;魔法&quot;:特殊方法与属性完全指南

在Python的世界里,以双下划线开头和结尾的"魔法成员"(如__init__、__str__)是面向对象编程的核心。它们赋予开发者定制类行为的超能力,让自定义对象像内置类型一样优雅工...

11个Python技巧 不Pythonic 实用大于纯粹

虽然Python有一套强大的设计哲学(体现在“Python之禅”中),但总有一些情况需要我们“打破规则”来解决特定问题。这触及了Python哲学中一个非常核心的理念:“实用主义胜于纯粹主义”...

Python 从入门到精通 第三课 诗意的Python之禅

导言:Python之禅,英文名是TheZenOfPython。最早由TimPeters在Python邮件列表中发表,它包含了影响Python编程语言设计的20条软件编写原则。它作为复活节彩蛋...

取消回复欢迎 发表评论: