您的位置:首页 > 编程语言 > Python开发

Python之条件 循环和其他语句 2014-4-6

2014-04-06 01:44 489 查看
#条件 循环和其他语句 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的内建函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: