Python学习笔记——4

for循环和标志位

for循环:

# for i in range(1, 11, 2):		# 间隔输出,2为步长
#     if i % 2 ==1:		# 嵌入可输出奇数
for i in range(1, 11):
    print("Loop:", i)	# 输出1-10

验证登录程序:

user = "hao"
password = "123"

for i in range(3):	 # range——[0,1,2]
    input_user = input("user:")
    input_password = input("password:")
    if input_password == password and input_user == user:
        print("welcome %s login...." % user)
        break
    else:
        print("Invalid username or password!"
else:
    print("Game over.")	 # 正常循环则会被执行,break打断后就不会执行此else语句,while循环也相同

输出:

user:我是
password:sd
Invalid username or password!
user:sa
password:dd
Invalid username or password!
user:sdf
password:fsdw
Invalid username or password!
Game over.

标志位关联break:

exit_flag = False
for i in range(10):
    if i < 5:
        continue
    print(i)
    for j in range(10):
        print("layer2", j)
        if j == 5:
            exit_flag = True
            break   # you jump!
    if exit_flag:
        break   # I jump!关联跳

输出:

5
layer2 0
layer2 1
layer2 2
layer2 3
layer2 4
layer2 5

列表增删改查:

1. 切片:

列表list索引下标从0开始,切片左闭右开

# 切片
a = ['one', 'two', 'apple', 'pear', 'huge', 'yifei']
print(a[1:3])
print(a[1:])      # 冒号右不加值表示取到最后一个值
print(a[1:-1])    # 冒号右赋值-1,-1表示列表最后一个索引(取到倒数第二个值),依次-2...
print(a[1:-1:2])    # 2表示步长,默认为1
print(a[1::2])    # 从左1到右步长为2
print(a[::-1])    # 从右到左步长为1

输出:

[‘two’, ‘apple’]
[‘two’, ‘apple’, ‘pear’, ‘huge’, ‘yifei’]
[‘two’, ‘apple’, ‘pear’, ‘huge’]
[‘two’, ‘pear’]
[‘two’, ‘pear’, ‘yifei’]
[‘yifei’, ‘huge’, ‘pear’, ‘apple’, ‘two’, ‘one’]

2. 增:

  1. 追加.append:在列表末尾追加元素
  2. 插入.insert:在指定位置插入元素
  3. 扩充.extend:把一个列表扩充到另一个列表
# 追加.append 插入.insert 扩充.extend
a = ['one', 'two', 'apple', 'pear', 'huge', 'yifei']
a.append('hao')     # 追加到最后位置
print(a)
a.insert(1, 'lin')  # 插入的'lin'索引值为1
print(a)
b = ['1', '3']
a.extend(b)     # 把b扩充到a,b需同为列表
print(a)

输出:

[‘one’, ‘two’, ‘apple’, ‘pear’, ‘huge’, ‘yifei’, ‘hao’]
[‘one’, ‘lin’, ‘two’, ‘apple’, ‘pear’, ‘huge’, ‘yifei’, ‘hao’]
[‘one’, ‘lin’, ‘two’, ‘apple’, ‘pear’, ‘huge’, ‘yifei’, ‘hao’, ‘1’, ‘3’]

3. 改:

  1. 赋值修改
  2. .reverse倒转列表
  3. .sort排序:按ASCII码大写字母在前,且按首字依次排序
# 赋值修改 .reverse倒转列表
a = ['one', 'two', 'apple', 'pear', 'huge', 'yifei']
a[2] = 'three'      # 直接赋值
a[3:5] = ['four', 'five']   # 切片修改,左右列表长度可以不匹配
print(a)
a.reverse()     # 倒转列表
print(a)
b = ['1', '3', '3', '1', '7', '10', '3']
a.sort()        # 排序,按ASCII码大写字母在前,且按首字母依次排序
b.sort(reverse=True)
print('''%s,
%s
''' % (a, b))

输出:

[‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘yifei’]
[‘yifei’, ‘five’, ‘four’, ‘three’, ‘two’, ‘one’]
[‘One’, ‘five’, ‘four’, ‘three’, ‘two’, ‘yifei’],
[‘7’, ‘3’, ‘3’, ‘3’, ‘10’, ‘1’, ‘1’]

4. 删:

  1. .remove
  2. .pop(弹出)
  3. del
  4. .clear(清空)
# 删除 .remove .pop(弹出) del .clear(清空)
a = ['one', 'two', 'apple', 'pear', 'huge', 'yifei']
a.remove('pear')        # 删除指定元素,不能删除切片
# a.remove(a[0])
print(a)
b = a.pop(0)        # 删除并返回删除元素,相当于栈弹出,不指定索引默认最后一个
print(a, '---', b)
del a[3]
# del a[]       # 删除对象a
# a.clear()       # 清空列表元素
print(a)

输出:

[‘one’, ‘two’, ‘apple’, ‘huge’, ‘yifei’]
[‘two’, ‘apple’, ‘huge’, ‘yifei’] — one
[‘two’, ‘apple’, ‘huge’]

5. 查:

  1. .index:查指定元素对应下标
  2. .count:查元素个数
  3. in:查某元素在不在列表中
# 查: .count查元素个数 .index查某元素索引 in查某元素在不在列表中
counter = ['1', '3', '3', '1', '7', '10', '3'].count('3')  # 查列表中某元素的个数
print(counter)
a = ['one', 'two', 'apple', 'pear', 'huge', 'yifei']
print(a.index('two'))   # 查列表a中'two'的索引
print('hao' in a)   # 查某元素在不在列表中

输出:

3
1
False

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值