python简单的函数定义和用法实例

yipeiwu_com5年前Python基础

本文实例讲述了python简单的函数定义和用法。分享给大家供大家参考。具体分析如下:

这里定义了一个温度转换的函数及其用法。

def convertTemp(temp, scale):
  if scale == "c":
   return (temp - 32.0) * (5.0/9.0)
  elif scale == "f":
   return temp * 9.0/5.0 + 32
temp = int(input("Enter a temperature: "))
scale = input("Enter the scale to convert to: ")
converted = convertTemp(temp, scale)
print("The converted temp is: " + str(converted))

希望本文所述对大家的Python程序设计有所帮助。

相关文章

进一步探究Python的装饰器的运用

装饰器在 python 中用的相当广泛,如果你用过 python 的一些 web 框架,那么一定对其中的 “ route() 装饰器” 不陌生,今天咱们再看一个具体的案例。 咱们来模拟一...

Python面向对象class类属性及子类用法分析

本文实例讲述了Python面向对象class类属性及子类用法。分享给大家供大家参考,具体如下: class类属性 class Foo(object): x=1.5 foo=Foo...

python快排算法详解

python快排算法详解

快排是python经典算法之一。 1、下面讲解的是什么是快排和快排的图示。 2、快排是一种解决排序问题的运算方法。 3、快排的原理:在数组中任意选择一个数字作为基准,用数组的数据和基...

python base64库给用户名或密码加密的流程

给明文密码加密的流程: import base64 pwd_after_encrypt = base64.b64encode(b'this is a scret!') pwd_bef...

python使用matplotlib绘图时图例显示问题的解决

python使用matplotlib绘图时图例显示问题的解决

前言 matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包。在使用Python matplotlib库绘制数据图时,需要使用图例标注数据类别,但是传...