names=['tom','jack','stefan',]
for name in names:
print("hello "+name)
-----------------------------------
hello tom
hello jack
hello stefan
names=['tom','jack','stefan',]
for name in names:
print(name.title()+" is a student")
-------------------------------------
Tom is a student
Jack is a student
Stefan is a student
names=['tom','jack','stefan',]
for name in names:
print("hello "+name)
print("welcome to here")
------------------------------------
hello tom
hello jack
hello stefan
welcome to here
记得不要丢掉冒号和缩进
避免不必要的错误缩进
for i in range(1,6):
print(i)
---------------------------------
1
2
3
4
5
上述代码好像会打印数字1~6,但其实不会打印数字6
numbers=list(range(1,6))
print(numbers)
---------------------------
[1, 2, 3, 4, 5]
打印10以内的偶数
numbers=list(range(2,11,2))
print(numbers)
----------------------------------
[2, 4, 6, 8, 10]
打印1~10的平方
squares=[]
for i in range(1,11):
square=i**2
squares.append(square)
print(squares)
----------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
squares=[]
for i in range(1,11):
squares.append(i**2)
print(squares)
----------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
numbers=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
min(numbers)
-------------------------------------------------
1
numbers=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
max(numbers)
----------------------------------------------
100
numbers=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
sum(numbers)
----------------------------------------------
385
squares=[i**2 for i in range(1,11)]
print(squares)
------------------------------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
my_colors=['blue','black']
my_girlfriend_colors=my_colors[:]
print("I like ")
print(my_colors)
print("My girlfriend like ")
print(my_girlfriend_colors)
-------------------------------------
I like
['blue', 'black']
My girlfriend like
['blue', 'black']
添加不同喜好
my_colors=['blue','black']
my_girlfriend_colors=my_colors[:]
my_colors.append("mua")
my_girlfriend_colors.append("pink")
print("I like ")
print(my_colors)
print("My girlfriend like ")
print(my_girlfriend_colors)
--------------------------------
I like
['blue', 'black', 'mua']
My girlfriend like
['blue', 'black', 'pink']
my_colors=['blue','black']
#这样试试
my_girlfriend_colors=my_colors
my_colors.append("mua")
my_girlfriend_colors.append("pink")
print("I like ")
print(my_colors)
print("My girlfriend like ")
print(my_girlfriend_colors)
------------------------------------------
I like
['blue', 'black', 'mua', 'pink']
My girlfriend like
['blue', 'black', 'mua', 'pink']
这样的结果不是我们想要得到的
yuanzu=(521,1314)
print(yuanzu[0])
print(yuanzu[1])
-----------------------------
521
1314
试试修改元组中的一个元素
yuanzu=(521,1314)
yuanzu[0]=886
print(yuanzu[0])
print(yuanzu[1])
--------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-d22d2a3640c1> in <module>
1 yuanzu=(521,1314)
----> 2 yuanzu[0]=886
3 print(yuanzu[0])
4 print(yuanzu[1])
TypeError: 'tuple' object does not support item assignment
出现错误,由于修改元组中的元素操作是被禁止的
这很好,这正是我们希望的。
nums=(1,5,8)
for num in nums:
print(num)
---------------------------------
1
5
8
虽然修改元组中元素的值是被禁止的,但是我们可以重新定义元组
nums=(1,5,8)
for num in nums:
print(num)
print("now change it")
nums=(521,125,1314)
for num in nums:
print(num)
----------------------
1
5
8
now change it
521
125
1314