程序设计除了要有很强的逻辑和编码能力外,我觉得更重要的是要讲究方法,这样在开发测试过程中才能做到事半功倍,按一定方法进行的程序设计,可以清晰的分析问题,处理问题,解决问题。
一、 IPO模式
所谓IPO模式,即Input Process Output,
1、用户输入I:input()获得输入,程序的输入包括:文件输入、网络输入、用户手工输入、随机数据输入、程序内部参数输入等。输入是一个程序的开始
2、运算部分P:程序对输入进行处理,输出产生结果。处理的方法也叫算法,是程序最重要的部分。可以说,算法是一个程序的主要灵魂。算法是一个程序的灵魂
3、结果输出O:print()输出结果,程序的输出包括:屏幕显示输出、文件输出、网络输出、操作系统内部变量输出等。
输出是一个程序展示运算成果的方式。
二、程序编写的步骤
分析问题:分析问题的计算部分;
确定问题:将计算部分划分为确定的IPO三部分;
设计算法:完成计算部分的核心方法;
编写程序:实现整个程序;
调试测试:使程序在各种情况下都能正确运行;
升级维护:使程序长期正确运行,适应需求的微小变化
三、inital-print模版
初始变量:运算需要的初始值
运算部分:根据算法实现
结果输出: print()输出结果
四、语法
1、注释:单行注释# 多行注释···
2、1个缩进=4个空格 。 表明代码直接的程序关系
3、命名规则:使用大小写字母、数字和下划线的组合,但首字母只能是大小写字母或下划线,不能使用空格;中文等非字母符号也可以作为名字
4、空格的使用:
表示缩进关系的空格不能改变
空格不能讲一个命名分割
程序可以使用空格增加程序的可读性
五、输入函数
Input()函数从控制台获得用户输入。
使用方法如下:
<变量>=input(<提示性文字>)
获得的用户输入以字符串的形式保存在<变量>中
六、计数循环基本过程
for i in range(<计数值>):
<表达式组>
例子:使某一段程序连续运行10次
for i in range(10)
<表达式组>
七、python对库函数引用的方式
1、import <库名>
2、from <库名> import<函数名>
from <库名> import *
八、数字类型转换
函数:整数int()、浮点数float()、复数complex()
九、字符串处理方法
十、列表的操作
十一、元组
元组有三个特点:
1、元组中元素可以是不同类型,例如:t3=123,333,("hello","中国")
2、元组中元素存在先后关系,可以通过索引访问元组中元素。例如:t3[0]
3、元组定义后不能更改,也不能删除。例如:t3[0]=456
十二、随机数
十三、简单的条件构造
1、简单条件基本模式<expr><relop><expr>
2、<relop>是关系操作符,使用“==”表示等于
3、除数字外,字符或字符串也可以按照字典顺序用于条件比较
4、<condition>是布尔表达式,为bool类型,布尔值的真和假以字符True和False表示
十四、异常处理语句
1、python使用try...except...,可使程序不因运行错误而奔溃
try:
<body>
except <ErroryType1>
<handler1>
except <ErroryType2>
<handler2>
except:
<handler3>
2、python异常处理语句还可以使用else和finally关机字
try:
<body>
except <ErroryType1>
<handler1>
except <ErroryType2>
<handler2>
except:
<handler3>
else:
<process_else>
finally:
<process_finally>
实例:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import math
def main():
print("this program finds the real solutions to a quadratic.\n")
try:
a, b, c = eval(input("Please enter the coefficents (a,b,c) :"))
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print("\nThe solution are:", root1, root2)
except ValueError as excObj:
if str(excObj) == "math domain error":
print("No Real Roots.")
else:
print(("You didn't give me the right number of coefficients."))
except NameError:
print("\nYou didn't enter three number.")
except TypeError:
print("\nYour inputs were not all numbers.")
except SyntaxError:
print("\nYour input was not the correct form, Missing comma?")
except:
print("\nSomething went wrong,sorry!")
main()
其中一个运行结果:
$ pytest -s tests/quadratic.py
============================= test session starts ==============================
platform darwin -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /Users/leiyuxing/pytest, configfile: pytest.ini
plugins: xdist-2.0.0, allure-pytest-2.8.18, forked-1.3.0
collecting ... this program finds the real solutions to a quadratic.
Please enter the coefficents (a,b,c) :.>>
Your input was not the correct form, Missing comma?
collected 0 items
============================ no tests ran in 6.56s =============================
十五、循环
1、无限循环
1.1、语法:while语句
while <condition>:
<body>
1.2、while语句中<condition>是布尔表达式
1.3、<body>是一条或多条语句
a、当条件<conditon>为真时,循环体重复执行
b、当条件<condition>为假时,循环终止
1.4、条件总是在循环顶部被判断,即在循环执行之前,这种结构又被称为前测循环
实例:
i = 0
while i <= 10:
print(i)
i = i +1
如果循环体忘记累加i,条件判断一直为真循环体将一直执行,这就是所谓的死循环程序。
这时通常使用<Ctrl>-c来终止一个程序。
2、for/while 中的continue用法
continue语句,其作用为结束本次循环。即跳出循环体中下面尚未执行的语句,对于while循环,继续求解循环条件。而对于for循环程序流程接着遍历循环列表。
3、continue语句和break语句的区别
3.1 continue语句只结束本次循环,而不终止整个循环的执行
3.2 break语句则是结束整个循环过程,不再判断执行循环的条件是否成立。
4、哨兵循环,伪代码如下:
初始化sum为0
初始化count为0
接受输入字符串数据,xStr
while xStr非空
将xStr转换为数字x
将x加入sum
count增加1
接受下个字符串数据,xStr
输出sum/count
5、文件循环代码:
def main()
fileName = input("What file are the numbers in? ")
infile = open(fileName, 'r')
sum = 0
count = 0
for line in infile:
sum = sum + eval(line)
count = count +1
print(“\nThe average of the numbers is”,sum / count)
main()
6、循环嵌套代码:
def main()
fileName = input("What file are the numbers in? ")
infile = open(fileName, 'r')
sum = 0.0
count = 0
line = infile.readline()
while line != ""
# 为line中的值更新其count和sum
for xStr in line.split(","):
sum = sum + eval(xStr)
count = count +1
line = infile.readline()
print(“\nThe average of the numbers is”,sum / count)
main()
十六、布尔操作符
1、布尔操作符:and,or, not
2、布尔运算符and和or用于组合两个布尔表达式,并产生一个布尔结果
a、<expr> and <expr>
b、<expr> or <expr>
3、not运算符计算一个布尔表达式的反not <expr>
4、布尔操作符:
a or not b and c == (a or ((not b) and c))