Python中循环及判断语句

循环判断条件是编程语言中一个很重要的部分,python也不例外,循环判断条件一般结合continue,return,break关键字来判断,这些关键字用法与java中基本一致

一、if判断语句

判断条件返回的结果为布尔值,在python中,布尔值为True/False,首字母必须大写,否则将出现如下异常

1>>> ls=false 2Traceback (most recent call last): 3 File "<stdin>", line 1, in <module> 4NameError: name 'false' is not defined

View Code

python中,值不为空时,判断条件为True,如

1>>> str='flag' 2>>> if str: 3... print("not empty") 4... else: 5... print("empty") 6... 7not empty 8>>> str='' 9>>> if str: 10... print("not empty") 11... else: 12... print("empty") 13... 14empty

View Code

单个判断条件

1>>> tup=('dog','cat','water','bj') 2>>> for val in tup: 3... if len(val) < 3: 4... print("the length of " + val + " is less than 3") 5... elif len(val) == 3: 6... print("the length of " + val + " is 3") 7... else: 8... print ("the length of " + val + " is more than 3") 9... 10the length of dog is 3 11the length of cat is 3 12the length of water is more than 3 13the length of bj is less than 3

多个判断条件可以使用and或者or

1>>> name="xiao" 2>>> if name != "" and len(name) > 3: 3... print ("long name") 4... else: 5... print ("short name") 6... 7long name 8 9>>> weight=100 10>>> if weight > 150 or weight < 60: 11... print ("no normal weight") 12... else: 13... print ("normal weight") 14... 15normal weight

 判断某个值是否在列表中存在

1>>> ls=['car','cat','dog'] 2>>> if 'car' in ls: 3... print("yes, it is car") 4... else: 5... print("no car") 6... 7yes, it is car

while循环

1>>> i=5 2>>> while(i>1): 3... i-=1 4... print(i) 5... 64 73 82 91

for循环

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

商业数据分析从入门到入职(6)Python程序结构和函数

一、Python程序结构Python中,有3种常见的程序结构:Sequence顺序从上向下依次执行。Condition条件满足某个条件则执行。Loop循环重复执行某个动作。1.if条件判断某个变量是否满足某个条件时如下:pythonpossibility_to_rain0.7print(possibility_to_rain

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''