zoukankan      html  css  js  c++  java
  • Python学习之控制结构以及random库的使用

    Python学习之控制结构以及random库的使用

    程序的控制结构大致如下图所示:

    注:众所周知,程序的执行过程是按照从上至下顺序执行,所以我们在写程序的时候要严格遵循这一点来进行编写

    1.分支结构

    • 单分支结构

      指根据判断条件结果而选择不同向前路径的运动方式

      demo:

      score = 95
      if score >90:
          print('excellent')
      

      results:

      excellent

    • 二分支结构

      指根据判断条件结果而选择不同向前路径的运动方式

      demo:

      score = 60
      if score>90:
          print('excellent')
      else:
          print('good')
      

      results:

      good

    • 多分支结构

      多分支结构

      注:
      • 注意多条件之间的包含关系
      • 注意变量取值范围的覆盖

      demo:

      score = eval(input("请输入成绩:"))
      if score > 95:
          print('excellent')
      elif score > 85:
          print('good')
      elif score > 75:
          print('medium')
      elif score > 60:
          print('pass')
      else:
          print('not pass')
      

      results:

      请输入成绩:80

      medium

    2.循环结构

    • 遍历循环

    • 无限循环

    • 循环控制保留字

    2.1 遍历循环(for循环)

    遍历某个结构形成的循环运行方式

    ​ 从遍历结构中逐一提取元素,放在循环变量汇总

    ​ 保留字forin组成,完整遍历所有元素后结束

    ​ 每次循环,所获得元素放入循环变量,并执行

    demo:

    for i in range(5):
    	print(i,end=' ')
    

    results:

    0 1 2 3 4

    2.2 无限循环(while循环)

    ​ 由条件控制的循环运行方式

    ​ 反复执行语句块,直到条件不满足时结束

    demo:

    a = 0
    while a < 5:
        a += 1
        print(a,end=' ')
    

    results:

    1 2 3 4 5

    2.3 循环控制保留字

    break 和 continue
    • break跳出并结束当前整个循环,执行循环后的语句
    • continue结束当次循环,继续执行后续次数循环
    • break和continue 可以与 for和while循环搭配使用

    for--continue--demo:

    for c in "PYTHON":
        if c == 'T':
            continue
        print(c,end=' ')
    

    results:

    P Y H O N

    for--break--demo:

    for c in "PYTHON":
        if c =='T':
            break
        print(c,end=' ')
    

    results:

    P Y

    while--continue--demo:

    s = "PYTHON"
    while s != "":
        for c in s:
            print(c, end=',')
        s = s[:-1]
    

    results:

    P,Y,T,H,O,N,P,Y,T,H,O,P,Y,T,H,P,Y,T,P,Y,P,

    while--break--demo:

    s = "PYTHON"
    while s != "":
        for c in s:
            if c == 'T':
                break
            print(c, end=',')
        s = s[:-1]
    

    results:

    P,Y,P,Y,P,Y,P,Y,P,Y,P,

    3. 条件判断及组合

    3.1 条件判断

    操作符 数学符号 描述
    < < 小于
    <= 小于等于
    >= 大于等于
    > > 大于
    == = 等于
    != 不等于

    3.2 条件组合

    用于条件组合的保留的三个保留字
    操作符及使用 描述
    x and y 两个条件x和y的逻辑与
    x or y 两个条件x和y的逻辑或
    not x 条件x的逻辑非

    4. Python异常处理

    异常处理的基本使用

    语法:

    try:
    	<语句块1>
    except:
        <语句块2>
    

    demo:

    try:
        a = input("请输入一个数字:")
        print(a-10)
    except Exception as e:
        print('error:',e)
    

    results:

    请输入一个数字:10
    error: unsupported operand type(s) for -: 'str' and 'int'

    5.课后小案例

    https://www.cnblogs.com/foreversun92/p/11202633.html

    今日总结

    程序的控制结构是编程中非常重要的一部分内容,因为编程的大部分内容都会涉及到程序控制。掌握它可以处理很多的问题和难题,要勤加练习,多思考,多用,灵活组合。get 新技能!
  • 相关阅读:
    UVA
    UVA
    模板——扩展欧几里得算法(求ax+by=gcd的解)
    UVA
    模板——2.2 素数筛选和合数分解
    模板——素数筛选
    Educational Codeforces Round 46 (Rated for Div. 2)
    Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
    Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
    Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
  • 原文地址:https://www.cnblogs.com/foreversun92/p/11203517.html
Copyright © 2011-2022 走看看