浅谈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.为模块nester创建文件夹nester,其中包含:nester.py(模块文件): 复制代码 代码如下:"""这是"nester.py"模块,提供了一个名为print_lol()函...

python列表list保留顺序去重的实例

常规通过迭代或set方法,都无法保证去重后的顺序问题 如下,我们可以通过列表的索引功能,对set结果进行序列化 old_list=["a",1,"b","a","b",2,5,1]...

python dataframe 输出结果整行显示的方法

在使用dataframe时遇到datafram在列太多的情况下总是自动换行显示的情况,导致数据阅读困难,效果如下: # -*- coding: utf-8 -*- import nu...

Python中列表(list)操作方法汇总

本文实例汇总了Python中关于列表的常用操作方法,供大家参考借鉴。具体方法如下: 一、Python创建列表: sample_list = ['a',1,('a','b')]...

python进程间通信Queue工作过程详解

Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信。 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Que...