Python之条件 循环和其他语句 2014-4-6
#条件 循环和其他语句 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的更多相关文章
- 一步一步学python(五) -条件 循环和其他语句
1.print 使用逗号输出 - 打印多个表达式也是可行的,但要用逗号隔开 >>> print 'chentongxin',23 SyntaxError: invalid synta ...
- python学习笔记2_条件循环和其他语句
一.条件循环和其他语句 1.print和import的更多信息. 1.1.使用逗号输出 //print() 打印多个表达式是可行的,用逗号隔开. 在脚本中,两个print语句想在一行输出 ...
- Python基础教程之第5章 条件, 循环和其它语句
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环 ...
- python基础教程第5章——条件循环和其他语句
1.语句块是在条件为真(条件语句)时执行或者执行多次(循环语句)的一组语句.在代码前放置空格来缩进语句即可穿件语句块.块中的每行都应该缩进同样的量.在Phyton中冒号(:)用来标识语句块的开始,块中 ...
- Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 7. 条件循环
条件循环能够让程序在条件成立时(即为真时)重复执行循环体中的语句.如果条件一直成立(即永远不会为假),则循环会一直进行下去,不会停止.如果初始时,条件不成立,则循环 1 次也不会执行.Python 中 ...
- python学习笔记之四:条件,循环和其他语句
前面已经介绍过几种基本语句(print,import,赋值语句),下面我们来介绍条件语句,循环语句. 一. print和import的更多信息 1.1 使用逗号输出 A.打印多个表达式,用逗号隔开,会 ...
- 【python学习笔记】5.条件、循环和其他语句
[python学习笔记]5.条件.循环和其他语句 print: 用来打印表达式,不管是字符串还是其他类型,都输出以字符串输出:可以通过逗号分隔输出多个表达式 import: 导入模块 impo ...
- python学习笔记(四)、条件、循环及其他语句
1 再谈print和import 1.1 打印多个参数 print 能够同时打印多个表达式,并且能自定义分隔符.如下: print('a','b','c') ——> a b c print('a ...
- python笔记05:条件、循环和其它语句
5.1 print和import的更多使用方式 5.1.1 使用逗号输出 print 'Age',42 print 1,2,3 如果要同时输出文本和变量值,又不希望使用字符串格式化的话,那么这个特性就 ...
随机推荐
- [POI2011]Temperature
Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The mea ...
- 二分查找+数学 HDOJ 4342 History repeat itself
题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...
- AC自动机 HDOJ 5384 Danganronpa
题目传送门 /* 题意:多个文本串,多个模式串在每个文本串出现的次数 AC自动机:这就是一道模板题,杭电有道类似的题目 */ /************************************ ...
- git介绍及安装
git介绍 git是一个开源的分布式版本控制系统,用于敏捷高效的处理任何或大或小的项目.git是linus Torvalds为了帮助管理Linux内核开发的一个开放源码的版本控制软件. Git 与常用 ...
- bootmanager is missing
问题描述: 在计算机管理->存储->磁盘管理中,因误操作,将D盘设置了"将分区标记为活动分区(M)",导致重启时无法无法进入系统,提示"bootmanager ...
- XmlPullParser简单教程
官网: http://www.xmlpull.org 本文参考 : http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html 1 ...
- Access2010 - 数据类型[转]
原文链接 Access允许十种数据类型:文本.备注.数值.日期/时间.货币.自动编号.是/否.OLE对象.超级链接.附件.查询向导 . 文本(Text):这种类型允许最大255个字符或数字,Acces ...
- ORA-00445: Background Process "xxxx" Did Not Start After 120 Seconds
Recent linux kernels have a feature called Address Space Layout Randomization (ASLR).ASLR is a feat ...
- hdu3433A Task Process( 二分dp)
链接 二分时间,在时间内dp[i][j]表示截止到第i个人已经做了j个A最多还能做多少个B #include <iostream> #include<cstdio> #incl ...
- 2556. [NOIP2016]玩具谜题
[题目描述] 小南有一套可爱的玩具小人,它们各有不同的职业.有一天,这些玩具小人把小南的眼镜藏了起来.小南发现玩具小人们围成了一个圈,它们有的面朝国内,有的面朝圈外.如下图: 这时singer告诉小南 ...