欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python学习笔记Day4

程序员文章站 2022-07-02 16:42:14
今日学习内容 1、if 判断(流程控制的一种) 2、while循环(条件循环) 3、for循环(迭代器循环) 一、if 判断 语法一:if判断 if判断使用注意事项:if条件紧跟着一个冒号:+回车,pycharm会自动进入到下一行并缩进4个空格,开始进入运行一组代码块。在这里注意中英文输入法对代码“ ......

今日学习内容

                  1、if 判断(流程控制的一种)

                  2、while循环(条件循环)

                  3、for循环(迭代器循环)

==========================================================

一、if 判断

语法一:if判断

sex = 'female'
age = 32
is_beautiful = true
if sex == 'female' and age == 18 and is_beautiful:
    print('begin your love words:')
print('another code1')
print('another code2')

if判断使用注意事项:if条件紧跟着一个冒号:+回车,pycharm会自动进入到下一行并缩进4个空格,开始进入运行一组代码块。在这里注意中英文输入法对代码“括号、冒号、逗号”这些字符的影响,谨记一定要用英文输入法输入,不然容易出现报错。

 

语法二:if+else判断

sex = 'female'
age = 18
is_beautiful = true
if sex == 'female' and 16 < age <24 and is_beautiful:
    print('begin your love words.')
else:
    print('say nothing...')

语法三:if+else 的嵌套

sex = 'female'
age = 18
is_beautiful = true
is_success = true
if sex == 'female' and 16 < age <24 and is_beautiful:
    print('begin your love words.')
    if is_success:
        print('hahaha')
    else:
        print('i hate love,go away!')
else:
    print('say nothing...')

tip:①and 多个条件并过长时,可用‘\+回车’来多行显示,这里不影响语法。

       ②快捷操作,tab键 可以进行快捷缩进,比如选中多行一起快捷缩进,要回退的话,按shift+tab组合键。

语法四:if+elif+else

# 输入成绩判断优秀、很好、一般、很差
score = input('please input your score:')
score = int(score)
if score >= 90:
    print('优秀')
elif score >= 80:
    print('很好')
elif score >= 70:
    print('一般')
else:
    print('很差')

小练习:

Python学习笔记Day4
name = input('请输入您的用户名:')
pwd = input('请输入您的密码:')
if name == 'sgt' and pwd == '123':
    print('登陆成功')
else:
    print('用户名或密码错误')
练习一:用户登陆验证
Python学习笔记Day4
sgt = 'spueradministrator'
sgf = 'systemadministrator'
shr = 'administrator'
sqs = 'guest'
name = input('please input your username:')
if name == 'sgt':
    print('你好,超级管理员!')
elif name =='sgf':
    print('你好,系统管理员!')
elif name == 'shr':
    print('你好,管理员!')
elif name == 'sqs':
    print('你好,宾客!')
根据用户输入内容打印其权限
Python学习笔记Day4
while true:
    today = input('今天是星期几?')
    if today == 'monday' or today == 'tuesday' or today == 'wednesday'\
        or today == 'thursday' or  today == 'friday':
        print('上班')
        break
    elif today == 'saturday' or today == 'sunday':
        print('出去浪')
        break
    else:
        print('''
        必须输入以下任意一种:
        monday
        tuesday
        wednesday
        thursday
        friday
        saturday
        sunday
        ''')
上班或出去浪

 

二、while循环

方式一:while+true/false

tag = true
while tag:
    name = input('please input your name:')
    pwd = input('please input your password:')
    if name == 'sgt' and pwd == '123':
        print('congratulation,region success!')
        tag = false
    else:
        print('your name or password is wrong,please try again!')

 

方法二:while+break

while true:
    name = input('please input your name:')
    pwd = input('please input your password:')
    if name == 'sgt' and pwd == '123':
        print('congratulation,region success!')
        break
    else:
        print('your name or password is wrong,please try again!')

