#条件 循环和其他语句 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. 水题 Codeforces Round #308 (Div. 2) A. Vanya and Table

    题目传送门 /* 水题:读懂题目就能做 */ #include <cstdio> #include <iostream> #include <algorithm> ...

  2. python版本2和3使用range()函数方法

    python 2:可以直接使用range(5) 输入的列表结果和预期的一样 python 3:使用range(5) 得到列表结果却是这个,和预期的不一致,其原因是节省空间,防止过大的列表产生 如果想要 ...

  3. JS格式化工具(转)

    <html> <head> <title>JS格式化工具 </title> <meta http-equiv="content-type ...

  4. T4869 某种数列问题 (jx.cpp/c/pas) 1000MS 256MB

    题目描述 众所周知,chenzeyu97有无数的妹子(阿掉!>_<),而且他还有很多恶趣味的问题,继上次纠结于一排妹子的排法以后,今天他有非(chi)常(bao)认(cheng)真(zhe ...

  5. css中border制作各种形状

    css利用border制作各种形状的原理如图: 使用border绘制三角形是什么原理?事实上,宽度相等的border是以45度对接的,如下图: 没有了上border如图所示: 再设置border的宽度 ...

  6. javascript中函数的四种调用模式详解

    介绍函数四种调用模式前,我们先来了解一下函数和方法的概念,其实函数和方法本质是一样,就是称呼不一样而已.函数:如果一个函数与任何对象关系,就称该函数为函数.方法:如果一个函数作为一个对象属性存在,我们 ...

  7. SQL——连接查询、聚合函数、开窗函数、分组功能、联合查询、子查询

    连接查询 inner join,用的最多,表示多张表一一对应 聚合函数 操作行数据,进行合并 sum.avg.count.max.min 开窗函数 将合并的数据分布到原表的每一行,相当于多出来了一列, ...

  8. scss常规用法

    保持sass条理性和可读性的最基本的三个方法:嵌套.导入和注释. 一般情况下,你反复声明一个变量,只有最后一处声明有效且它会覆盖前边的值. $link-color: blue; $link-color ...

  9. 【译】x86程序员手册36-9.9异常汇总

    9.9 Exception Summary 异常汇总 Table 9-6 summarizes the exceptions recognized by the 386. Table 9-6. Exc ...

  10. 针对windowsserver 创建iis站点访问出错的解决方案(HTTP 错误 500.19 - Internal Server Error)

    错误如下:   服务器错误 Internet信息服务 7.0 错误摘要HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详 ...