浅谈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使用win32com实现的模拟浏览器功能示例

本文实例讲述了Python使用win32com实现的模拟浏览器功能。分享给大家供大家参考,具体如下: # -*- coding:UTF-8 -*- #!/user/bin/env p...

Python中pygal绘制雷达图代码分享

Python中pygal绘制雷达图代码分享

pygal的安装和简介,大家可以参阅《pip和pygal的安装实例教程》,下面看看通过pygal实现绘制雷达图代码示例。 雷达图(Radar): import pygal rada...

情人节快乐! python绘制漂亮玫瑰

情人节快乐! python绘制漂亮玫瑰

情人节快乐!这个节日怎么会少了浪漫的玫瑰花! 用Python的turtle库绘图是很简单的,画了一个玫瑰花,下面奉上源码: 源码: ''' Created on Nov 18, 2...

python3生成随机数实例

本文实例讲述了python3生成随机数的方法。分享给大家供大家参考。具体实现方法如下: 该实例是根据一本书上看到过一个随机数的小程序,经过自己改动,变为了一个猜数字的小游戏,现在在pyt...

SVM基本概念及Python实现代码

SVM基本概念及Python实现代码

SVM(support vector machine)支持向量机: 注意:本文不准备提到数学证明的过程,一是因为有一篇非常好的文章解释的非常好:支持向量机通俗导论(理解SVM的三层境界)...