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 ...
随机推荐
- poj 2709
http://poj.org/problem?id=2709 题意:就是那个老师需要n瓶颜色的墨水,和1瓶颜色的灰色的墨水,但是灰色的墨水没得卖,只能由三种颜色相同的墨水混合而成,但是3瓶50ML的墨 ...
- poj3904
题意:给出n(n<10000)个数,这些数<=10000,要求选出四个数字且他们的最大公约数为1的(注意:不需要两两互质),有多少种选法. 分析: 容斥原理 假设平面上有一些圆,互相之间有 ...
- Delphi经验总结(3)
------------------------------------------------------- ◇删掉程序自己的exe文件 procedure TForm1.FormClose(Sen ...
- CPinyin unicode汉字查找拼音(支持多音字)
下载代码 --------------------------------------------------------------------------------- 虽然很笨的办法,却非常有效 ...
- codeforces A. Sereja and Bottles 解题报告
题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...
- 在一个JSP页面中包含另一个JSP页面的三种方式
转载自://http://blog.163.com/neu_lxb/blog/static/179417010201121343132918/ (1)include指令 includ ...
- JDK1.7 ConcurrentHashMap 源码浅析
概述 ConcurrentHashMap是HashMap的线程安全版本,使用了分段加锁的方案,在高并发时有比较好的性能. 本文分析JDK1.7中ConcurrentHashMap的实现. 正文 Con ...
- kail ip配置
设置ip /etc/network/interfaces # This file describes the network interfaces available on your system # ...
- Solr的函数查询(FunctionQuery)
作用 通过函数查询让我们可以利用 numeric域的值或者与域相关的的某个特定的值的函数,来对文档进行评分. 如何使用 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr http 接口的 ...
- 今天连续几次被其他电脑客户端踢下线,也不知是否是ip冲突
可能是利用ip然后我的网络也没有关掉共享,其次就是远程开启了一个叫做"学生端"的应用,好在我改密码连续两次改的快