python中的循环
>>> 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中的循环的更多相关文章
- 详解Python中的循环语句的用法
一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...
- python 中 for 循环 if循环 break
python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...
- 一文了解Python中的循环(for while break continue 嵌套循环...)
循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...
- python中for循环的底层实现机制 迭代
在python中,存在2种循环方式:for循环和while循环. while循环的实现很简单, 其本质就是一个条件语句,自定义条件,当条件满足的时候,不断执行while代码块. 但是for循环,究竟是 ...
- python中的循环以及,continue和break的使用
循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...
- python中while循环和for循环的定义和详细的使用方法
1. 循环的定义,反复做某事,具有明确的开始和结束. 2. 在Python中循环有while和for两种方式: While循环:1) 语法结构 >>> while 条件: ... ...
- python中的循环和编码,运算符, 格式化输出
1.while循环 现在让我们来看看python中的while循环 格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...
- Python中的循环语句
Python中有while循环和for循环 下面以一个小例子来说明一下用法,用户输入一些数字,输出这些数字中的最大值和最小值 array = [5,4,3,1] for i in array: pri ...
- for和while——python中的循环控制语句详解
循环语句在绝大多数的语言中,都是必不可少的一种控制语句,循环语句允许我们执行一个语句或语句组多次.在python中有for循环和while循环两种,讲到这里,就不得不提到我们的迭代器对象 迭代器 迭代 ...
- Python中的循环与跳出
--start-- for循环: for i in range(3): user_input = input("Your username:") passwd = int(inpu ...
随机推荐
- (转)SQL Server 的事务和锁(二)-Range S-S锁
在这篇随笔中,我们的主要关注点在 Key-Range Lock.Key-Range Lock有 S-S.S-U.I-N.X-X几种情况.我们一个一个来说,力求明白.遗憾的是,这里可能会比较冗长,那么死 ...
- Java for LeetCode 042 Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 6.原型模式(Prototype Pattern)
using System; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { // 孙 ...
- php变量的几种写法
一.最简单的 $str = 'Hello World!'; 二.来个变种 $str = 'good'; $good = 'test'; $test = 'Hello World!'; echo $$$ ...
- Python中通过cx_oracle操作ORACLE数据库的封闭函数
哈哈,看来我的SQL自动化发布,马上就全面支持ORACLE,MYSQL,POSTGRESQL,MSSQL啦... http://blog.csdn.net/swiftshow/article/deta ...
- oracle 卸载步骤(图解)
1.卸载第一步:点击开始菜单: 步骤阅读 2 2.点击Universal Installer来实现下面 步骤阅读 3 3.点击卸载产品后出现的内容: 步骤阅读 4 4.点击打开Oracle主目录下的O ...
- 关于android LinearLayout的比例布局(转载)
关于android LinearLayout的比例布局,主要有以下三个属性需要设置: 1,android:layout_width,android:layout_height,android:layo ...
- matlab参数查询
nargout nargout的作用是在matlab中定义一个函数时, 在函数体内部, nargout指出了输出参数的个数(nargin指出了输入参数的个数). 特别是在利用了可变参数列表的函数中, ...
- 如何应对Session丢失》》State Server
Session丢失已经是一种习以为常的问题了,在自己也了解一些如何解决的问题,但是也一直没有机会去用,现在由于新的项目要在B/S下开发,所以不得不让我考虑Session的问题. 解决session丢失 ...
- oracle的常用函数
1. nvl NVL函数的格式如下:NVL(expr1,expr2) 含义是:如果oracle第一个参数expr1为空,那么显示第二个参数的值为expr2,如果第一个参数的值expr1不为空,则显示第 ...