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服务器压力测试工 ...
随机推荐
- ORACLE监听配置及测试实验(2)
实验四 在tnsname.ora里添加默认监听代号 [oracle@oracle01 admin]$ vi tnsnames.ora 添加一行 PORT1528=(ADDRESS = (PROTOCO ...
- FTP主动模式和被动模式的区别(转)
dd by zhj: 一般使用被动模式,在命令行下,被动模式的格式是:ftp -p (yinservice_env) ajian@ubuntu-desk:~$ ftp -pftp> 之前在用命令 ...
- Matlab中添加路径与去除路径
今天在使用Matlab调用内部的PCA函数的时候,报错: 错误使用 pca输入参数太多. 如下图所示: 网上查找原因之后发现是因为我之前下载过开源的工具包toolbox,并且将它的路径add到了Mat ...
- springMVC(二): @RequestBody @ResponseBody 注解实现分析
一.继承结构 @RequestBody.@ResponseBody的处理器:RequestResponseBodyMethodProcessor @ModelAttribute处理器: ModelAt ...
- 协程.md
一. # 可迭代l = [ i for i in range(10) ] # l是可迭代的,但不是迭代器for idx in l: print(idx) # range是个迭代器for i ...
- 安全易用的云许可-VirboxLM许可管理平台
Virbox LM是深思推出的基于云许可管理的开放平台,旨在为开发者提供低成本.高强度.操作便捷的一站式软件保护方案. Virbox LM包括用户许可管理工具.加壳工具.API帮助工具.开发商管理工具 ...
- javascript篇-slice(),splice(),split(),substring(),substr()的用法以及区别
1.slice(),从已经有的数组中返回选定的元素, 使用范围是:Array,string 语法:obj.slice(start,end) 参数: start: 必需.规定从数组(字符串)的哪个ind ...
- PHP自动加载SPL的四种处理方式
libs目录下有3个类文件: Test.class.php <?php class Test { public function __construct() { echo "Loadi ...
- 开发十年,只剩下这套Java开发体系了
蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力.培训.出国.大公司的经历,还有很多很好的朋友.但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为一个优秀的程序员,可惜 ...
- SAX解析器
1.继承类DefaultHandler package com.SSLSocket.test; import org.xml.sax.Attributes; import org.xml.sax.SA ...