友情提示:380元/半年,儿童学编程,就上码丁实验室。
接上篇
4.if-else 陈述句
if 陈述句可以用来实现所谓的判断流程,让某些程序只有在某个条件成立时才会执行
if 语法需要注意的细节:
1.if 陈述句之后,必须加一个冒号
2.只有在该判断式成立时才会执行的程序代码,称为 if 区块(if block),if block 里面的每一行程序代码,前面都必须缩排
3.else 后面也必须增加冒号,下面的内容叫 elseblock,else block 里面的内容也必须缩排
EX:
判断奇偶数,no % 2 为 0
num = input(‘please input a n’)
if num%2 == 0:
print(num, ‘even’)
else:
print(num, ‘odd’)
5.循环
循环 (loop) 就是在特定程序区块 (block) 中,重复执行相同的工作,分为 for 循环及 while 两种,下列再详细描述
❖ for
for 循环拥有一个计数器,称为循环变量。这使得 for 循环能够知道过程中的执行顺序
EX:
no = [1,2,3,4,5,6,7,8,9]
for i in no:
print i
Output:
1
2
3
4
5
6
7
8
9
❖ while
利用一个返回结果为布尔值的表达式作为循环条件,当这个表达式的返回值为「true」时,则反复执行循环内的程序代码;若表达式的返回值为「false」,则不再执行循环体内的代码,继续执行循环体下面的代码
EX:
# While 循环
x=0
while x < 10:
print ‘x is currently: ‘,x
print ‘x is still less than 10, adding 1 to x’
x+=1
else:
print ‘All Done!’
Output:
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
All Done!
6.函式(function)
将重复使用的程序代码转换为一个函式,有效简化程序代码,并可重复利用,也可将函式当成一个函式的输入(lambda)
先前章节我们有使用过的函式:
•print: 印出某个值
•len: 取得一个串行的长度
•type: 取得一个值的型别
EX:
len(list)
5
语法剖析:
1.len 是函式名称
2.函式名称之后须加一对括号,括号间的是函式的输入
3.在这里只有一个输入,即 list
4.若有多个输入值,则用逗号分隔( , )
5.(非语法) 5 是这个函数呼叫的输出
函式撰写方式
EX:
# 函式
def addNum(a, b): # 函式命名标准会采取驼峰式的样式
return a+b #Return
print addNum(1,4)
Output:
5
如果要在函式的输入值不预先定义有几个,可以透过下列语法:
# 接受多个 Key/Value 参数
def make_two_lists(**kwargs):
keys,values = [],[]
for k,v in kwargs.items():
keys.append(k)
values.append(v)
return [keys,values]
make_two_lists(david=’M’, Mary = ‘F’, John=’M’)
Output:
[['John', 'Mary', 'david'], ['M', 'F','M']]
7.档案(file)
本章节主要描述该如何读取档案内容以及对档案的操作
❖写档
EX:
#将Hello World 写入档案中
fid = open(‘test.txt’, ‘w’)
fid.write(‘HellonWorld’)
fid.close()
❖读档
EX:
# 一行一行读取档案
with open(‘test.txt’, ‘r’) as fid:
for line in fid:
print(“Line: ” + line.strip())
# 读取档案中所有内容
with open(‘test.txt’, ‘r’) as fid:
s= fid.read()
print “读取档案中所有内容line =>”,s
Output:
Line: Hello
Line: World
读取档案中所有内容line => Hello World
8.函式库(libraries)
函式库是使用 Python 不可或缺的一环,没有函式库的 Python 简直没有用处!
所谓的函式库:
1.前人或神人写好的一堆函式
2.使用者可以「引用」(import) 函式库来使用这些函式
3.让程序代码更简洁且开发更快速
EX:
# 引用函式库,并显示目前的时间
import time
# 我们可以使用这个函式库里面的函式strftime
proc_time = time.strftime(“%Y-%m-%d%H:%M:%S”)
print(proc_time)
Output:
2017-09-21 06:51:11
也可以只引用函式库中的其中某几个函式
EX:
# from 后面接的是函式库名称,import 后接的是函式名称
from random import choice
choice([1, 5, 7, 3, 9, 0])
Output:
5
9.错误侦测(try catch)
程序在执行的过程中,往往会遇到错误状况,例如:型别错误,数据读取错误等,为了避免一发生错误程序就停止且能够实时纪录或显示错误信息,就必须做好错误判断的处理
EX:
# 处理 IO Exception
try:
f= open(‘testfile’,’w’)
f.write(‘Test write this’)
except IOError: #处理IOError
print(“Error: Could not find file or read data”)
else:
print(“Content written succesfully”)
f.close()