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 ...
随机推荐
- cas单点登录用户名为中文的解决办法
当用户名为中文时,登录后返回的用户名乱码.解决这个问题只需要在客户端的CAS Validation Filter中添加下配置就行了. <init-param> <param-name ...
- 如何用adb logcat保存日志
//将log 保存到当前目录下 adb logcat -v time >a.log //log过滤 adb logcat | grep MyAppName //清除log adb logcat ...
- mysql源码:关于innodb中两次写的探索
两次写可以说是在Innodb中很独特的一个功能点,而关于它的说明或者解释非常少,至于它存在的原因更没有多少文章来说,所以我打算专门对它做一次说明. 首先说明一下为什么会有两次写这个东西:因为innod ...
- 【python】继承时注意事项
1. __init__ 注意事项 如果父类有__init__函数,子类没有,则子类自动调用父类__init__函数 如果父类有__init__函数,子类也有,则子类必须主动调用父类__init__函数 ...
- MFC 选择一个文件或者文件夹路径
//选择文件CFileDialog dlg(TRUE, 0, 0, OFN_HIDEREADONLY, "文本文件|*.txt|所有文件|*.*)||",0);if (dlg.Do ...
- VelocityTracker简介
android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率.用addMovement(MotionEvent)函数将Mot ...
- mysql自增字段重排 或 归零
由于删除了某些记录行,所以自增字段不连续了.重排或归零的方法:方法1:truncate table 你的表名//这样不但重新定位自增的字段,而且会将表里的数据全部删除,慎用!方法2:delete fr ...
- 一、HTML和CSS基础--HTML+CSS基础课程--第6部分
第十一章 CSS代码缩写,占用更少的带宽 盒模型代码简写 :还记得在讲盒模型时外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上 ...
- PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)
php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...
- 以多个实例方式打开Notepad++
Right-click any Notepad++ shortcut. Select Properties. Move to the Shortcut tab. In the end of the T ...