python中的逗号及分号解释

新手在python一段代码中使用分号会出现意想不到的结果,慎用逗号,多用分号及代码换行,代码执行以分号为结束。

实例:

test=['a',"b",12,34.5]
test[-1]=666;test.insert(2,333);test.append("888");print(test);
print(test[-1]);print(test);

期望值:

D:\py\venv\Scripts\python.exe D:/py/test.py
['a', 'b', 333, 12, 666, '888']
888
['a', 'b', 333, 12, 666, '888']

 修改实例:

test=['a',"b",12,34.5]
test[-1]=666,test.insert(2,333);test.append("888");print(test);
print(test[-1]);print(test);
D:\py\venv\Scripts\python.exe D:/py/test.py
['a', 'b', 333, 12, (666, None), '888']
888
['a', 'b', 333, 12, (666, None), '888']

再次修改:

test=['a',"b",12,34.5]
test[-1]=666,test.insert(2,333),test.append("888");print(test);
print(test[-1]);print(test);
D:\py\venv\Scripts\python.exe D:/py/test.py
['a', 'b', 333, 12, 34.5, (666, None, None)]
(666, None, None)
['a', 'b', 333, 12, 34.5, (666, None, None)]

最后修改:

test=['a',"b",12,34.5]
test[-1]=666,test.insert(2,333),test.append("888"),print(test);#逗号创造了元组
print(test[-1]);print(test);#此时打印对象列表test会看到最后一个成员为元组(后三个成员为None)
a=test.insert(2,444); b=test.append("888");cc=print(); print(a,b,cc);#函数返回值测试
D:\py\venv\Scripts\python.exe D:/py/test.py
['a', 'b', 333, 12, 34.5, '888']
(666, None, None, None)
['a', 'b', 333, 12, 34.5, (666, None, None, None)]

None None None

实例分析:
1.代码执行以分号为结束
2.逗号创造了元组(666,函数返回值None,函数返回值None,函数返回值None)
3.先执行元组里边的内置函数insert.append.print等操作都是最原始的对象test变为['a', 'b', 333, 12, 34.5, '888']
4.最后test[-1]表示对象test最后一个成员替换为元组(666, None, None, None)
5.内置函数返回值测试abc为None

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值