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程序设计有所帮助。

相关文章

pycham查看程序执行的时间方法

如下所示: import time 首先导入时间模块 在程序开始执行的地方写入: start=time.clock() 在程序末尾写入: end=time.clock()...

python3.7 openpyxl 删除指定一列或者一行的代码

python3.7 openpyxl 删除指定一列或者一行 # encoding:utf-8 import pandas as pd import openpyxl xl = pd....

全面了解Python环境配置及项目建立

全面了解Python环境配置及项目建立

一、安装Python Python比较稳定的两个版本是Python 3.5和Python 2.7,我用的是Python 2.7,下载地址是:https://www.python.org/...

跟老齐学Python之赋值,简单也不简单

变量命名 在《初识永远强大的函数》一文中,有一节专门讨论“取名字的学问”,就是有关变量名称的问题,本温故而知新的原则,这里要复习: 名称格式:(下划线或者字母)+(任意数目的字母,数字或...

对Xpath 获取子标签下所有文本的方法详解

对Xpath 获取子标签下所有文本的方法详解

在爬虫中遇见这种怎么办 想提取名称, 但是 名称不在一个标签里 使用xpath string()方法 例如 data.xpath("string(path)") path --...