从0开始的Python学习012数据结构&对象与类
简介
数据结构是处理数据的结构,或者说,他们是用来存储一组相关数据的。
在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数据结构&对象与类的更多相关文章
- python学习4—数据结构之列表、元组与字典
python学习4—数据结构之列表.元组与字典 列表(list)深灰魔法 1. 连续索引 li = [1,1,[1,["asdsa",4]]] li[2][1][1][0] 2. ...
- Python学习笔记_Python对象
Python学习笔记_Python对象 Python对象 标准类型 其它内建类型 类型对象和type类型对象 Python的Null对象None 标准类型操作符 对象值的比較 对象身份比較 布尔类型 ...
- Python学习day34-面向对象和网络编程总结
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day26-面向对象之小结
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day25-面向对象之组合,多态和封装
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day24-面向对象的三大特征之继承
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day23-面向对象的编程
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 从0开始的Python学习017Python标准库
简介 Python标准库使随着Python附带安装的,它包含很多有用的模块.所以对一个Python开发者来说,熟悉Python标准库是十分重要的.通过这些库中的模块,可以解决你的大部分问题. sys模 ...
- 从0开始的Python学习010return语句&DocStrings
return语句 return语句用来从一个函数中 返回 即跳出函数.当然也可以从函数中返回一个值. #return 语句从一个函数返回 即跳出函数.我们也可选从函数返回一个值 def maximum ...
随机推荐
- 5.JAVA-内部类实例
在JAVA中,类内部可以添加其它类,当然也可以实现类继承(后续章节学习). 本章示例-实现部门类和雇员类 可以通过部门对象,查找该部门的雇员信息. 可以通过雇员对象,查找该雇员所在的部门信息 代码如下 ...
- 包装类及 LeetCode 每日一题
1.包装类与创建对象 Java 为8大数据类型都提供了相应的包装类,并提供属性和方法,更方便的操作基本数据类型.包装类位于java.lang包中. 对于这几种类型的基本数据,都有相似的方法实现基本数据 ...
- ORACLE字符集修改ORA-02374\ORA-12899\ORA-02372
IMPDP时部分日志显示这个警告ORA-02374: conversion error loading table "MEMXXX"."T_MEMBER_XXXX&quo ...
- SQLServer之创建链接服务器
创建链接服务器注意事项 当我们要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中就必须要创建远程数据库的DBLINK,通过DBLINNK数据库可以像访问本地数据库一样访问远程数据库表中的数据 ...
- Linux iptables用法与NAT
1.相关概念 2.iptables相关用法 3.NAT(DNAT与SNAT) 相关概念 防火墙除了软件及硬件的分类,也可对数据封包的取得方式来分类,可分为代理服务器(Proxy)及封包过滤机制(IP ...
- 从Android源码修改cpu信息
cpuinfo 网上的文章都是怎么查看/proc/cpuinfo,一直以为这种东西没法改呢,我还是太天真了./proc/cpuinfo是个文件,只读,想直接写肯定不行的.今天研究了一下,发现它的输出逻 ...
- windows安装IDEA
1.安装下载 https://www.jetbrains.com/idea/download/#section=windows 2.服务器激活地址: 39.129.9.34:8888 https:// ...
- Java中食之无味弃之可惜的数组
在Java的泛型出现之前,只有数组可以用来存储指定类型的对象:在自动装箱机制出现之前,只有数组可以用来存储基本数据类型:也就是说,在泛型和自动装箱机制出现之前,数组在Java当中的分量举足轻重. 况且 ...
- cAdvisor+Prometheus+Grafana监控docker
cAdvisor+Prometheus+Grafana监控docker 一.cAdvisor(需要监控的主机都要安装) 官方地址:https://github.com/google/cadvisor ...
- Python:logging.NullHandler 的使用
在使用 peewee 框架时,默认是不会出现日志消息的. from peewee import Model, CharField, DateTimeField, IntegerField from p ...