基础-if判断

if 基础语法格式(注意格式--缩进)

  • if 语句 以及 缩进部分可以看成一个 完整的代码块
age = 18
if age >= 18:
    print("happy")

if-延伸-自定义变量类型

  • if 和 else 语句 以及 各自的缩进部分共同是一个 完整的代码块
  • input 用户输入的数据 类型是 str() == 字符串类型  所以要用到类型转换
age = int(input('请输入年龄:'))
if age >= 18:
    print('happy')
else:
    print('go home')

 

拓展:比较运算符

 

逻辑运算符

 

连接条件

  and  两者都为真才是真

  or     一个为真就为真

取反

  not  非

 

逻辑演练1:

# 定义一个变量age 编写代码判断年龄是否正确
age = 1211

# 要求年龄在 0-199 之间
if age >= 0 and age <= 199:
    print("年龄正确")
else:
    print("年龄错误")

逻辑演练2:

# 定义两个变量 python_score c_score 编写代码判断成绩
python_score = 20
c_score = 20
# 只要一门成绩大于60分就合格 if python_score > 60 or c_score > 60: print("成绩合格") else: print("成绩不合格")

逻辑演练3:

  • 在开发中,通常希望某个条件不满足时,执行一些代码 可以用not
  • 如果需要拼接复杂的逻辑计算条件,同样也可能使用到 not
# 定义一个布尔型变量 is_employee,编写代码判断是否是本公司员工
is_employee = False
# 如果不是提示不允许入内 if not is_employee: print("非本公司员工,不许入内")

 

pycharm小技巧

选中行 按下 Tab键 会自动缩进

那么怎么 返回呢 , 只需要 按下 shift + Tab 即可

 

posted @ 2020-04-08 08:33  鄢笑  阅读(220)  评论(0编辑  收藏  举报