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服务器压力测试工 ...
随机推荐
- npm的源改成淘宝镜像
修改源地址为淘宝 NPM 镜像npm config set registry http://registry.npm.taobao.org/ 修改源地址为官方源npm config set regis ...
- [MySQL 5.6] information_schema.innodb_metrics
1. 概括 已关闭/打开的配置 use information_schema select count(*), status from innodb_metrics group by status; ...
- python练习题-day2
1.判断下列逻辑语句的True,False 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 True ...
- 前端 HTML body标签相关内容 常用标签 段落标签 p标签
段落标签 <p>,paragraph的简写.定义段落,默认段落之间有间隔的 浏览器展示特点: 跟普通文本一样,但我们可以通过css来设置当前段落的样式 是否又独占一行呢? 答案是的 块级元 ...
- win10安装pycharm及汉化包
PyCharm 是一款功能强大的 Python 编辑器,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,那么如何安装pycharm呢?都是英文看不懂有没有汉化版呢?跟ytkah一起 ...
- what's the python之python介绍
其实这一篇文章的大部分都是啰嗦话,大部分在百度百科中都有详尽的叙述.既然决定学python了就要风雨兼程,你不用洞悉python到底是什么,你只要知道这是一门编程语言,跟Java.C++等语言一样都是 ...
- Mysql事件监控日志
建立监控表: CREATE TABLE `t_event_history` ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, `dbname ...
- GUI库之认识Tkinter(一)
一.介绍 Tkinter是Python默认的GUI库,我们经常使用的IDLE就是用Tkinter设计出来的,因此我们在使用的时候直接导入Tkinter模块就好了. 1.特点:可移植性.灵活性高 2.构 ...
- 静态文件link 数据库迁移命令 新建app命令
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"& ...
- vue 验证码倒计时
//html <div class="input-div" v-show="formData.phone"> <input type=&quo ...