浅谈Python基础—判断和循环

yipeiwu_com6年前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递归遍历列表及输出的实现方法。分享给大家供大家参考。具体实现方法如下: def dp(s): if isinstance(s,(int,str)):...

python进程类subprocess的一些操作方法例子

subprocess.Popen用来创建子进程。 1)Popen启动新的进程与父进程并行执行,默认父进程不等待新进程结束。 复制代码 代码如下: def TestPopen(): &nb...

Python使用matplotlib 模块scatter方法画散点图示例

Python使用matplotlib 模块scatter方法画散点图示例

本文实例讲述了Python使用matplotlib 模块scatter方法画散点图。分享给大家供大家参考,具体如下: # -*-coding:utf-8-*- import matp...

对于Python深浅拷贝的理解

对于Python深浅拷贝的理解

1,浅拷贝是什么? 浅拷贝是对于一个对象的顶层拷贝,通俗的理解是:拷贝了引用,并没有拷贝内容 通过a=b这种方式赋值只是赋值的引用(内存地址),a和b都指向了同一个内存空间,所...

Python-基础-入门 简介

Python简介及入门 python为什么是python 选择自己喜欢的语言,这往往不容易,更多的是根据需求 话说,之前是java,大学用了三年+实习半年,后来入职做测试开发后,碰到了p...