浅谈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微信好友删除的具体代码,供大家参考,具体内容如下 #weixin.py #coding:utf-8 # !/usr/bin/env python #...

Python3基础之list列表实例解析

通常来说Python中任何值都是一个对象,因此任何类型(int、str、list…)都是一个类。而类就必然有它的方法或属性,我们要记下这么多类的所有方法显然是不可能的,对此本文介绍两个小...

使用Python+wxpy 找出微信里把你删除的好友实例

使用Python+wxpy 找出微信里把你删除的好友实例

之前看到好友在发各种"群发"来检验对方是不是把自己删除了,好吧,其实那个没啥用处. 所以决定自己动手做一个 百度了一下,检测是否被删除,总结出大概网上的一些方法 第一种方法: 拉群法 就...

Python查找文件中包含中文的行方法

前言 近几天在做多语言版本的时候再次发现,区分各种语言真的是一件比较困难的事情,上一次做中文提取工具的就花了不少时间,这次决定用python试一试,结果写起来发现真是方便不少,自己整理了...

Python代码解决RenderView窗口not found问题

Python代码解决RenderView窗口not found问题

源起   Error:setParent: Object 'renderView' not found   这是一个在工作中很常见的问题,以前做特效的时候有10%的概率会碰到,多发生在打...