《python基础教程》读书笔记第五章-条件、循环和其他

1.import功能

import somemodule

或者

from somemodule import somefunction

或者

from somemodule import somefunction , anotherfunction,yetanotherfunction

或者

form somemodule import *

给导入模块取别名

import somemodule as xxxxmodule

from somemodule import somefunction as xxxfunction

eg.

>>> import math as testmodule

>>> testmodule.sqrt(9)

3.0

>>> from math import sqrt as test

>>> test(9)

3.0

2.赋值魔法

序列解包(sequence unpacking)

交换

eg.

>>> x,y,z = 1,2,3

>>> x,y,z

(1, 2, 3)

>>> x,y,z = z,x,y

>>> x,y,z

(3, 1, 2)

元组赋值

>>> myinfo

{'tel': '18081953671', 'name': 'Bruce'}

>>> key,value=myinfo.popitem()

>>> key,value

('name', 'Bruce')

3.0版本中有一个特殊用法

a,b,rest*=[1,2,3,4,5,6],赋值结果a=1,b=2,剩余的值收集道rest中

链式赋值

x=y=somefunction()

等效于

y=somefunction()

x=y

增量赋值

x=6

x += 2  x -= 3 x *= 4

对其他数据类型同样适用

>>> x = 'bruce'

>>> x += ' study'

>>> x

'bruce study'

>>> x *= 2

>>> x

'bruce studybruce study'

3.条件和条件语句

bool类型

>>> True

True

>>> False

False

>>> True==1

True

>>> False==0

True

bool函数

>>> bool('Bruce study python')

True

>>> bool(20)

True

>>> bool('')

False

>>> bool(0)

False

条件执行 if elif else

if 条件:

      语句1

      语句2

      ....

elif:

      语句1

      语句2

      ....

else:

      语句1

      语句2

      ....

于其他语言不同的比较

x is y    x和y是同一个对象?

x is not y x和y是不同的对象?

x in y y是x容器

x not in y y不是x容器

== 和 is的区别:==比较两个对象是否相等,is 比较两个对象是否是同一个对象

>>> m=[1,2]

>>> n=[1,4]

>>> m==n

False

>>> m is n

False

>>>n[1]=2

>>>m=n

>>>True

>>>m is n

>>>False


in 成员运算符

字符串和序列比较

bool运算符

断言assert

4.循环

while循环

x=1

while x<=100

print x

x +=1

for循环

for a in b

  xxxx

  xxxx


迭代工具

zip函数

>>> name=['nancy','bruce','pipi','popo']

>>> age=[28,34,2,61]

>>> zip(name,age)

[('nancy', 28), ('bruce', 34), ('pipi', 2), ('popo', 61)]


enumerate函数

翻转和排序迭代

>>> a=[2,3,7,2,3,9,5]

>>> sorted(a)

[2, 2, 3, 3, 5, 7, 9]

>>> list(reversed(a))

[5, 9, 3, 2, 7, 3, 2]

注意,sorted函数并没有改变a这个列表,reversed函数也没有改变a列表本身


循环跳出

break语句 continue语句

for x in seq:

  if condition1:continue

  if condition1:continue

  if condition1:continue

  do_something()

  .....

  .....

自己写的简单程序

while True:

if name != 'Bruce':

  name = raw_input('input your name:')

else:

  if password != '123456':

  password = raw_input('input your password:')

  else:

  print 'you have input the right name and password!'

  name=password='' 

  continue

if name == 'over':

  break

列表推倒式

利用其他列表创建薪列表的一种方法


pass,del,exec语句

pass 什么都不做 跟nop类似,作用是当部分代码未完成而需要代码来填充格式

del 删除那些不再使用的对象


exec和eval

书上说这两个函数要慎用


小结:

1.打印

2.导入

3.赋值

4.块

5.条件

6.断言

7.循环

8.列表推倒式

9.pass del exec eval语句

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,510评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,857评论 0 6
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,764评论 18 399
  • 我的恐惧是一丛刺 默默地长在缝隙 你要是被它扎住 千万啊!不要讶异! 它是我长久的侣伴 眼里住着沉积的秘密 它恋着...
    苏格拉李阅读 186评论 2 3