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 如果要同时输出文本和变量值,又不希望使用字符串格式化的话,那么这个特性就 ...
随机推荐
- [Usaco2005 Jan]Muddy Fields泥泞的牧场
Description 雨连续不断的击打了放牛的牧场,一个R行C列的格子(1<=R<=50,1<=C<=50).虽然这对草来说是件好事,但这却使得一些没有草遮盖的土地变得很泥泞 ...
- TCP模型,控制标志,握手,挥手,长连接*
1. TCP协议 Transmission Control Protocol,传输控制协议 面向连接的协议 需要三次握手建立连接 需要四次挥手断开连接 TCP报头最小长度:20字节 2.模型图 3.T ...
- 比较C#中几种常见的复制字节数组方法的效率[转]
[原文链接] 在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadByte ...
- mysql各个版本下载地址
之所记录下来是因为我找了好久才找到:这下记着了:http://downloads.mysql.com/archives/community/ 希望对没有找到的朋友有帮助
- React.js 基本环境安装
安装 React.js React.js 单独使用基本上是不可能的事情.不要指望着类似于 jQuery 下载放到 <head /> 标签就开始使用.使用 React.js 不管在开发阶段生 ...
- JS格式化工具(转)
<html> <head> <title>JS格式化工具 </title> <meta http-equiv="content-type ...
- AJPFX学习笔记JavaAPI之String类
学习笔记JavaAPI之String类 [size=10.5000pt]一.所属包java.lang.String,没有子类.特点:一旦被初始化就不可以被改变. 创建类对象的两种方式: String ...
- 移动设备访问使用百度js跳转
以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.bai ...
- scala打印error,debug,info
1.以wordcount为例 package org.apache.spark.examples import org.apache.spark.examples.SparkPi.logger imp ...
- 手机酷派4G5316 5313s 黑砖 求转成功 9008端口 9006端口 少走弯路选对镜像
首先要有资料 里面有教程 http://pan.baidu.com/s/1bpjxP6n 1.用其他手机 or u 盘往sd卡放进“强制进入下载模式的文件” 2. 驱动 3.刷机工具 下载镜像 ...