流程控制--while
/* while 是在有条件控制的情况下 进行的循环 */
[root@localhost test1]# vim .py
//ADD
#!/usr/bin/python n =
while True:
if n == :
break
print n, 'hello'
n += [root@localhost test1]# python .py
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
[root@localhost test1]# vim .py
//ADD
#!/usr/bin/python while True:
print 'hello'
input = raw_input("Please input something.q for quit: ")
if input == "q":
break [root@localhost test1]# python .py
hello
Please input something.q for quit: e
hello
Please input something.q for quit: r
hello
Please input something.q for quit: t
hello
Please input something.q for quit: f
hello
Please input something.q for quit: q
[root@localhost test1]# vim .py
//add
#!/usr/bin/python x = ''
while x != 'q':
print 'hello'
x = raw_input("Please input something, q for quit: ") /* 首先,要给x定义一个初始值, 然后再继续执行。 这里没有定义输入q后会有什么结果, 但是执行后,输入q则自动退出
*/ [root@localhost test1]# python .py
hello
Please input something, q for quit: s
hello
Please input something, q for quit: s
hello
Please input something, q for quit: d
hello
Please input something, q for quit: f
hello
Please input something, q for quit: g
hello
Please input something, q for quit: q
/* 先创建一个文件 */
[root@localhost tmp]# cat 1.txt
1111 //先用一个变量定义,这个变量对应什么文件
In [1]: ll = open('/tmp/1.txt') In [2]: ll
Out[2]: <open file '/tmp/1.txt', mode 'r' at 0x99d81d8> //将这个文件或变量打开,并赋予w权限
In [4]: ll = open('/tmp/1.txt', 'w') //写变量。括号里为写的内容,此处写的内容会覆盖原来的内容
In [5]: ll.write("aa") [root@localhost tmp]# cat 1.txt
aa[root@localhost tmp]# /* 有时,需要需要将变量关掉才可以执行看到改变 */ /* 再一次打开python的界面需要重新定义变量。*/
In [2]: ll = open('/tmp/1.txt') In [3]: ll.read() //从最开始读
Out[3]: 'aa' In [4]: ll.read() //当第二次读取时,指针已经读完了前面的内容,就只剩空了
Out[4]: ''
/* 读取的具体内容 */ //1. 输入需要读取的 前几个字符
[root@localhost tmp]# cat 1.txt
abc
sjdh[root@localhost tmp]# In [1]: ll = open('/tmp/1.txt' , 'r') In [2]: ll
Out[2]: <open file '/tmp/1.txt', mode 'r' at 0x9392180> In [3]: ll.read(2)
Out[3]: 'ab' // 2.读取一行
In [1]: ll = open('/tmp/1.txt') In [2]: ll.readline()
Out[2]: 'abc\n' // 3.读取多行(有多少行读多少行),并且返回一个list // 4. next() 可对一个文件一行一行的读取
In [5]: ll = open('/tmp/1.txt') In [6]: ll.readlines()
Out[6]: ['abc\n', 'sjdh'] In [1]: ll = open('/tmp/1.txt') In [2]: ll.next()
Out[2]: 'abc\n' In [3]: ll.next()
Out[3]: 'sjdh' In [4]: ll.next()
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-4-e9a4e3e4293f> in <module>()
----> 1 ll.next() StopIteration:
/* python脚本,对文件进行遍历 */ [root@localhost test1]# vim 16.py
//ADD
#!/usr/bin/python ll = open('/tmp/1.txt')
for line in ll.readlines():
print line, [root@localhost test1]# python 16.py
abc
sjdh
流程控制--while的更多相关文章
- 第10章 Shell编程(4)_流程控制
5. 流程控制 5.1 if语句 (1)格式: 格式1 格式2 多分支if if [ 条件判断式 ];then #程序 else #程序 fi if [ 条件判断式 ] then #程序 else # ...
- Shell命令和流程控制
Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...
- PHP基础知识之流程控制的替代语法
PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch. 替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,e ...
- Python黑帽编程2.4 流程控制
Python黑帽编程2.4 流程控制 本节要介绍的是Python编程中和流程控制有关的关键字和相关内容. 2.4.1 if …..else 先上一段代码: #!/usr/bin/python # - ...
- 使用yield进行异步流程控制
现状 目前我们对异步回调的解决方案有这么几种:回调,deferred/promise和事件触发.回调的方式自不必说,需要硬编码调用,而且有可能会出现复杂的嵌套关系,造成"回调黑洞" ...
- [Java入门笔记] Java语言基础(四):流程控制
流程控制指的是在程序运行的过程中控制程序运行走向的方式.主要分为以下几种: 顺序结构 顺序结构,顾名思义,是指程序从上往下逐步顺序执行.中间没有任何的判断和跳转. 分支结构 Java提供两种分支结构: ...
- node基础13:异步流程控制
1.流程控制 因为在node中大部分的api都是异步的,比如说读取文件,如果采用回调函数的形式,很容易造成地狱回调,代码非常不容易进行维护. 因此,为了解决这个问题,有大神写了async这个中间件.极 ...
- Shell入门教程:流程控制(1)命令的结束状态
在Bash Shell中,流程控制命令有2大类:“条件”.“循环”.属于“条件”的有:if.case:属于“循环”的有:for.while.until:命令 select 既属于“条件”,也属于“循环 ...
- Oracle中PL/SQL的执行部分和各种流程控制
Oracle中PL/SQL的执行部分和异常部分 一.PL/SQL的执行部分. 赋值语句. 赋值语句分两种,一种是定义一个变量,然后接收用户的IO赋值:另一种是通过SQL查询结果赋值. 用户赋值举例: ...
- swift_简单值 | 元祖 | 流程控制 | 字符串 | 集合
//: Playground - noun: a place where people can play import Cocoa var str = "Hello, playground& ...
随机推荐
- 【题解】SCOI2008配对
贪心+dp~观察数据,发现一个规律:将数字排序之后,最优匹配只可能产生在该数字和与它距离不超过二的数字之间. 所以可以用dp[i]代表前i个数(排序)匹配的最小差值,之后暴力选出该新数应该如何匹配. ...
- 一些技巧 && 常数优化 && 出现の错误
开坑原因 7.21 今天DTZ大爷教了我一个算欧拉函数的好方法......是质因数复杂度的 这让我想到,这些小技巧小idea,很多时候,可能就是考场上最致命.最一击必杀的"大招" ...
- BZOJ5157 & 洛谷3970:[TJOI2014]上升子序列——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5157 https://www.luogu.org/problemnew/show/P3970 给定 ...
- BZOJ 1342: [Baltic2007]Sound静音问题 | 单调队列维护的好题
题目: 给n个数字,一段合法区间[l,l+m-1]要求max-min<=c 输出所有合法区间的左端点,如果没有输出NONE 题解: 单调队列同时维护最大值和最小值 #include<cst ...
- Android源码4.4.4_r1下载和编译
系统:ubuntu 16.04.2 TLS 1.源码下载: sudo apt-get install curl curl https://storage.googleapis.com/git-repo ...
- JavaScript身份证号码有效性验证
最近需要对身份证合法性进行验证,实名验证是不指望了,不过原来的验证规则太过简单,只是简单的验证了身份证长度,现在业务需要加强下身份证验证规则,网上找到了不少资料,不过都不合偶的心意,无奈只好直接写一个 ...
- New Year and Domino 二维前缀和
C. New Year and Domino time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- HDU 3081 最大流+二分
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- arm架构与体系结构
1.cpu与soc 内核版本号与soc版本号由arm公司确定. 芯片型号由各半导体公司确定. soc包括cpu与一些基本内设.(一般提到CPU都指的是soc,实际上cpu只是soc的一部分). RIS ...
- maven工程pom.xml报Missing artifact net.sf.jasperreports:jasperreports:jar:6.2.0
有时maven工程的pom.xml报以下类型错误: Description Resource Path Location TypeMissing artifact net.sf.jasperrepor ...