Python - 4. Control Structures
From:http://interactivepython.org/courselib/static/pythonds/Introduction/ControlStructures.html
Control Structures
As we noted earlier, algorithms require two important control structures: iteration and selection.
- Iteration
- while
>>> counter = 1
>>> while counter <= 5:
... print("Hello, world")
... counter = counter + 1 Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
- for
>>> for item in [1,3,6,2,5]:
... print(item)
...
1
3
6
2
5
>>> for item in [1,3,6,2,5]:
... print(item)
...
1
3
6
2
5
wordlist = ['cat','dog','rabbit']
letterlist = [ ]
for aword in wordlist:
for aletter in aword:
letterlist.append(aletter)
print(letterlist)
- Selection
Selection statements allow programmers to ask questions and then, based on the result, perform different actions.
- ifelse
if n<0:
print("Sorry, value is negative")
else:
print(math.sqrt(n))
if score >= 90:
print('A')
else:
if score >=80:
print('B')
else:
if score >= 70:
print('C')
else:
if score >= 60:
print('D')
else:
print('F')
if score >= 90:
print('A')
elif score >=80:
print('B')
elif score >= 70:
print('C')
elif score >= 60:
print('D')
else:
print('F')
- if
if n<0:
n = abs(n)
print(math.sqrt(n)) - list comprehension
>>> sqlist=[]
>>> for x in range(1,11):
sqlist.append(x*x) >>> sqlist
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>
>>> sqlist=[x*x for x in range(1,11)]
>>> sqlist
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>
>>> sqlist=[x*x for x in range(1,11) if x%2 != 0]
>>> sqlist
[1, 9, 25, 49, 81]
>>>
>>>[ch.upper() for ch in 'comprehension' if ch not in 'aeiou']
['C', 'M', 'P', 'R', 'H', 'N', 'S', 'N']
>>>
Python - 4. Control Structures的更多相关文章
- 【Scala】Scala之Control Structures
一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Structures Scala中的控制结构与Java中的颇 ...
- R Programming week2 Control Structures
Control Structures Control structures in R allow you to control the flow of execution of the program ...
- Scala Control Structures
Scala之Control Structures 一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Struc ...
- Chapter 5 : Control Structures 2 : Repetition
import java.util.*; import java.io.*; public class Loop { static Scanner console = new Scanner(Syste ...
- Chapter 4 : Control Structures 1 : Selection
Although it need not be, the expression is usually an identifier. Whether it is an identifieror an e ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- 《Writing Idiomatic Python》前两部分的中文翻译
汇总了一下这本小书前两部分的内容: 翻译<Writing Idiomatic Python>(一):if语句.for循环 翻译<Writing Idiomatic Python> ...
- 翻译《Writing Idiomatic Python》(一):if语句、for循环
开篇废话 这是在美国Amazon上评价很不错的一本书,其实严格来说这可能不算书,而是一本小册子.就像书名一样,里面的内容主要是用一些例子讲述地道的Python的代码是怎样写的.书中把很多例子用不良风格 ...
- Python:渗透测试开源项目
Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...
随机推荐
- insert select带来的问题
当使用 insert...select...进行记录的插入时,如果select的表是innodb类型的,不论insert的表是什么类型的表,都会对select的表的纪录进行锁定.对于那些从oracle ...
- 帝国cms修改栏目后文章列表的url错误怎么解决
修改了某个栏目的目录地址,原本是/abc/,现在改成了/ab/,重新生成了栏目页面/ab/和文章页面/ab/*.html,但是栏目页的列表文章url还是/abc/*.html(正确的url应该是/ab ...
- [QGLViewer]3D场景鼠标点击位置
重载鼠标事件: void AxMapControl::mousePressEvent(QMouseEvent* e) { switch(currentTool) { case AX_DRAW_DIRE ...
- 基于UDP/TCP协议的套接字
1.UDP UDP的数据报协议特点是不粘包,非可靠传输 服务端 import socket server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) ...
- 异常:分为 严重性错误:Error 异常:Exception
异常:是在运行时期发生的不正常情况.在java中用类的形式对不正常情况进行了描述和封装对象描述不正常的情况的类,就称为异常类以前:正常流程代码和问题处理代码相结合现在将正常流程代码和问题处理代码分离, ...
- centos7部署fabric-ca错误解决
1.fabric-ca 编译错误:ltdl.h: no such file 在fabric-ca目录中使用make编译时,会出现如下错误: 解决方案: 如果在ubunt操作系统中,只需安装:apt i ...
- Tf中的NCE-loss实现学习【转载】
转自:http://www.jianshu.com/p/fab82fa53e16 1.tf中的nce_loss的API def nce_loss(weights, biases, inputs, la ...
- 【Redis】事务
在Redis中,事务是以multi/exec/discard进行的, 其中multi表示事务的开始, exec表示事务的执行,discard表示丢弃事务. > multi # 事务的开始 OK ...
- [LeetCode] 434. Number of Segments in a String_Easy
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- CentOS7安装Jdk1.8
一.前期准备 a) 首先从官网上下载Jdk 8 for Linux x64到window下. b) 我这边用的最小安装,所以没有安装centos自带的openjdk,如果你安装时,不是最小安装的话,可 ...