zoukankan      html  css  js  c++  java
  • Python 入门 之 流程控制语句

    1、if判断

    (1) 单 if

    if –- 如果

    if 条件:
    缩进 结果
    
    # (缩进:官方推荐4个空格,或者一个tab   不能空格和tab混合使用)
    
    money = 10
    print("从学校出发")
    if money >= 10:    
    	print("买个炸鸡")    
    	print("买个啤酒")
    print("走啊走")
    print("到家了")
    

    (2) if else -- 二选一

    if 条件:
    缩进  结果
    else:
    缩进  结果
    
    if 3>2:    
    	print("这是如果执行了")    
    	print("123")    
    	print("234")
    else:    
    	print("这是否则执行了")
    

    (3) if elif elif elif ---多选一或不选

    if 条件:
    缩进 结果
    elif 条件:
    缩进 结果
    elif 条件:
    缩进 结果
    
    if 3>2:    
    	print("这是A")
    elif 4>3:    
    	print("这是B")
    elif 5>3:    
    	print("这是C")
    elif 6>3:    
    	print("这是D")
    

    (4) if elif elif else --多选一

    if 条件:
    缩进 结果
    elif 条件:
    缩进 结果
    elif 条件:
    缩进 结果
    else:
    缩进 结果
    
    if 1>2:    
    	print("A")
    elif 2>3:    
    	print("B")
    elif 5>6:    
    	print("c")
    else:    
    	print("D")
    

    (5)if if if -- 多个条件选多个

    if 条件:
    缩进 结果
    if 条件:
    缩进 结果
    if 条件:
    缩进 结果
    
    if 3>2:    
    	print("A")
    if 4>2:    
    	print("B")
    if 6>3:    
    	print("C")
    

    (6)if 嵌套:

    if 条件:
         if 条件:
    	缩进 结果
         else:
    	缩进 结果
    else:
    缩进 结果
    
    sex = "女"
    age = 30
    if sex == "女":    
    	if age == 30:        
    		print("年龄30")    
    	else:        
    		print("不是30岁")
    else:    
    	print("不是女性")
    

    2、while循环

    while 循环:死循环(可通过条件和break能够终止循环)

    (1)while --- 关键字

    while 条件:
        循环体
    
    while True:    
    	print("爱情买卖")    
    	print("痒")    
    	print("年少有为")    
    	print("浮夸")    
    	print("威风堂堂")    
    	print("大悲咒")    
    	print("情锁")
    # 这是一个死循环
    

    (2)break终止当前循环:

    while True:
        循环体
        Break
        语句
    
    while True:    
    	print("爱情买卖")    
    	break
    
    # break ---- 必须在循环中使用
    # break ---- 终止当前循坏并且break下方的代码不会执行
    

    (3)Continue 跳出本次循环,继续下次循环(伪装成循环体中的最后一行代码)

    while True:
    	循环体
    	Continue
            语句
    
    while True:    
    	print("爱情买卖")   
        print("痒")    
        continue    
        print("年少有为")
    
    # continue ---- 跳出本次循环,继续下次循环(伪装成循环体中最后一行代码)
    # continue ---- 必须在循环中使用,并且continue下方的代码不会被执行
    

    (4)while else --- 是一个整体循环

    while True:
    	循环体
    else:
    	语句
    
    # while else --- 当while后的条件不为真时,执行else后的语句
    # 当while循环体中出现了break就不会再执行else
    
    flag = True
    while flag:    
    	print(123)    
    	flag = False
    else:    
    	print("循环结束!")
    
    # 使用while输出10 - 57的数字(包含10和57)
    num = 10
    while num <= 57:    
    	print(num)    
    	num = num + 1
    	
    # 使用while 输出 100-10 的数字(包含100和10)
    num = 100
    while num > 9:    
    	print(num)    
    	num = num - 1
    

    3、for循环

    for i in 变量:

    ​ 执行语句

    msg = "好好学习,天天向上"
    for a in msg:
        print(a)
    print(a)
    

    pass :过,占位

    for i in 变量:

    ​ pass # … 和pass 功能一样,但推荐使用pass

    for a in "abcds":
        pass              # 过  占位,不做任何操作,pass就是相当于在for循环中执行了一个什么都不做的语句
    print(a)
    

    for – 关键字 i --- 变量名 in ---- 关键字 msg--- 可迭代对象

    (可迭代对象:在Python数据类型中,除了int, bool 其余都可以迭代)

    for循环是可以循环的数据结构:

    • 字符串(str)
    • 列表(list)
    • 元组(tuple)
    • 字典(dict)
    • 集合(set)

    唯独不能进行循环的就是 整型(int)和布尔值(bool)
    for循环在循环的时候就已经进行了赋值

    天之涯,海之角,吾与money天荒地老。
  • 相关阅读:
    POJ2778 DNA Sequence AC自动机上dp
    codeforces732F Tourist Reform 边双联通分量
    codeforces786B Legacy 线段树优化建图
    洛谷P3588 PUS 线段树优化建图
    codeforces1301D Time to Run 模拟
    codeforces1303B National Project 二分或直接计算
    codeforces1303C Perfect Keyboard 模拟或判断欧拉路
    codeforces1303D Fill The Bag 二进制应用+贪心
    python之路——使用python操作mysql数据库
    python之路——mysql索引原理
  • 原文地址:https://www.cnblogs.com/caiyongliang/p/11409527.html
Copyright © 2011-2022 走看看