#条件 循环和其他语句 23:30pm-1:43
1.print和import的更多信息
使用逗号将多个表达式输出
>>> print 'age:',42
age: 42

>>> name="tom"
>>> salution="Mr"
>>> greeting='hello'
>>> print greeting,salution,name
hello Mr tom

print greeting,',',salution,name
->会在逗号前加入空格
建议使用
print greeting+',',salution,name

把某件事作为另一件事导入
import somemodule或者
from somemodule import somefunction
或者
from somemodule import somefunction,anotherfunction,yetanotherfunction
或者
from somemodule import *

两个模块都有open函数
module1.open(...)
module2.open(...)
或者语句末尾增加一个as子句 在该子句后给出名字 或为整个模块提供别名
import math as foobar
foobar.sqr(4)
2.0
亦可以为函数提供别名
>>>from math import sqrt as foobar
>>>foobar(4)
2.0
对于open可以这么使用
from module1 import open as open1
from module2 import open as open2

2.赋值魔法
序列解包
>>> x,y,z=1,2,3
>>> print x,y,z
1 2 3

>>> values=1,2,3
>>> values
(1, 2, 3)
>>> x,y,z=values
>>> x
1

链式赋值
x=y=xfunction()
等同于
y=xfunction()
x=y
不一定等价于
x=xfunction()
y=xfunction()

增量赋值
>>> x=2
>>> x+=1
>>> x
3
>>> x*=2
>>> x
6
字符串形式
>>> fnord='foo'
>>> fnord+='bar'
>>> fnord
'foobar'

3.语句块:缩排的乐趣
4.条件和条件语句
false None 0 "" ''() [] {}都会看作为假
false返回0
true返回1
>>> bool(43)
True
>>> bool('')
False

条件执行和if语句
else 语句
name=raw_input('what is your name:')
if name.endswith('hellen'):
print 'hello,hellen'
else:
print 'hello,stranger'
>>>what is your name:hellen
hello,hellen

elif(else if)
num=input('Enter a number:')
if num>0:
print 'the number is positive'
elif num < 0:
print 'the number is negative';
else:
print 'the number is zero';

>>>Enter a number:0
the number is zero

嵌套代码块

更复杂的条件
比较运算符
==全等于
<小于
>大于
<=小于等于
>=大于等于
!=不等于
x is y x和y是同一个对象
x is not y x和y不是同一个对象
x in y x是y容器(序列)的成员
x not in y x不是y容器(序列)的成员
0<age<100可以多个连用>>

num=input('enter a number between 1 and 10:')
if num<=10 and num>=1:
print 'great'
else:
print 'wrong'
>>>enter a number between 1 and 10:4
great

断言
if not condition:
crash program
assert 判断错误的时候 让它出现
>>> age=10
>>> assert 0<age<100
>>> age=-1
>>> assert 0<age<100

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
assert 0<age<100
AssertionError

>>> age=-1
>>> assert 0<age<100, 'the age must be realistic
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
assert 0<age<100, 'the age must be realistic'
AssertionError: the age must be realistic
>>>

5.循环
while循环
x=1
while x<100:
print x
x+=1
>>>

name=''
while not name:
name=raw_input('enter your name:')
print 'hello,%s!'%name
如果不输入名字 而是按下回车键 那么
enter your name:
enter your name:
enter your name:
enter your name:

for循环
words=['hell0','good','morning']
for word in words:
print word

>>>
hell0
good
morning

迭代(循环的另外一种说法)
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
range下限为0
>>>range(10)
[0,1, 2, 3, 4, 5, 6, 7, 8, 9]
打印1-100的数字
for number in range(1,101):
print number

循环遍历字典元素
d={'x':1,'y':2}
for key in d:
print key,'==>',d[key]
>>>
y ==> 2
x ==> 1

一些迭代工具
(1)并行迭代
names=['anne','beth','george','tom']
age=[12,23,34,102]
打印出对应的名字和年龄
for i in range(len(names)):
print names[i],'is',age[i],'years old'

?zip 做啥用的

(2)编号迭代
迭代序列中的对象 同时还要获取其索引
index=0
for string in strings:
if 'xxx' in string:
strings[index]='[consored]'
index+=1

(3)翻转和排序迭代
sorted([4,5,1,3])
[1,3,4,5]

跳出循环
(1)break
寻找100以内的最大平方的数
from math import sqrt
for n in range(99,0,-1):
root=sqrt(n)
if root==int(root):
print n
break
>>>81
(2)continue
跳过本次循环 继续下一次循环
(3)while True/break
使用while做多功能的问题
word='dummy'
while word:
word=raw_input('please enter a word: ')
print 'the word was '+word
>>>
please enter a word: hello
the word was hello
please enter a word: tom
the word was tom
please enter a word:
不断要求输入

while True:
word=raw_input('please enter a word: ')
if not word:break
print 'the word was '+word

while True 实现了一个永远不会自己停止的循环 但是条件内部用 if,条件满足的时候可以调用break 终止循环

else语句

