||
Using for loops with lists
Example-1
animal=['cat','dog','fish','elephant']
for i in animal:
print('The animal is '+i)
---
outPut
The animal is cat
The animal is dog
The animal is fish
The animal is elephant
Is a specific value in a list?
'cat' in animal
outPut
True
Multiple assignment trick
cat=['fat','orange','loud'] size=cat[0] color=cat[1] disposition=cat[2] | cat=['fat','orange','loud'] size,color,disposition=cat |
a,b='alice','bob' -->a='alice', b='bob'
a,b=b,a -->c=a,a=b,b=c
For more, https://automatetheboringstuff.com/chapter4/, https://docs.python.org/3/tutorial/datastructures.html#more-on-lists, http://www.runoob.com/python/python-variable-types.html
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-4-19 16:16
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社