浅谈Python基础—判断和循环

yipeiwu_com5年前Python基础

判断

缩进代替大括号。

冒号(:)后换号缩进。

if

test=100
if test>50:
 print('OK')
print('test')

if-elif-else

test=50
if test>200:
 print('200')
elif test<100:
 print('100')
else:
 print('100-200')

列表

test1=[123,456,789]
if 123 in test1:
 print('OK')

字典

test2={'hello':123,'world':456}
if 'hello' in test2:
 print('OK')

循环

while

数值

test=0
while test<10:
 print(test)
 test+=1

集合

test1=set(['hello','world'])
while test1:
 test2=test1.pop()
 print(test2)

for

集合

test3=set(['hello','world'])
for t in test3:#相当于foreach
 print(t)

列表

test4=[123,456,789]
for i in range(3):
 print(test4[i])
test5=[123,456,789,34,5435,26,2362,262,26,5]
for i in range(len(test5)):
 print(test5[i])

continue

test6=[11,12,13,14,15,16,17,18,19,20]
for i in test6:
 if i%2==0:
  print(i) 
 else:
  continue
 print(i)

break

test7=[12,13,14,15,16,17,18,19,20]
for i in test7:
 if i%2==0:
  print(i) 
 else:
  break
 print(i)

以上所述是小编给大家介绍的Python基础—判断和循环详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python简单过滤字母和数字的方法小结

本文实例讲述了Python简单过滤字母和数字的方法。分享给大家供大家参考,具体如下: 实例1 crazystring = 'dade142.!0142f[., ]ad' # 只保留数...

详解Python 定时框架 Apscheduler原理及安装过程

详解Python 定时框架 Apscheduler原理及安装过程

在我们的日常工作自动化测试当中,几乎超过一半的功能都需要利用定时的任务来推动触发,例如在我们项目中有一个定时监控模块,根据自己设置的频率定时跑测试用例,定时检测是否存在线上紧急任务等等,...

python 实现归并排序算法

理论不多说: 复制代码 代码如下: #!/usr/bin/python import sys def merge(array, q, p, r): left_array = array[...

Python实现的微信好友数据分析功能示例

Python实现的微信好友数据分析功能示例

本文实例讲述了Python实现的微信好友数据分析功能。分享给大家供大家参考,具体如下: 这里主要利用python对个人微信好友进行分析并把结果输出到一个html文档当中,主要用到的pyt...

Python卸载模块的方法汇总

easy_install 卸载 通过easy_install 安装的模块可以直接通过  easy_install -m PackageName 卸载,然后删除\Python27...