zoukankan      html  css  js  c++  java
  • python -- 程序的结构语句

    一、顺序结构

    顺序结构是python脚本程序中基础的结构,它是按照程序语句出现的先后顺序进行依次执行

    二、选择结构

    选择结构是通过判断某些特定的条件是否满足来决定程序语句的执行顺序

    常见的有单分支选择结构、双分支选择结构、多分支以及嵌套选择结构

    # ----------------------------------------------------
    # 选择结构:通过判断某些特定条件是否满足来决定程序的执行顺序。
    # ----------------------------------------------------
    # 布尔表达式中常用的操作运算符:<、<=、>、>=、==、!=、in和not in
    # 单分支选择结构
    a = 100
    b = 200
    if a < b:
    a, b = b, a
    print("将a和b进行了交换,a中保存较大的值")
    print("a, b:", a, b)

    # bool() 将其他值转换为布尔类型
    print(bool(1))
    print(bool("非空的元素,转换后都为True。"))
    print(bool([10, 20]))
    #以下执行的结果为False
    print(bool(0))
    print(bool(""))
    print(bool([]))

    # 双分支选择结构
    student_info1 = ['张晓晓', '女', 12, '七年级']
    student_info2 = ['刘烨', '男', 14, '九年级']
    name = '刘烨'
    if name == '张晓晓':
    print(student_info1)
    else:
    print(student_info2)


    # 三元运算符:value1 if condition else value2
    student_info = student_info1 if name == '张晓晓' else student_info2
    print(student_info)


    # 多分支选择结构
    score = 88
    level = ''
    if score >= 95:
    level = 'A+'
    elif score >= 85:
    level = 'A'
    elif score >= 75:
    level = 'A-'
    elif score >= 60:
    level = 'B'
    else:
    level = 'C'
    print("Level:", level)


    # pass语句-表示程序结构的完整性,占位符
    n = 2
    if n == 1:
    print('Hello python!')
    elif n == 2:
    pass
    elif n == 3:
    print('Hello world!')


    # 嵌套分支结构
    score = 88
    level = ''
    if score >= 60:
    print("通过考核!")
    if score >= 80:
    level = '优等生'
    else:
    level = '中等生'
    else:
    print("未通过考核!")
    if score >= 45:
    level = '不及格'
    else:
    level = '低等生'
    print("Level:", level)


    三、循环结构
    循环结构是根据代码的逻辑条件来判断是否重复执行某段程序
    在python中主要有两种形式的循环结构:while、for 循环

    # ----------------------------------------------------
    # 循环结构:根据代码的逻辑条件来判断是否重复执行某一段程序。
    # ----------------------------------------------------
    # while循环
    # 数字猜猜猜
    num = 8
    guess = -1
    score = 100
    print("一起来玩数字猜猜猜!")
    while guess != num:
    guess = int(input("请输入一个数字:"))

    if guess == 0:
    break #退出循环体

    if guess < 0:
    continue #退出当前循环,再次进入循环体
    if guess == num:
    print("恭喜恭喜!猜中了!")
    score += 100
    elif guess < num:
    print("笨笨,猜的数字小了!")
    score -= 10
    elif guess > num:
    print("笨笨,猜的数字大了!")
    score -= 10

    print("你当前的积分是:", score)
    #当while语句False时,执行else语句,如在循环体中执行break语句是不会执行else语句
    else:
    print("顺利过关")
    print("游戏结束!")


    # for循环
    # range() 生成一个数列
    for x in range(10): # 等价于range(0, 10, 1)
    print(x)

    # 也可以指定一个区间
    for y in range(5, 10):
    print(y)

    # 还可以指定不同的增量
    for z in range(20, 100, 10):
    print(z)

    # 遍历一个序列的索引
    cities = ['北京', '天津', '上海', '广州', '深圳', '珠海']
    n = 0
    for city in cities:
    print(n, city)
    n += 1
    # 遍历循环结束,退出循环体
    print("遍历城市列表结束")

    for n in range(len(cities)):
    print(n, cities[n])
    print("遍历城市列表结束")

    n = 0
    while n < len(cities):
    print(n, cities[n])
    n += 1
    print("遍历城市列表结束")
  • 相关阅读:
    【Winform】Webservice调用服务器端EXE
    LeetCode: Word Ladder II
    LeetCode: Maximal Rectangle
    Algorithm: 拓扑排序
    Algorithm: 匈牙利算法
    LeetCode: Scramble String
    LeetCode: Integer to Roman
    LeetCode: Roman to Integer
    算法导论:基础知识。
    C++主函数main()讲解
  • 原文地址:https://www.cnblogs.com/Teachertao/p/11217439.html
Copyright © 2011-2022 走看看