Python 使用list和tuple+条件判断详解

作者:cznczai 时间:2022-05-13 16:36:39 

list

list是一种有序的集合,可以随时添加和删除其中的元素。跟java不一样的是 可以使用arr[-1] 0>-x >=- len(arr) 索引的数字为 0~ len(arr)-1 -len(arr)~ -1 超过会报错


classmates= ['A','B','C','D','E']
print(classmates)
print(len(classmates))
for i in classmates:
 print(i)

'''
['A', 'B', 'C', 'D', 'E']
5
A
B
C
D
E
'''
for i in range(0,len(classmates)):
 print(classmates[i])
'''
A
B
C
D
E
'''
classmates= ['A','B','C','D','E']
print(classmates[-1])
print(classmates[-2])
print(classmates[-3])
print(classmates[-4])
print(classmates[-5])
'''
print(classmates[-6])
IndexError: list index out of range
'''
'''
E
D
C
B
A
'''

list是一个可变的有序表,所以,可以往list中追加元素到末尾:

append()方法

在某个位置上插入

insert(x,'???')

要删除list末尾的元素

pop(i) 没有参数默认为最后一个


classmates= ['A','B','C','D','E']
classmates.append('F')
classmates.insert(1,'G')
classmates.insert(-1,'H')
classmates.insert(1,'I')
print(classmates)
for i in range(0,len(classmates)):
 classmates.pop(0)
 print(classmates)
'''
['A', 'I', 'G', 'B', 'C', 'D', 'E', 'H', 'F']
['I', 'G', 'B', 'C', 'D', 'E', 'H', 'F']
['G', 'B', 'C', 'D', 'E', 'H', 'F']
['B', 'C', 'D', 'E', 'H', 'F']
['C', 'D', 'E', 'H', 'F']
['D', 'E', 'H', 'F']
['E', 'H', 'F']
['H', 'F']
['F']
[]

要把某个元素替换成别的元素,可以直接赋值给对应的索引位置:更改值任意属性


classmates= ['A','B','C','D','E']
classmates[0] ='F'
print(classmates)
'''
['F', 'B', 'C', 'D', 'E']
'''

list元素也可以是另一个list,list里面的元素的数据类型也可以不同


p = [1,2,3]
classmates= ['A',p,True ]
print(classmates)

tuple

tuple和list非常类似,但是tuple一旦初始化就不能修改,更具有安全性 相比list 没有append() pop() insert()等方法

classmates = ('Michael', 'Bob', 'Tracy') 跟list 区别是括号 [] / ()

注意:因为定义tuple 类型跟 四则运算的() 相互混了


false_t = (1)      # 定义的不是tuple,是1这个数!
print(false_t)    
true_t = (1,)      # 只有1个元素的tuple定义时必须加一个逗号,,来消除歧义:
print(true_t)
true_t1 = ()
print(true_t1)
'''
1
(1,)
()

'''

虽然说tuple不可变 如下


p = ['男',20]
change_t = (1,'czncai',p)
print(change_t)
p[0] = ' * '
p[1] = 21
print(change_t)
'''
(1, 'czncai', ['男', 20])
(1, 'czncai', [' * ', 21])
'''

 Python 使用list和tuple+条件判断详解

条件判断 + input()

input('请输入一个数字')
if boolean表达式 :
elif :
else :

循环

for x in list :

是把list每个元素代入变量x,然后执行缩进块的语句

for x in range() range()函数,可以生成一个整数序列


list(range(5))
[0, 1, 2, 3, 4]

while boolean表达式 :


n = n+1
b = true

break continue

  • break 可以提前退出循环

  • continue 跳过当前的这次循环,直接开始下一次循环。

来源:https://www.cnblogs.com/cznczai/p/11268013.html

标签:python,list,tuple,条件,判断
0
投稿

猜你喜欢

  • 基于python批量处理dat文件及科学计算方法详解

    2021-05-16 18:00:12
  • php让json_encode不自动转义斜杠“/”的方法

    2023-08-19 17:04:28
  • 恢复被删除的数据 Log Explorer for SQL Server 4.2 (一)

    2010-07-01 19:24:00
  • PyQT5 QTableView显示绑定数据的实例详解

    2023-09-09 19:40:40
  • python二叉树遍历的实现方法

    2021-09-19 03:53:14
  • Python基于有道实现英汉字典功能

    2021-05-23 19:35:57
  • django反向解析和正向解析的方式

    2021-11-21 11:03:49
  • sina和265天气预报调用代码

    2007-11-19 13:32:00
  • python转化excel数字日期为标准日期操作

    2021-01-14 22:38:59
  • Python中的协程(Coroutine)操作模块(greenlet、gevent)

    2021-01-17 12:24:25
  • SQL Server与Oracle、DB2的优劣对比

    2009-01-07 14:16:00
  • 使用Pytorch+PyG实现MLP的详细过程

    2023-05-03 17:48:14
  • OpenCV2.3.1+Python2.7.3+Numpy等的配置解析

    2021-12-09 16:04:57
  • GitHub上值得推荐的8个python 项目

    2021-01-11 22:40:12
  • django定期执行任务(实例讲解)

    2022-12-13 20:43:35
  • Python线程之如何解决共享变量问题

    2023-08-27 16:15:56
  • 使用Pycharm+PyQt5弹出子窗口的程序代码

    2022-03-09 20:15:17
  • 实例讲解Oracle到SQL Server主键迁移

    2009-03-25 13:30:00
  • 鼠标实现图片的渐有渐无

    2013-06-30 02:49:10
  • python之OpenCV的作用以及安装案例教程

    2021-11-27 07:14:20
  • asp之家 网络编程 m.aspxhome.com