03-10 python语法入门之流程控制之if判断

if后面紧跟条件,根据条件进行判断,什么是条件请移交以下地址:https://www.cnblogs.com/yang1333/p/12430145.html

前提:了解if判断我们需要了解代码块的概念,什么是代码块?代码块就是同一空格缩进级别的所有代码,我们称之为代码块。

提示:顶行代码也是同一级别的代码。python中默认4个空格的缩进,但是你1个空格的缩进也是可以的,只是规范使用要使用4个空格。

什么是伪代码?可以把伪代码理解成就是一种表示语法使用规范的代码模型

简化连接快捷键:Alt + Shift + 回车

if

# 1、伪代码展示
'''
if 条件:
    代码1
    代码2
    代码3
'''

# 2、代码实例
age = 60
is_beautiful = True
star = '水平座'

# 注意: 以下16 < age and age < 20可以简化成16 < age < 20,简化连接快捷键 Alt + Shift + 回车
# if 16 < age and age < 20 and is_beautiful and star == '水平座':
if 16 < age < 20 and is_beautiful and star == '水平座':
    print('我喜欢,我们在一起吧。。。')

print('其他代码.............')

if ... else

# 1、伪代码示例
'''
if 条件:
    代码1
    代码2
    代码3
else:
    代码1
    代码2
    代码3
'''
# 2、代码实例
age = 60
is_beautiful = True
star = '水平座'

if 16 < age < 20 and is_beautiful and star == '水平座':
    print('我喜欢,我们在一起吧。。。')
else:
    print('阿姨好,我逗你玩呢,深藏功与名')

print('其他代码.............')

if ... elif ... elif

# 1、伪代码示例
'''
if 条件1:
    代码1
    代码2
    代码3
elif 条件2:
    代码1
    代码2
    代码3
elif 条件2:
    代码1
    代码2
    代码3
'''

# 2、代码实例
score = 73
if score >= 90:
    print('优秀')
elif score >= 80 and score < 90:
    print('良好')
elif score >= 70 and score < 80:
    print('普通')
    
# 改进:score < 90 与 score < 80 这两个条件多余
score = input('请输入您的成绩:')  # score="18"
score = int(score)

if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')

if ... elif ... elif ... else

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

# 1、伪代码示例
score = input('请输入您的成绩:')  # score="18"
score = int(score)

if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差,小垃圾')

print('=====>')

if嵌套

age = 17
is_beautiful = True
star = '水平座'

if 16 < age < 20 and is_beautiful and star == '水平座':  # 条件判断为True,执行改if下面的子代码块
    print('开始表白。。。。。')
    is_successful = True
    if is_successful:	# 条件判断为True,执行改if下面的子代码块
        print('两个从此过上没羞没臊的生活。。。')
else:
    print('阿姨好,我逗你玩呢,深藏功与名')

print('其他代码.............')

更多详细内容请移交地址:https://www.cnblogs.com/yang1333/p/12341054.html

posted @ 2020-03-06 20:45  给你加马桶唱疏通  阅读(273)  评论(0编辑  收藏  举报