>>> x = 100
>>> y = 10
>>> x < y and x or y
10
>>> x if x > y else y
100

if 语句:

>>> x = 10
>>> if x == 10:
... print(x)
...
10

>>> if x == 10:
... print(x)
... else:
... print("x not 10!")
...
10

>>> if x == 10:
... print(x)
... elif x == 100:
... print(x)
... else:
... print(x)
...
10

while循环:

super@super:/python$ cat w.py
#!/usr/bin/env python

list1=[1,2,3,4,5,6,7,8,9,10]
sum = 0
while list1:
    sum += list1[0]
    list1 = list1[1:]
print(sum)

super@super:/python$ python w.py
55

for循环:

super@super:/python$ cat f.py 
#!/usr/bin/env python

list1 = [1,3,5,6,9]
for i in list1:
    print(i)

super@super:/python$ python f.py
1
3
5
6
9

else语句:

super@super:/python$ cat f.py
#!/usr/bin/env python

list1 = [1,3,5,6,9]
for i in list1:
    print(i)
else:
    print("while is normal over!")

super@super:/python$ python f.py
1
3
5
6
9
while is normal over!

break语句:

super@super:/python$ cat f.py
#!/usr/bin/env python

list1 = [1,3,5,6,9]
for i in list1:
    print(i)
    break
else:
    print("while is normal over!")
super@super:/python$ python f.py
1

continue语句:

super@super:/python$ cat f.py
#!/usr/bin/env python

list1 = [1,3,5,6,9]
for i in list1:
    if i == 5:
        continue
    print(i)
else:
print("while is normal over!")
super@super:/python$ python f.py
1
3
6
9
while is normal over!

python中的循环的更多相关文章

  1. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  2. python 中 for 循环 if循环 break

    python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...

  3. 一文了解Python中的循环(for while break continue 嵌套循环...)

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...

  4. python中for循环的底层实现机制 迭代

    在python中,存在2种循环方式:for循环和while循环. while循环的实现很简单, 其本质就是一个条件语句,自定义条件,当条件满足的时候,不断执行while代码块. 但是for循环,究竟是 ...

  5. python中的循环以及,continue和break的使用

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...

  6. python中while循环和for循环的定义和详细的使用方法

    1. 循环的定义,反复做某事,具有明确的开始和结束.   2. 在Python中循环有while和for两种方式: While循环:1) 语法结构 >>> while 条件: ... ...

  7. python中的循环和编码,运算符, 格式化输出

    1.while循环 现在让我们来看看python中的while循环  格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...

  8. Python中的循环语句

    Python中有while循环和for循环 下面以一个小例子来说明一下用法,用户输入一些数字,输出这些数字中的最大值和最小值 array = [5,4,3,1] for i in array: pri ...

  9. for和while——python中的循环控制语句详解

    循环语句在绝大多数的语言中,都是必不可少的一种控制语句,循环语句允许我们执行一个语句或语句组多次.在python中有for循环和while循环两种,讲到这里,就不得不提到我们的迭代器对象 迭代器 迭代 ...

  10. Python中的循环与跳出

    --start-- for循环: for i in range(3): user_input = input("Your username:") passwd = int(inpu ...

随机推荐

  1. linux ldconfig

    http://blog.csdn.net/dante_k7/article/details/7211868 ldconfig的主要用途: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/l ...

  2. Linux shell脚本编程基础之练习篇

    shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...

  3. Enum:Smallest Difference(POJ 2718)

    最小的差别 题目大意:输入一串数字,问你数字的组合方式,你可以随意用这些数字组合(无重复)来组合成一些整数(第一位不能为0),然后问你组合出来的整数的差最小是多少? 这一题可以用枚举的方法去做,这里我 ...

  4. c++ template函数的声明和实现需要在同一个文件中

    新建一个class C;生成2个文件C.h和C.cpp,在C.h中声明一个函数 template<class T> T stringTo(char* str); 直接用VAssistX的R ...

  5. Emblog 备忘

    emblog换后台:(如admin-->xx) 1.xx目录下的globals.php打开找到替换admin(1) 2.www目录下的t\index.php 中找到替换admin(1) 3.ww ...

  6. 在有EditText控件的AlertDialog对话框中自动弹出输入法

    我们先回顾一下创建AlertDialog的一般步骤. 一 inflate AlertDialog的布局文件   例如,其中dlg就是我们的布局文件.    View layout = LayoutIn ...

  7. Net4.0---AspNet中URL重写的改进(转载)

    转载地址:http://www.cnblogs.com/oec2003/archive/2010/07/27/1785862.html URL重写有很多的好处,如有利于SEO.便于记忆.隐藏真实路径使 ...

  8. linux tricks 之 roundup.

    转载:http://stackoverflow.com/questions/1010922/question-about-round-up-macro 以下内容转载自stackoverflow关于 r ...

  9. Java Hour 21 Weather

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为21 Hour,请各位不吝赐教. 继续心情不佳,那 ...

  10. [译] UML中的关系之Dependency

    在UML中,依赖关系表示Client依赖于另一个元素,叫做Supplier. 通常来说,依赖关系不需要特殊的名字. 依赖的类别 抽象 abstraction, derive, refine, trac ...