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 如果要同时输出文本和变量值,又不希望使用字符串格式化的话,那么这个特性就 ... 
随机推荐
- SQL 初级教程学习(五)
			1.DEFAULT 约束用于向列中插入默认值. CREATE TABLE Orders(Id_O int NOT NULL,OrderNo int NOT NULL,Id_P int,OrderDat ... 
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
			题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ... 
- MyEclipse配置Tomcat
			1.在Window->Perferences->MyEclipse->Servers->Tomcat->Tomcat 7.x中启用Tomcat并配置Tomcat路径和JD ... 
- VS2010环境下.NET4.0中Tuple<T>的一个小BUG问题
			启动一个桌面程序后,发现一个窗体cfdata=null, 执行时发生错误, 但是在初始化的时候,我明明是cfdata=new Cfdata();为什么会出现这个错误呢. 我开始跟踪,发现当执行cfda ... 
- Redis和SpringDataRedis
			一.Redis简介  Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库,运行在内存中,由ANSI C编写.企业开发通常采用Redis来实现缓存.同类的产品还有memcac ... 
- Android学习笔记(八) CheckBox和RadioGroup
			一.CheckBox 1.CheckBox的常用方法: boolean isChecked() :返回当前CheckBox的选中状态 void setChecked(boolean isChecked ... 
- nginx for windows 安装
			一.nginx for windows 的安装地址: http://nginx.org/en/download.html 二.nginx 安装地址: http://nginx.org/en/docs/ ... 
- vue ---- Object的一些常用的方法
			在对象上添加新属性的几种方法: 直接附代码: 法一:Es6扩展运算符添加属性 法二:利用语法Object.assign(target, ...sources) target目标对象.source ... 
- SparkRPC源码分析之RPC管道与消息类型
			SparkRPC源码分析之RPC管道与消息类型我们前面看过了netty基础知识扫盲,那我们应该明白,ChannelHandler这个组件内为channel的各种事件提供了处理逻辑,也就是主要业务逻辑写 ... 
- js  复制文字、 复制链接到粘贴板
			<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ... 