6.列表推导式--轻量级循环
>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> [x*x for x in range(10) if x%3==0]
[0, 9, 36, 81]

7.其他语句
pass用来占位 啥也不用做
del删除对象
使用exec和eval执行和求值字符串--注意其安全性
执行一个字符串的语句是exec:
exec "print 'hello,world'"
hello,world
eval(用于“求值”)是类似于exec的内建函数

Python之条件 循环和其他语句 2014-4-6的更多相关文章

  1. 一步一步学python(五) -条件 循环和其他语句

    1.print 使用逗号输出 - 打印多个表达式也是可行的,但要用逗号隔开 >>> print 'chentongxin',23 SyntaxError: invalid synta ...

  2. python学习笔记2_条件循环和其他语句

    一.条件循环和其他语句 1.print和import的更多信息. 1.1.使用逗号输出  //print() 打印多个表达式是可行的,用逗号隔开.       在脚本中,两个print语句想在一行输出 ...

  3. Python基础教程之第5章 条件, 循环和其它语句

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环 ...

  4. python基础教程第5章——条件循环和其他语句

    1.语句块是在条件为真(条件语句)时执行或者执行多次(循环语句)的一组语句.在代码前放置空格来缩进语句即可穿件语句块.块中的每行都应该缩进同样的量.在Phyton中冒号(:)用来标识语句块的开始,块中 ...

  5. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 7. 条件循环

    条件循环能够让程序在条件成立时(即为真时)重复执行循环体中的语句.如果条件一直成立(即永远不会为假),则循环会一直进行下去,不会停止.如果初始时,条件不成立,则循环 1 次也不会执行.Python 中 ...

  6. python学习笔记之四:条件,循环和其他语句

    前面已经介绍过几种基本语句(print,import,赋值语句),下面我们来介绍条件语句,循环语句. 一. print和import的更多信息 1.1 使用逗号输出 A.打印多个表达式,用逗号隔开,会 ...

  7. 【python学习笔记】5.条件、循环和其他语句

    [python学习笔记]5.条件.循环和其他语句 print: 用来打印表达式,不管是字符串还是其他类型,都输出以字符串输出:可以通过逗号分隔输出多个表达式 import: 导入模块     impo ...

  8. python学习笔记(四)、条件、循环及其他语句

    1 再谈print和import 1.1 打印多个参数 print 能够同时打印多个表达式,并且能自定义分隔符.如下: print('a','b','c') ——> a b c print('a ...

  9. python笔记05:条件、循环和其它语句

    5.1 print和import的更多使用方式 5.1.1 使用逗号输出 print 'Age',42 print 1,2,3 如果要同时输出文本和变量值,又不希望使用字符串格式化的话,那么这个特性就 ...

随机推荐

  1. python 关闭redis的连接

    在python语言中使用redis时,没有找到对应的关闭的方法 try: self.redisconn = StrictRedisCluster(startup_nodes=self.redisNod ...

  2. [Usaco2008 Dec]Patting Heads 轻拍牛头

    Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号 ...

  3. codevs 3044 矩形面积求并 || hdu 1542

    这个线段树的作用其实是维护一组(1维 平面(?) 上的)线段覆盖的区域的总长度,支持加入/删除一条线段. 线段树只能维护整数下标,因此要离散化. 也可以理解为将每一条处理的线段分解为一些小线段,要求每 ...

  4. Activity的onSaveInstanceState和onRestoreInstanceState触发的时机

    转自:http://www.cnblogs.com/heiguy/archive/2010/10/30/1865239.html 1.原文 先看Application Fundamentals上的一段 ...

  5. hibernate 中createQuery与createSQLQuery(转载)

    息: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.miracle.dm.doc.catalog.mo ...

  6. iOS 集成银联支付(绕过文档的坑,快速集成)-转

    本文是投稿文章,作者:南栀倾寒当初集成支付宝的时候,觉得见了这么丑的代码,加上这么难找的下载地址,在配上几乎为零的文档,寒哥就要吐血了. 下午去集成银联,才知道血吐的早了. 下载地址:https:// ...

  7. 基于CentOS6.5下如何正确安装和使用Tcpreplay来重放数据(图文详解)

    前期博客 基于CentOS6.5下snort+barnyard2+base的入侵检测系统的搭建(图文详解)(博主推荐) tcpreplay是什么? 简单的说, tcpreplay是一种pcap包的重放 ...

  8. node入门(一)——安装

    node可以让我们用js写服务器.此外还可以用来前端自动化开发,它找到特定服务要使用的包,然后下载.安装.管理. 首先安装node,进入官网下载需要的node版本,然后一键式傻瓜安装.(我的环境是wi ...

  9. Android开发-下载网络图片并显示到本地

    Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...

  10. [翻译] API测试最佳实践 - 组织你的测试

    组织你的测试 适用级别:初学者 在最底层,一个测试步骤(Test Step)用来验证一个单独的操作.组合若干测试步骤到测试用例,允许你验证那些被分隔出来的一个一个的功能,这些功能是应用程序所需要的.接 ...