设为首页 加入收藏

TOP

从0开始的Python学习012数据结构&对象与类
2019-03-29 10:14:47 】 浏览:42
Tags:开始 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)更好的理解这个概念。

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CTF编程题-三羊献瑞(实验吧)解.. 下一篇【Python3爬虫】最新的模拟登录新..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目