【跟着MIT学Python】Unit 2.4 Functions

本文探讨了算法设计中的关键概念,如问题分解技巧和抽象思维在编程中的应用,通过实例解析函数定义、返回与打印、作用域、递归与交互的区别,以及模块导入和文件操作。深入理解分解问题成小块和使用黑箱抽象是提升编程效率的基础。
摘要由CSDN通过智能技术生成

Decomposition & Abstraction

{ D e c o m p o s i t i o n : break   problems   into   small   pieces A b s t r a c t i o n : Black   box \begin{cases}Decomposition: \textit{break problems into small pieces}\\Abstraction: \textit{Black box}\end{cases} {Decomposition:break problems into small piecesAbstraction:Black box

Function

  • Characteristic:

    name

    parameter

    docstring

    body

def is_even(i): # name and the parameter
	'''
	here is the docstring
	'''
	<body>
is_even(n) #call the function

The indentation indicated the body of the function.

Calling function and Scope

Return & Print

ReturnPrint
Only has meaning inside the functionCan be used outside the function
Only one return executed inside a functionCan execute many print statement inside a function
  • Python returns the value None, if nothing return given.

Scope

  • inside a function, can access a variable outside
  • inside ac function, cannot modify a variable outside

Keyword Arguments

EXAMPLE

	def printName(firstname, lastname, reverse):
		if reverse:
			print()
		else:
			print()
  • Reverse the input order

printName(lastname = 'balaba', firstname='balaba',reverse = False)
  • Given default value
def printName(firstname, lastname, reverse = False)
	<scope>

printName(balaba,balaba) #THE REVERSE WILL BE DEFAULT VALUE

Specification

'''
input: i , a positive int
Returns True if i is even, False if i is odd
'''

Objects

Built-in Types - Python 3.10.2 documentation

capitalizeupperisupperswapcaseindexfindcountreplace

indexraises ValueError when x is not found in s

find Return -1 if sub is not found.

Interaction vs. Recursion

RECURSION

Breaking down question into a smaller version of the question itself.

a × b = a + a + . . . + a ⏟ b = a + a + a + . . . + a ⏟ b − 1 a\times b = \underbrace{a+a+...+a}_{b}\\= a+\underbrace{a+a+...+a}_{b-1} a×b=b a+a+...+a=a+b1 a+a+...+a

# Recursion
def mult(a,b)
	if b == 1:
		return a
	else:
		return a + mult(a, b-1)

INTERACTION

Looping constructs lead to interactive algorithms

# Interaction
def mult_inter(a,b)
	result = 0
	while b > 0:
		result += a
		b -= 1
	return result

Files

module and files

a module is a .py doc

# the file circle.py contains:
pi = 3.14159
def area(radius):
	return pi*(radius**2)
def circumference(radius):
	return 2*pi*radius
  • import and use the module
import circle
pi = 3
print(pi) # print out 3
print(circle.pi) # print out 3.14159
print(circle.area(3))
  • Dont want to refer to functions and variable by module,
from cicle import *

Files

  • save work for latter use

OPEN and CLOSE

#open a file called kids for writing use
nameHandle = open('kids','w')
name = input('type something:')
nameHandle.write(name+'\n')
nameHandle.close()

#open a file called kids for reading use
nameHandle = open('kids','r')
print(line)
nameHandle.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值