break是终止本层while循环。

方法三:while+continue

nums = 1
while nums <= 6:
    if nums == 4 or nums == 5:
        nums += 1
        continue
    else:
        print(nums)
        nums += 1

终止当前循环,直接进入下次循环。

方法四:while+else

nums = 1
while nums <= 6:
    if nums == 4 or nums == 5:
        nums += 1
        continue
    else:
        print(nums)
        nums += 1
else:
    print('前面代码无break,else 条件成立,否则else不执行。')

方法五:while的嵌套

示例一:

while true:
    name = input('please input your username:')
    pwd = input('please input your password:')
    if name == 'egon' and pwd == '123':
        print('login successful')
        while true:
            print('''
            0_退出
            1_取款
            2_转账
            3_查询                   
            ''')
            choice = input('请输入您要执行的操作:')
            if choice == '0':
                break
            elif choice == '1':
                print('取款...')
            elif choice == '2':
                print('转账...')
            elif choice == '3':
                print('查询...')
            else:
                print('输入指令有误,请重新输入:')
        break
    else:
        print('username or password error!')

改进示例二:

tag = true
while tag:
    name = input('please input your username:')
    pwd = input('please input your password:')
    if name == 'egon' and pwd == '123':
        print('login successful')
        while tag:
            print('''
            0_退出
            1_取款
            2_转账
            3_查询       
            ''')
            choice = input('请输入您要执行的代码:')
            if choice == '0':
                tag = false
            elif choice == '1':
                print('取款...')
            elif choice == '2':
                print('转账...')
            elif choice == '3':
                print('查询...')
            else:
                print('指令错误,请重新输入')
    else:
        print('username or password error')

三、for 循环 (其强大之处在于循环取值)

方案一、

for循环取值列表:

取值如果用while循环:
l = ['a', 'b', 'c', 'd', 'e']
i = 0
while i < len(l):                   #这里的len(l)表示计算列表l的长度(也就是索 
                                    #引数) 
    print(l[i])                      
    i += 1                         


如果我们使用for循环取值:
l = ['a', 'b', 'c', 'd', 'e']
for x in l:                        #这里的x in l 是直接将l列表里的数据取出关联 
     print(x)                      #到x,不用额外赋值。

for循环取值字典:

info = {'sgt': 'me', 'sgf': 'brother', 'age': 18, 'sun': 'shr'}
for x in info:
    print(x, info[x])

方案二、for+break

取出列表前几个数,后面的不取

nums = [11, 22, 33, 44, 55]
for x in nums:
    if x == 44:
        break
    print(x)

方案三、for+continue

不取其中的多个数据

nums = [11, 22, 33, 44, 55]
for x in nums:
    if x == 22 or x == 44:
        continue
    print(x)

方案四、for+else

names = ['egon', 'alexdsb', 'sb', 'dsb']
for name in names:
    if name == 'alexdsb':
        break
    print(name)
else:
    print('>>>>>>>>>>>')

#有break ,else下面的print不执行。

names = ['egon', 'alexdsb', 'sb', 'dsb']
for name in names:
    if name == 'alexdsb':
        continue
    print(name)
else:
    print('>>>>>>>>>>>')
#如果无break,else下面的print执行。

方案五、for+range()

range的用法:

>>> range(5)
[0, 1, 2, 3, 4]
>>> range(1,5)
[1, 2, 3, 4]
>>> range(1,5,2)
[1, 3]
>>> range(1,1)
[]
>>>
正规格式为range(1,5,2)
表示:从1开始计数,每计数一次递增2,所以结果是[1,3]
如果括号里只有一个数,代表(0,5,1),即从0开始计数,递增数为1.

示例:

for x in range(5):
    print(x)

#结果:
#0
#1
#2
#3
#4

方案六、for循环嵌套

for x in range(3):
    for y in range(3):
        print(x, y)
'''
结果
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
'''