Python学习笔记

(一)循环语句(for、while、嵌套循环)

2021年4月15日,今天开始记录自己学习编程的心路历程。
从菜鸟开始,一步一步扎实的向前走,希望有一个好多结果,希望可以坚持!

1、for循环

(1)for循环可以遍历任何序列的项目,如一个列表或一个字符串;
(2)for循环与while循环的区别用法:1)当某个操作重复执行且循环次数已知时可用for循环;2)所有for循环均可以用while实现。
(3)for循环的语法结构:

for i in 一组值(可以是除数字以外的基本类型)

(4)实例
1)for循环遍历字符串

str1='abcd'
for i in str1:
	print(i)

2)for循环遍历元组 ( )

tuple1=(1 2 3 4 5 6 7 8 9 10)
for i in tuple1:
	print(i,end=' ')

3)for循环遍历列表 [ ]

#新老版身份证号的变换
order=["220105030705122","220102310523023","110105431208031","560110781120989"]
syst=["7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"]
last="10X98765432"
new_order=[]#建立一个空的列表
print(order)
for item in order:
    new17 = item[:6] + "19" + item[6:]
    # print(item,end=" ")
    # print(new17)
    add=0
    for i in range(17):
        add+=int(new17[i])*int(syst[i])
    new18=add%11
    new = new17 + last[new18]
    print(new)
    new_order.append(new)#将所有的18位身份证号都添加到一个列表里面方便下面进行遍历
for items in new_order:
    print(items[0:6]+"******"+items[12:18])

4)for循环遍历集合 { }

set1={’chen‘,1 2 3 4 5 6}
for i in set1:
	print(i,end=' ')

5)for循环遍历字典{ key:value }

for k in info.keys():#遍历字典info中的键
    print(k)
for v in info.values():#遍历字典info中的值
    print(v)
for k,v info.items():#遍历字典info的键值对
	print(k,'--->',v)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值