zoukankan      html  css  js  c++  java
  • 条件语句和循环语句

    一、python条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
    Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
    Python 编程中 if 语句用于控制程序的执行,基本语法有以下几种:

    1.语法一

    if 条件:
        # 条件成立时执行的子代码块
        代码1
        代码2
        代码3
    

    示例:

    sex='female'
    age=18
    is_beautiful=True
    if sex == 'female' and age > 16 and age < 20 and is_beautiful:
        print('开始表白。。。')
    print('other code1...')
    print('other code2...')
    print('other code3...')
    

    2.语法二
    其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。
    else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句

    if 条件:
        # 条件成立时执行的子代码块
        代码1
        代码2
        代码3
    else:
        # 条件不成立时执行的子代码块
        代码1
        代码2
        代码3
    

    示例:

    sex='female'
    age=38
    is_beautiful=True
    if sex == 'female' and age > 16 and age < 20 and is_beautiful:
        print('开始表白。。。')
    else:
        print('阿姨好。。。')
    print('other code1...')
    print('other code2...')
    print('other code3...')
    

    3.语法三

    if 条件1:
        if 条件2:
            代码1
            代码2
            代码3
    

    示例:

    sex='female'
    age=18
    is_beautiful=True
    is_successful=True
    height=1.70
    if sex == 'female' and age > 18 and age < 25 and is_beautiful and height > 1.60 and height < 1.80: 
        print('开始表白。。。')
        if is_successful:
            print('在一起。。。')
        else:
            print('继续下一个')
    else:
        print('阿姨好。。。')
    print('other code1...')
    print('other code2...')
    print('other code3...')
    

    4.语法四

    if 条件1:
        代码1
        代码2
        代码3
    elif 条件2:
        代码1
        代码2
    .......
    else:
        代码1
        代码2
        代码3
    

    示例:
    如果成绩 >= 90,那么:优秀
    如果成绩 >= 80且 < 90, 那么:良好
    如果成绩 >= 70且 < 80, 那么:普通
    其他情况:很差

    score = input('please input your score: ')  # score='100'
    score = int(score)
    if score >= 90:
        print('优秀')
    elif score >= 80:
        print('良好')
    elif score >= 70:
        print('普通')
    else:
        print('很差')
    
    二、python While循环语句

    Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。
    执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
    当判断条件假false时,循环结束。

    1.基本语法

    while 条件:
        代码1
        代码2
        代码3
    

    示例

    while True:
        name=input('please input your name: ')
        pwd=input('please input your password: ')
        if name == 'egon' and pwd == '123':
            print('login successful')
        else:
            print('username or password error')
    

    2.结束while循环的两种方式
    while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,
    方式一:条件改为False,
    在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效

    tag=True
    while tag:
        name=input('please input your name: ')
        pwd=input('please input your password: ')
    
        if name == 'egon' and pwd == '123':
            print('login successful')
            tag=False
        else:
            print('username or password error')
    
        print('===>')
    

    方式二:while+break
    break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环

    while True:
        name=input('please input your name: ')
        pwd=input('please input your password: ')    
        if name == 'egon' and pwd == '123':
            print('login successful')
            break
        else:
            print('username or password error')  
        print('===>>>>>')
        print('===>>>>>')
    

    while+continue:结束本次循环,直接进入下一次循环
    示例一

    count=1
    while count < 6: #count=6
        if count == 4:
            count += 1
            continue        
        print(count)
        count+=1
    

    示例二:

    while True:
        name=input('please input your name: ')
        pwd=input('please input your password: ')
        if name == 'egon' and pwd == '123':
            print('login successful')
            break
        else:
            print('username or password error')
            # continue # 此处加continue无用
    

    了解知识
    while + else:

    while 条件:
        代码1
        代码2
        代码3
    else:
        在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码 
    

    示例

    tag=True
    while tag:
        print(1)
        print(2)
        print(3)
        # tag=False
        break
    else:
        print('else的代码')
    

    3.while嵌套循环

    while 条件1:
        while 条件2:
            代码1
            代码2
            代码3
    

    示范一:

    while True:
        name=input('please input your name: ')
        pwd=input('please input your password: ')
    
        if name == 'egon' and pwd == '123':
            print('login successful')
            while True:
                print("""
                0 退出
                1 取款
                2 转账
                3 查询
                """)
                choice=input('请输入您要执行的操作:') #choice='1'
                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')
    
    三、Python for 循环语句

    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
    for循环的强大之处在于循环取值

    1.利用for循环可以把字典或列表内的所有元素遍历

    dic={'name':'egon','age':18,'gender':'male'}
    for x in dic:
        print(x,dic[x])
    

    2.for循环与break的用法

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

    3.for循环与continue的用法

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

    4.for循环与else的用法

    names=['egon','peter','jack','tome']
    for name in names:
        if name == 'dab':
            break
        print(name)
    else:
        print('======>')
    

    5.for循环和range()的用法

    range的用法

    >>> range(1,5)
    [1, 2, 3, 4]
    >>> for i in range(1,5):
    ...     print(i)
    ...
    1
    2
    3
    4
    >>> range(1,5,2) # 1 3
    [1, 3]
    
    for i in range(5): # 0 1 2 3 4
        print(i)
    

    6.for嵌套

    for i in range(3):
        for j in range(4):
            print(i,j)
    
  • 相关阅读:
    python第三十二课——队列
    python第三十二课——栈
    python提示警告InsecureRequestWarning
    关于requests.exceptions.SSLError: HTTPSConnectionPool
    python第三十一课--递归(3.递归的弊端)
    01 redis特点及安装使用
    22 nginx配置与集群
    21-nginx单机1W并发优化
    20-ab压力测试及nginx性能统计模块
    19 大网站的优化思路
  • 原文地址:https://www.cnblogs.com/linwow/p/10693794.html
Copyright © 2011-2022 走看看