根据您的个性需求进行定制 先人一步 抢占小程序红利时代
语句 | 角色 | 例子 |
---|---|---|
赋值 | 创建引用值 | a='Apple',b='bike' |
调用 | 执行函数 | log.write('mylog') |
打印调用 | 打印对象 | print(1,'hello') |
if/elif/else | 选择动作 | if a in b: print(a) |
for/else | 序列迭代 | for i in list: print(i) |
while/else | 一般循环 | while True: print('True') |
pass | 空占位符 | for i in list: pass |
break | 循环/迭代退出 | while True:if a==b: break |
continue | 循环继续 | for i in list: if i<5: continue |
def | 函数定义 | def add(a,b): print(a+b) |
return | 函数返回 | def add(a,b): return a+b |
import | 模块访问 | import os |
from | 属性访问 | from sys import stdin |
class | 创建类 | class myclass(): def myprint(): print('myprint') |
try/except/finally | 捕获异常 | try: open('/tmp/file') except: print('no file') |
raise | 触发异常 | raise <'error type'> |
assert | 调试检查 | assert a<0,'a is too large' |
with/as | 环境管理器 | with open(file) as f: f.read() |
del | 删除引用 | del_list[i] del_list[i:j] del obj.attr |
在python里是不使用{}或者别的符号来限制语句的开始和结尾的,一个语句的开始(除了复合语句),就是开头,换行就是结束。在开头,不能随意增加空格:
创新互联从2013年开始,是专业互联网技术服务公司,拥有项目成都做网站、成都网站设计、成都外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元贵州做网站,已为上家服务,为贵州各地企业和个人服务,联系电话:18980820575
>>> print (1)
1
>>> print (1)
File "", line 1
print (1)
^
IndentationError: unexpected indent
在复合语句里也是同样的,当你使用缩进时,必须一致:
>>> def add(str):
... str=str
... print(str)
File "", line 3
print(str)
^
IndentationError: unindent does not match any outer indentation
复合语句有单行写法和多行写法。从冒号后面就是复合语句的开始。
单行:复合语句只有一行时,可使用单行写法,但是复合语句所包含的语句不是单行时,需要使用对齐的缩进来表示复合语句
#单行语句
>>> if 1>0: print(1)
...
1
#多行语句
>>> if 1>0:
... int=1
... print(int)
...
1
一般语句里,一行的结束就是此语句的结束。
在简单语句可以使用分号来隔开多个语句。
>>> a=3;b=3;print(a+b)
6
使用列表,元组,字典的时候按照一定的方式可以把一个语句分成多行:
>>> dict={1:'first',
... 2:'second'}
当我们所写的语句有bug,会出现一些错误,程序会中断运行。
但我们在这个时候,不想让程序中断但还是需要提示报错的时候可以使用try:
>>> while True:
_input=input("please input digit:")
try:
print("{:d} *10 is {:d}".format(int(_input),int(_input)*10))
break
except:
print("{} is not a number".format(_input))
please input digit:a
a is not a number
please input digit:1
1 *10 is 10
变量名可以使大小写字母,数字和下划线,但只能以大小写字母和下划线开头,不能以数字开头。变量名是区分大小写的,保留字符是不能使用的。
python3.0里的保留字符:
false class finally is
None continue for lambda
True def from nonlocal
and del dlobal not
as elif if or
assert else import pass
break except in raise
特殊变量名:
main等,前后都有两个下划线的变量名,有很多是有特殊意义的
从python3.0开始print变成了函数,但返回值为None,print函数的格式如下:
print([object,...][,sep=''][,end='\'][file=sys.stdout])
在这里,object是要打印的内容。object可以是任意对象。默认是没有。
sep是两个object之间隔开的字符。默认是一个空格。
end是结尾,默认为换行。
file为输出目标,默认为标准输出流。
>> print(1,2,3,sep=';')
1;2;3
>>> print(1,2,3,sep=':')
1:2:3
>>> print(1,2,3,end='')
1 2 3
>>> print(1,2,3,end='');print (4,5,6,end='')
1 2 34 5 6
>>> print(1,2,3,end='\n');print (4,5,6,end='')
1 2 3
4 5 6
>>> print(1,2,3,file=open(r'D:\ruanjian\1.txt','wt'))
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'1 2 3\n'
在if语句里的<'test'>位置里的就是判断语句,结果为True,就能进入子语句,判断语句包含:
>>> 1<2
True
>>> True and True
True
>>> 1 and True
True
>>> True or False
True
>>> not True
False
>>> not False
True
>>> 1 in [1,2]
True
>>> 1 in (1,2)
True
>>> 1 in {'1':1}
False
当我们使用and和or的时候,返回结果不一定是True或False:
and:当其中一个或多个测试值为false的时候,取第一个false的值
False
>>> 1 and [] and {}
[]
and:当全部值得测试值为True的时候,取最后一个值
>>> 1 and 2 and True
True
>>> 1 and 2 and True
True
>>> 1 and 2 and True and 3
3
or:当其中一个或多个值为True的时候,取第一个True的值
>>> 0 or [1] or {1:'1'}
[1]
>>> 1 or 0 or 2
1
or:当全部值为false的时候,去左后一个false值
>>> 0 or [] or {}
{}
>>> False or 0 or {}
{}
三元表达式的格式如下:
<'value1'>if <''test'> else <'value2'>
当测试值为真的时候取<'value1'>,假的时候取<'value2'>
>>> 1 if True else 2
1
>>> 1 if False else 2
2
>>> 'True' if 1>2 else 'False'
'False'
>>> 'True' if 1<2 else 'False'
'True'
这个还可以如下运用:
[<'value2'>,<'value1'>][<'test'>]
>>> [2,1][True]
1
>>> [2,1][False]
2
>>> ['False','True'][1>2]
'False'
>>> ['False','True'][1<2]
'True'
whil语句一般格式:
while <'test1'>:
<'statement1'>
else:
<'statement2'>
只要测试语句为真,会一直循环<'statement1'>。当test1为假的时候会运行else语句里的内容。从这里,退出循环的方法有:
1.在<'statement1'>里的语句更改<'test1'>的结果为False
2.在<'statement1'>里的语句里增加break语句来跳出循环
3.在<'statement1'>里的语句里增加exit()来退出python循环,不过这里会退出整个的python程序
例子
>>> a=0;b=10
>>> while a
break,continue语句
break语句用来退出最近所在的for语句或while语句。
continue语句是用来跳到最近所在的for语句或者while语句的结尾。
>>> a=0;b=10
>>> while a
pass语句
pass语句是占位的空语句,在有些复合语句里,可能没有具体的语句,但需要正常运行,这就需要设置空语句(pass)来代替
例子
>>> if True:
print('true')
else: pass
true
else语句
else语句,只有在for语句和while语句正常结束后,会运行:
>>> a=0;b=10
>>> while a
for语句
for语句在python里是一个通用的序列迭代器:可以遍历任何有序的序列对象内的元素。可用于字符串、列表、元组、其他内置可迭代对象以及之后我们能通过类所创建的新对象。
一般格式:
for in