首页 技术 正文
技术 2022年11月19日
0 收藏 300 点赞 4,284 浏览 2472 个字

《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)

print ‘AB’, 123 ==> AB 123 # 插入了一个空格
print ‘AB’, ‘CD’ ==> AB CD # 插入了一个空格
print 1,2,3 ==> 1 2 3
print (1,2,3) ==> (1, 2, 3)
#在脚本中以下ABCD连在一起输出
print ‘AB’,
print ‘CD’

import somemodule #导入模块
from somemodule import somefunction #导入函数
from somemodule import function1,function2… #导入函数
from somemodule import *
module1.open() #使用模块中函数
module2.open()
import math as foobar #为模块提供别名
foobar.sqrt(4)
from math import sqrt as foobar #为函数提供别名
foobar(4)

序列解包
x,y,z=1,2,3; print x,y,z ==> 1 2 3
x,y,z=1,2,3; x,y=y,z; print x,y ==> 2 3
x=1,2,3; a,b,c=x; print a,b,c ==> 1 2 3
d={‘key1′:’value1′,’key2′:’value2’}; key,value=d.popitem(); print key,value ==> key2 value2

链式赋值
x=y=z={‘key1′:’value1’}; x is y ==> True
x=y=z={‘key1′:’value1’}; x==y  ==> True

增量赋值
x+=1; x*=1;

可以用tab或空格分层缩进代码;推荐用四个空格来缩进。用Tab缩进更方便,比空格更快。

: 表示语句块的开始

布尔值:True, False, bool()
这些被看为False: False, None, 0, “”, (), [], {}
True==1  ==> True
False==0  ==> True
True+False+4   ==> 5

bool(‘ABC’) ==> True
bool(”) ==> False
bool(3) ==> True
bool(0) ==> False

if expression:
    block1
    
if expression:
    block1
else:
    block2
    
if expression1:
    block1
elif expression2:
    block2
else:
    block3

比较运算符
x==y 相等运算符
x!=y 不相等运算符
x is y 同一性运算符
x is not y
x in y 成员资格运算符
x not in y

0<age<100 # python可以这样用

‘ab’==’ab’ ==> True
‘ab’==’cd’ ==> False
12==34 ==> False

x=y=[1,2]; x==y ==> True
x=y=[1,2]; x is y ==> True
x=y=[1,2]; z=[1,2]; x is z ==> False
x=y=[1,2]; z=[1,2]; x==z ==> True

‘ABC'<‘ABC’ ==> False
[1,2]<[2,1] ==> True

布尔运算符:
and, or, not

断言
age=-1; assert 0<age<100, ‘NOTE’
Traceback (most recent call last):
  File “<pyshell#21>”, line 1, in <module>
    age=-1; assert 0<age<100, ‘NOTE’
AssertionError: NOTE

while expression:
  block

for item in itemset:
  block

range函数包含下限,不包含上限;
range(0,3) ==> [0, 1, 2]
range(3) ==> [0, 1, 2]

遍历字典
d={‘key1′:’value1′,’key2′:’value2’};
for key in d:
    print key,d[key]

for key,value in d.items():
    print key,value

并行迭代
a=[1,2,3]; b=[‘a’,’b’,’c’]; zip(a,b) ==> [(1, ‘a’), (2, ‘b’), (3, ‘c’)]

编号迭代
for index, string in enumerate(strings):
    if ‘xxx’ in string:
       strings[index]=’NewValue’

反转和排序迭代
sorted([3,1,2]) ==> [1, 2, 3]
x=[1,2,3]; list(reversed(x)) ==> [3, 2, 1] # reversed返回可迭代对象

break结束循环
continue进行下一次循环

while True:
   block1
   if expression:
      break
   block2

for item in items:
   block1
   if expression:
      break
   block2
else:
   block3  # 当for执行完毕,并且没有执行过其中的break,此时执行block3

列表推导式
[x*x for x in range(3)] ==> [0, 1, 4]
[x*x for x in range(3) if x%2==0] ==> [0, 4]
[(x,y) for x in range(3) for y in range(3)] ==> [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

pass # 占位符,类似于空语句
del # 删除对象
exec # 执行一个字符串
exec “print ‘ABC'” ==> ABC
from math import sqrt; scope={}; exec ‘sqrt=1’ in scope; sqrt(4); scope[‘sqrt’];

eval 求值,类似于 exec
eval(raw_input(“”))
1+1
2

scope={}; scope[‘x’]=2; scope[‘y’]=3; eval(‘x*y’,scope) ==> 6

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,495
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,741
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298