编程小白的第一本 python 入门书 学习笔记04 第五章 循环与判断



列表 List

album=['BlackStar','DB',25,True]

album.append('newsong')

print(album[0],album[-1])

 

bool(0)      #False

bool([])     #False

bool('')     #False

bool(False)  #False

bool(None)   #False

 

a_thing= None
 

1 < 3and 2 < 5  #True

1 < 3and 2 > 5  #False

1 < 3or 2 > 5   #True

1 > 3or 2 > 5   #False

 

 

条件控制

ifcondition:

do something

 

else:

do something

 

 

password_list=['*#*#','12345']

def account_login():
    password=input('Password:\n')
   password_correct=password==password_list[-1]
   password_reset=password==password_list[0]

    if password_correct:
        print('Loginsuccess!')
    elif password_reset:
        new_password=input('Entrya new password:\n')
       password_list.append(new_password)
        print("Yourpassword has been changed successfully!")
       account_login()
    else:
        print("Incorrectpassword or invalid input!")
       account_login()


   account_login()

 

 

 

循环 Loop

for循环

 

for itemin iterable:

       do something

 

 

songslist = ['HolyDiver', 'Thunderstruck', 'Rebel Rebel']


for song in songslist:
    if song == 'Holy Diver': print(song,' -Dio')
    elif song == 'Thunderstruck': print(song,' -AC/DC')
    elif song == 'Rebel Rebel': print(song,' -David Bowie')

 

乘法表

for i in range(1,10):
    for j in range(1,10):
        print('{0}* {1} = {2}'.format(i,j,i*j), end='\t')
    print('\n')

 

 

 

while循环

 

whilecondition:

do something

 

while1<3:

print('1 is less than 3')# Ctrl+C 停止运行

 

 

count = 0

whileTrue:

print('Repeat this line!')

count=count+1

if count == 5:

break

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值