简介


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

在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. openlayers4 入门开发系列之地图属性查询篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  2. SqlServer中的系统数据库

    SqlServer中的系统数据库有五个,平时写代码不太关注,今天一时兴起研究了一下. 1. master 记录SQL Server系统的所有系统级信息,例如:登陆账户信息.链接服务器和系统配置设置.记 ...

  3. windows dll的def文件

    DLL(testcase_1.dll )源码:myfun.h #pragma once #ifdef TESTCASE_1_EXPORTS #define MY_API __declspec(dlle ...

  4. nginx 安装、启动、重启、关闭 (linux系统命令行)

    前言: 最近在部署我的hexo静态博客到腾讯云服务器上,用到了很多nginx的知识,在此做下总结: 刚接触的linux服务器上,nginx配置乱的有点令人发指,就把老的卸载了重新装一下. 1.卸载 y ...

  5. caffe安装教程(Ubuntu14+GPU+pycaffe+anaconda2)

    caffe安装教程 本文所使用的底层环境配置:cuda8.cudnn6.OpenCV2.4.5.anaconda2(Python2.7).如使用其他版本的环境,如cuda,可安装自己的版本,但须在相应 ...

  6. C/C++中extern和static

    目录 1 extern概念 2 extern作用 2.1 变量声明 2.2 变量定义 2.3 声明和定义举例 3 为什么使用extern 4 怎么使用extern 4.1 基本数据类型定义变量 4.2 ...

  7. 小白都会超详细--ELK日志管理平台搭建教程

    目录 一.介绍 二.安装JDK 三.安装Elasticsearch 四.安装Logstash 五.安装Kibana 六.Kibana简单使用 系统环境:CentOS Linux release 7.4 ...

  8. Spring Boot 中的静态资源到底要放在哪里?

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...

  9. 知识小罐头04(idea+maven+部署war包到tomcat 下)

    上一节新建了一个基本的maven web环境,现在最快速度新建一下springmvc环境! 1.最快搭建springmvc环境 什么叫做最快,当然是怎么简单怎么来啦!由于内容都是很熟悉的东西,serv ...

  10. Java集合与泛型中的几个陷阱,你掉进了几个?

    下面我总结了集合.泛型.数组转集合等一些常见的陷进,认真看完,相信你绝对有所收获. 1.List ,List<?> 与 List<Object> 有区别吗? 说实话,我敢保证很 ...