Python学习——Functions
来源:互联网 发布:河北邢台招聘程序员 编辑:程序博客网 时间:2024/05/23 02:01
前言:
本节将介绍如何自定义函数、调用函数、使用函数库中的函数
1、自定义函数
a、格式:def + 函数名(参数):
def hello_world(word):
注意,有colon冒号:
b、可选的函数注释"""Prints 'Hello World!' to the console."""
c、函数体
word = "Hello World!"print(word)整理上述代码如下:
def hello_world(word):"""Prints 'Hello World!' to the console."""word = "Hello World!"print(word)
2、调用函数
函数名(参数)helloworld("I love Python!")
3、函数调用函数
与C/C++一致,直接在函数内部调用另一个函数
def cube(number): return number*number*numberdef by_three(number): if (number % 3) == 0: return cube(number) else: return False
4、module 函数库
import module
比如想使用math函数库,需要在程序前面添加import math
比如想使用math函数库,需要在程序前面添加import math
import mathprint math.sqrt(25)#输入结果为5
上述只为使用均方函数,但使用math.sqrt()较长的表达式,显得冗长。
可以使用from module import function,即from math import sqrt,简化调用表达式,即sqrt()即可。
from math import sqrtprint sqrt(25)
5、Universal Imports
若想普通方便调用函数库中的函数,可以添加from module import *表达式
如想调用math函数库中的所有函数,可利用下述程序代码
如想调用math函数库中的所有函数,可利用下述程序代码
from math import *
6、Built-In Functions
def biggest_number(*args): print max(args) return max(args) def smallest_number(*args): print min(args) return min(args)def distance_from_zero(arg): print abs(arg) return abs(arg)biggest_number(-10, -5, 5, 10)smallest_number(-10, -5, 5, 10)distance_from_zero(-10)
函数分析:
max()——返回整型或浮点型数值集合中的最大值
maximum = max(1,2,3,8.8,9.9,4,7,6,9)print maximummin()——返回整型或浮点型数值集合中的最小值
minimum = min(1,2,3,0.5)print minimumabs()——返回输入数值的绝对值
absolute = abs(-99)print absolutetype()——返回数据的类型
print type(99) #intprint type(99.9) #floatprint type('Python') #str
上述函数都是内置函数,即不需要添加函数库
7、Review
if/elif/else
def shut_down(s): if s == "yes": return "Shutting down" elif s == "no": return "Shutdown aborted" else: return "Sorry"
Modules
import mathprint math.sqrt(13689) #结果是117.0
Built-In Functions
def distance_from_zero(number): if type(number) == int or type(number) == float: return abs(number) else: return "Nope"
8、实战Program——Taking a Vacation
8.1 Planning Your Trip
def hotel_cost(nights): return 140*nights
def plane_ride_cost(city): if city == "Charlotte": return 183 elif city == "Tampa": return 220 elif city == "Pittsburgh": return 222 elif city == "Los Angeles": return 475
def rental_car_cost(days): if days >= 7: return days*40 - 50 elif days >= 3: return days*40 - 20 return days*40
def hotel_cost(nights): return 140*nights def plane_ride_cost(city): if city == "Charlotte": return 183 elif city == "Tampa": return 220 elif city == "Pittsburgh": return 222 elif city == "Los Angeles": return 475 def rental_car_cost(days): if days >= 7: return days*40 - 50 elif days >= 3: return days*40 - 20 return days*40 def trip_cost(city,days): return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)
8.5 Plan Your Trip
What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money?def hotel_cost(nights): return 140*nights def plane_ride_cost(city): if city == "Charlotte": return 183 elif city == "Tampa": return 220 elif city == "Pittsburgh": return 222 elif city == "Los Angeles": return 475 def rental_car_cost(days): if days >= 7: return days*40 - 50 elif days >= 3: return days*40 - 20 return days*40 def trip_cost(city,days,spending_money): return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money print("Above costs %d dollars!" % trip_cost("Los Angeles", 5, 600))
0 0
- Python学习——Functions
- Python学习笔记(2)——Functions
- Python学习笔记2——Python built-in functions学习
- Python第四节FUNCTIONS学习汇总。
- Python学习教程-3_Use functions
- Python学习练习4----Functions(函数)
- 闭包—Functions that return functions
- ES6学习——新的语法:Arrow Functions
- Kotlin官方文档翻译—《Functions and Lambdas-Functions》
- Javascript—Higher Order Functions
- JavaScript Functions — In-Depth
- Arrow Functions—箭头函数
- Python 3000——配接(Adaptation) 还是泛型函数(Generic Functions)?
- python build-in functions
- Python main() functions
- Python en:Functions
- Functions Basics------Learning Python
- Python Built-in Functions
- 关于C语言中内存的3个问题
- 控制公司
- 数组从小到大排列
- Spring中的Bean配置
- java接口与抽象类
- Python学习——Functions
- Android定时器Alarm
- RMQ算法详解(区间最值查询)
- git log
- HDU 4539 排兵布阵 【状态压缩dp】
- 浅谈web网站架构演变过程
- javascript事件轮询(event loop)详解
- Android进阶之路 - 四大存储之SharedPreferences存储
- Puzzle UVA - 227