学python的前提_Python语言学习前提:条件语句

一、条件语句

1.条件语句:通过一条或多条语句的执行结果(True或False)来决定执行额代码块。python程序语言指定任何非0或非空(null)的值为true,0或null为false。

2. if 语句

if 判断条件:

执行语句

else:

执行语句

例子如下:

#! /usr/bin/env python

username = 'crystal'

password = '1234'

Username = input("username:")

Password = input("password:")

if username == Username and password==Password:

print("Welcome user {name} login...".format(name=Username))

else:

print("Invalid username or password")

python不只是switch语句,如果需要做多个条件判断,只能用elif来实现。以下实例中用到 or 和 and 两个运算符

#! /usr/bin/env python

num = 9

if num >=0 and num <=10: #判断值是否在0-10之间

print ("hello") #输出结果hello

num = 10

if num <0 or num >10: #判断值是否在小于0或大于10的区间里

print ("hello")

else:

print ("undefine") #输出结果:undefine

num = 8

#判断是否在0~5或者10~15之间

if (num >=0 and num <=5) or (num >=10 and num <=15):

print ("hello")

else:

print ("undefine")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值