python学习笔记02(数字列表,元组,遍历列表)

在这里插入图片描述

1.操作列表

1.1遍历整个列表

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

1.2避免错误

记得不要丢掉冒号缩进
避免不必要的错误缩进

2.创建数字列表

2.1使用函数range()

for i in range(1,6):
    print(i)
 ---------------------------------
 
1
2
3
4
5

上述代码好像会打印数字1~6,但其实不会打印数字6

2.2使用range()创建数字列表

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]

2.3对数字列表进行简单统计工作

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

2.4列表解析

squares=[i**2 for i in range(1,11)]
print(squares)
------------------------------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

2.5复制列表

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']

这样的结果不是我们想要得到的

3.元组

3.1定义元组

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

出现错误,由于修改元组中的元素操作是被禁止的
这很好,这正是我们希望的。

3.2遍历元组中的所有值

nums=(1,5,8)
for num in nums:
    print(num)
 ---------------------------------
1
5
8

3.3修改元组变量

虽然修改元组中元素的值是被禁止的,但是我们可以重新定义元组

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

这样就可以啦~
在这里插入图片描述
好了,今天就学到这里吧。随手点个赞吧

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据攻城小狮子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值