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的更多相关文章

  1. 【Scala】Scala之Control Structures

    一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Structures Scala中的控制结构与Java中的颇 ...

  2. R Programming week2 Control Structures

    Control Structures Control structures in R allow you to control the flow of execution of the program ...

  3. Scala Control Structures

    Scala之Control Structures 一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Struc ...

  4. Chapter 5 : Control Structures 2 : Repetition

    import java.util.*; import java.io.*; public class Loop { static Scanner console = new Scanner(Syste ...

  5. Chapter 4 : Control Structures 1 : Selection

    Although it need not be, the expression is usually an identifier. Whether it is an identifieror an e ...

  6. [译]The Python Tutorial#5. Data Structures

    [译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...

  7. 《Writing Idiomatic Python》前两部分的中文翻译

    汇总了一下这本小书前两部分的内容: 翻译<Writing Idiomatic Python>(一):if语句.for循环 翻译<Writing Idiomatic Python> ...

  8. 翻译《Writing Idiomatic Python》(一):if语句、for循环

    开篇废话 这是在美国Amazon上评价很不错的一本书,其实严格来说这可能不算书,而是一本小册子.就像书名一样,里面的内容主要是用一些例子讲述地道的Python的代码是怎样写的.书中把很多例子用不良风格 ...

  9. Python:渗透测试开源项目

    Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...

随机推荐

  1. PHP 快速实现大文件上传

    简单的上传代码 最简上传代码 <?php move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES[ ...

  2. 对vue生命周期的理解

    总共分为8个阶段,创建前/后,载入前/后,更新前/后,销毁前/后: 创建前/后:在beforeCreated阶段,vue实例的挂载元素$el和数据对象data都为undefined,还未初始化.在cr ...

  3. RSA 时序攻击

    RSA的破解从理论上来讲是大数质数分解,可是就是有一些人另辟蹊径,根据你解密的时间长短就能破解你的RSA私钥. 举一个不恰当但是比较容易理解的例子: 密文0101 私钥0110 明文0100 问题的关 ...

  4. dedecms前端无法调用自定义变量怎么解决

    网友问ytkah说他的dedecms前端无法调用自定义变量要怎么解决,登录他的网站后台看了一下,自定义变量已经添加了,也写入了数据库表中,但是就是前台没办法调用出来,后面想想可能是文件权限不够,具体是 ...

  5. mysql批量插入数据

    建表 create table `dept`( `id` ) unsigned NOT NULL AUTO_INCREMENT, `deptno` mediumint() unsigned ', `d ...

  6. 小程序要求的 TLS 版本必须大于等于 1.2

    1.打开windows powershell   右击屏幕左下角的开始->所有程序->附件->“Windows PowerShell”.     2.在 PowerShell中运行以 ...

  7. .net core中常用的属性

    //IsNullOrWhiteSpace()判断字符串是否为空 指示指定的字符串是 null.空还是仅由空白字符组成,如果字符串的空白String a="  ";IsNullOrE ...

  8. golang 的 math/big 进行

    进行高进度运算的时候unint64已经无法满足需求,这个时候大家可以采用math.big库来进行高进度计算,下面以计算第10000位的菲波纳切数来展示big的用法,代码如下: package main ...

  9. abap method中的异常处理

    1: 抛出异常 2: 处理异常

  10. (转)ThreadLocal-面试必问深度解析

    ThreadLocal是什么 ThreadLocal是一个本地线程副本变量工具类.主要用于将私有线程和该线程存放的副本对象做一个映射,各个线程之间的变量互不干扰,在高并发场景下,可以实现无状态的调用, ...