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. win10下切换多个jdk版本

    1.每次切换时,修改JAVA_HOME变量 2.编辑path环境变量,如图所示,将%JAVA_HOME%\jre\bin和%JAVA_HOME%\bin移到最上边 3.在控制面板中打开java控制面板 ...

  2. 洛谷P1315 观光公交 [noip2011D2T3] 贪心

    正解:贪心 解题报告: 这里是链接! 唔我觉得还是很容易想到是贪心的,这个难就难在怎么贪心 下面列一下常见的几个贪心思想: 1)根据车上的人数排序,人最多的那条路用加速器 错误,人数多并不意味着加速的 ...

  3. opencv 替换图像中的一部分

    首先选取图像中的Roi区域,然后对Roi区域进行赋值,那么原图像相应的区域也跟着变化了: dst = src.clone(); cv::Mat Roi(dst, cv::Rect(x, y, cut_ ...

  4. Linux中程序开机自启

    Linux中程序开机自启 https://www.itbulu.com/debian-ubuntu-rclocal.html

  5. ORACLE入门之Linux基础篇

    VIM0 这是数字『0 』:移动到这一行的最前面字符处$    移动到这一行的最后面字符处G    移动到这个档案的最后一行nG   n 为数字.移动到这个档案的第n 行.例如20G 则会移动到这个档 ...

  6. testrem

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. awesome vue

    https://blog.csdn.net/caijunfen/article/details/78216868

  8. Centos7 下 yum -y install ntp 出现/var/run/yum.pid 已被锁定

    [root@localhost ~ ]# yum -y install ntp已加载插件:fastestmirror, langpacksRepodata is over 2 weeks old. I ...

  9. linux sed 常见字符串处理

    1.删除特殊字符 将 1.1.0_boke_1.0.1 转换为110_boke_101 command: new_var=`echo 1.1.0_boke_1.0.1 |sed  s/\.//g` ( ...

  10. 《全栈性能Jmeter》-7JMeter常用脚本开发