简介


数据结构是处理数据的结构,或者说,他们是用来存储一组相关数据的。

在Python中三种内建的数据结构--列表、元组和字典。学会了使用它们会使编程变得的简单。

列表


list是处理一组有序的数据结构,即你可以在一个列表中存储一个序列的项目。在Python每个项目之间用逗号分隔。
列表中的项目应该包括在方括号中,所以列表是一个可变的数据类型。

使用列表

shoplist = ['apple','mango','carrot','banana']

print('I have',len(shoplist),'items to purchase.')

print('These items are:',shoplist)
for item in shoplist:
print(item) print('I also have to buy rice.')
shoplist.append('rice')
print('My shopping list is now',shoplist) print('I will sort my list now')
shoplist.sort()
print('Sorted shopping list is',shoplist) print('The first item I will buy is',shoplist[0])
olditem = shoplist[0]
del shoplist[0]
print('I bought the',olditem)
print('My shopping list is now',shoplist) print(help('list'))

运行效果

元组


tuple

元祖和列表十分类似,只不过元祖和字符串一样是不可变的。
元祖使用圆括号用逗号分隔项目

使用元组

zoo = ('wolf','elephant','penguin')
print(type(zoo))
print('Number of animals in the zoo is',len(zoo)) new_zoo = ('monkey','dolphin',zoo)
print('Number of animals in the new zoo is',len(new_zoo))
print('All animals in new zoo are',new_zoo)
print('Animals brought from old zoo are',new_zoo[2])
print('Last animal brought from old zoo is',new_zoo[2][2]) print('==================================================')
#元祖与打印语句
age = 22
name = 'Swaroop' #%d表示整数%s表示字符串
print('%s is %d years old'%(name,age))
print('Why is %s playing with that python?'%name)

运行结果

print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同顺序来定制。

字典


以键值对的方式存储数据,键必须是唯一的,记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。
只能使用不可变对象来作为字典的键。
字典d={key1:value1,key2:value2}
字典是dict类的实例/对象

使用字典

a={
'Swaroop':'aaaaa',
'larry':'bbbbb',
'Mats':'ccccc'
}
print("Swaroop's value is %s" %a['Swaroop'])
a['Qing'] = 'asdasd' print(a) del a['Mats'] for name ,value in a.items():
print('Contact %s at %s'%(name,value)) if 'larry' in a:
print("larry's value is %s" %a['larry'])

运行结果

关键字参数与字典。

如果换一个角度看待你在函数中使用的关键字参数的话,你已经使用了字典了!只需想一下——你在函数定义的参数列表中使用的键/值对。当你在函数中使用变量的时候,它只不过是使用一个字典的键(这在编译器设计的术语中被称作 符号表 )。

序列


列表、元组和字符串都是序列,序列的两个主要特点是索引和切片,索引可以从序列中抓取一个特定的项目。

切片操作符使我们能够获取序列的一个切片(一部分序列)。

使用序列

#序列
'''列表、元组和字符串都是序列
序列的两个主要特点是索引和切片
索引可以从序列中抓取一个特定的项目。
切片使我们能够获取序列的一个切片(一部分序列)''' print(__doc__) shoplist = ['apple','mange','carrot','banana'] print('Item 0 is ',shoplist[0])
print('Item 1 is ',shoplist[1])
print('Item 2 is ',shoplist[2])
print('Item 3 is ',shoplist[3])
print('Item -1 is ',shoplist[-1])
print('Item -2 is ',shoplist[-2]) print('Item 1 to 3 is',shoplist[1:3])
print('Item 2 to end is',shoplist[2:])
print('Item 1 to -1 is',shoplist[1:-1])
print('Item start to end is',shoplist[:]) name = 'swaroop'
print('characters 1 to 3 is',name[1:3])
print('characters 2 to end is',name[2:])
print('characters 1 to -1 is',name[1:-1])
print('characters start to end is',name[:])

运行结果

print()换行问题


print(item,end=' ')

