标题列表的创建
数值类型:int float (long)
布尔型
字符串
列表(list)
# 数组:存储同一种数据类型的集和 scores=[1,2,33,44]
# 列表(打了激素的数组):可以存储任意数据类型的集和
# li = [1,2.2,True,'hello']
# print(li,type(li))
#
#
# # 列表里面是可以嵌套列表的
# li = [1,2,3,False,'python',[1,2,3,4,5]]
# print(li,type(li))
import random
li = list(range(10))
random.shuffle(li)
print(li)
列表的特性
service = ['http','ssh','ftp']
# 索引
# 正向索引
print(service[0])
# 反向索引
print(service[-1])
# 切片
print(service[::-1]) # 列表的反转
print(service[1::]) # 除了第一个之外的其他元素
print(service[:-1]) # 除了最后一个之外的其他元素
# 重复
print(service * 3)
# 连接
service1 = ['mysql','firewalld']
print(service + service1)
# 成员操作符
print('firewalld' in service)
print('ftp' in service)
print('firewalld' not in service)
print('ftp' not in service)
# 列表里面嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]
# 索引
print(service2[0][0])
print(service2[-1][1])
列表的练习
根据用于指定月份,打印该月份所属的季节。提示: 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
month = int(input('Month:'))
if month in [3,4,5]:
print('春季')
elif month in [6,7,8]:
print('夏季')
elif month in [9,10,11]:
print('秋季')
else:
print('冬季')
假设有下面这样的列表:
names = [‘fentiao’,‘fendai’,‘fensi’,‘fish’]
输出的结果为:'I have fentiao,fendai,fensi and fish’
names = ['fentiao','fendai','fensi','fish']
print('I have ' + ','.join(names[:-1]) + ' and ' + names[-1])
stack = [1,2,3,4]
栈的工作原理:先进后出
入栈
出栈
栈顶元素
栈的长度
栈是否为空
队列的工作原理:先进先出
入队
出队
队头
队尾
队列是否为空
显示队列元素
stack = []
info = """
栈操作
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空
"""
while True:
print(info)
choice = input('请输入选择:')
if choice == '1':
item = input('入栈元素:')
stack.append(item)
print('元素%s入栈成功' %(item))
elif choice == '2':
# 先判断栈是否为空
if not stack:
print('栈为空,不能出栈')
else:
item = stack.pop()
print('元素%s出栈成功' %(item))
elif choice == '3':
if len(stack) == 0:
print('栈为空,无栈顶元素')
else:
print('栈顶元素为%s' %(stack[-1]))
elif choice == '4':
print('栈的长度为%s' %(len(stack)))
elif choice == '5':
if len(stack) == 0:
print('栈为空')
else:
print('栈不为空')
elif choice == 'q':
print('欢迎下次使用')
break
else:
print('请输入正确的选择')
列表元素的增加
service = ['http', 'ssh', 'ftp']
# 1.
# print(service + ['firewalld'])
# append:追加,追加一个元素到列表中
service.append('firewalld')
print(service)
# extend:拉伸,追加多个元素到列表中
service.extend(['hello','python'])
print(service)
# insert:在索引位置插入元素
service.insert(0,'firewalld')
print(service)
列表元素的删除
service = ['http', 'ssh', 'ftp']
# remove
# service.remove('ftp')
# print(service)
# service.remove('https')
# print(service)
# claer:清空列表里面的所有元素
# service.clear()
# print(service)
# del(python关键字) 从内存中删除列表
# del service
# print(service)
# print('删除列表第一个索引对应的值:',end='')
# del service[1]
# print(service)
print('删除前两个元素之外的其他元素:',end='')
del service[2:]
print(service)
列表元素的修改
service = ['ftp','http', 'ssh', 'ftp']
# 通过索引,重新赋值
# service[0] = 'mysql'
# print(service)
# 通过slice(切片)
service[:2] = ['mysql','firewalld']
print(service)
列表元素的查看
# service = ['ftp','http', 'ssh', 'ftp','ssh','ssh']
#
# # 查看元素出现的次数
# print(service.count('ftp'))
#
# # 查看制定元素的索引值(也可以指定范围)
# print(service.index('ssh'))
# print(service.index('ssh',4,8))
#
# # 排序查看(按照ascii码进行排序)
# print(service.sort(reverse=True))
# print(service)
#
#
# # 对字符串排序不区分大小写
# phones = ['alice','bob','harry','Borry']
# # phones.sort(key=str.lower)
# phones.sort(key=str.upper)
# print(phones)