end就表示print将如何结束,默认为end="\n"(换行),只要让end不使用默认值"\n",就能阻止它换行。

对象与类的快速入门


列表是使用对象和类的一个例子。当你使用变量给它赋值的时候,比如i=5,你可以认为你创建了一个类型为int的对象i。事实上可以通过help(int)更好的理解这个概念。

从0开始的Python学习012数据结构&对象与类的更多相关文章

  1. python学习4—数据结构之列表、元组与字典

    python学习4—数据结构之列表.元组与字典 列表(list)深灰魔法 1. 连续索引 li = [1,1,[1,["asdsa",4]]] li[2][1][1][0] 2. ...

  2. Python学习笔记_Python对象

    Python学习笔记_Python对象 Python对象 标准类型 其它内建类型 类型对象和type类型对象 Python的Null对象None 标准类型操作符 对象值的比較 对象身份比較 布尔类型 ...

  3. Python学习day34-面向对象和网络编程总结

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. Python学习day26-面向对象之小结

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. Python学习day25-面向对象之组合,多态和封装

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  6. Python学习day24-面向对象的三大特征之继承

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  7. Python学习day23-面向对象的编程

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  8. 从0开始的Python学习017Python标准库

    简介 Python标准库使随着Python附带安装的,它包含很多有用的模块.所以对一个Python开发者来说,熟悉Python标准库是十分重要的.通过这些库中的模块,可以解决你的大部分问题. sys模 ...

  9. 从0开始的Python学习010return语句&DocStrings

    return语句 return语句用来从一个函数中 返回 即跳出函数.当然也可以从函数中返回一个值. #return 语句从一个函数返回 即跳出函数.我们也可选从函数返回一个值 def maximum ...

随机推荐

  1. maven下载及安装最详解

    maven的下载及安装 1.maven下载地址:https://maven.apache.org/download.cgi 2.将下载的安装包解压到自定义目录 3.配置环境变量 此电脑->右键属 ...

  2. Android中资源的引用

    R.java简单来说就是资源 R.java会自动收录当前应用中所有的资源,并根据这些资源建立对应的ID,包括:布局资源.控件资源.String资源.Drawable资源等 可以理解把所以资源按规则存放 ...

  3. SQL执行WebService

    写了一个钉钉发送消息的类, 要发送用友等审核单据信息, 模式: 钉钉发消息功能在webservice中, 用友消息列表中有新消息时,采用触发器执行webservice. 在测试中 ,功能正常 ,但将在 ...

  4. July 10th, 2018. Tuesday, Week 28th

    Winning isn't everything, but wanting it is. 胜利并不能代表一切,但求胜心可以. From Arnold Palmer. Compared to this ...

  5. ASP.NET Core 共享第三方依赖库部署的Bug(*.deps.json on 2.2.0 or 4.6.0 版本)

    背景: I try to put the Microsoft.*.dll and System.*.dll togather to a new folder.以便把(第三方或)系统的和应用的dll分开 ...

  6. C# 定时关机小程序

    1.打开VS2019,创建界面和按钮 2. 代码如下: private void button1_Click(object sender, EventArgs e) { downpc(txttime. ...

  7. window下的nginx的安装和使用

    nginx功能之一可以启动一个本地服务器,通过配置server_name和root目录等来访问目标文件 一. 下载 http://nginx.org/en/download.html 下载后安装在你钟 ...

  8. ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...

  9. DotNetCore跨平台~关于appsettings.json里各种配置项的读取

    回到目录 对于dotnet Core来说,依赖注入的集成无疑是最大的亮点,它主要用在服务注册与注入和配置文件注册与注入上面,我们一般会在程序入口先注册服务或者文件,然后在需要的地方使用注入即可,下面主 ...

  10. 浅谈JavaWeb架构演变

    一  JavaWeb架构演变 在java架构模式中,我们可以将MVC架构模式抽象为如下结构: 1.View层.View层即UI层,可采用的技术如JSP,Structs,SpringMVC等 2.Con ...