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

yipeiwu_com6年前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 tensorflow基于cnn实现手写数字识别

一份基于cnn的手写数字自识别的代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- import tensorflow as tf from tenso...

Python3中urlencode和urldecode的用法详解

在Python3中,将中文进行urlencode编码使用函数 urllib.parse.quote(string, safe='/', encoding=None, errors=N...

在Python中Dataframe通过print输出多行时显示省略号的实例

在Python中Dataframe通过print输出多行时显示省略号的实例

笔者使用Python进行数据分析时,通过print输出Dataframe中的数据,当Dataframe行数很多时,中间部分显示省略号,如下图所示: 0 项华祥 1...

使用Python中的线程进行网络编程的入门教程

引言 对于 Python 来说,并不缺少并发选项,其标准库中包括了对线程、进程和异步 I/O 的支持。在许多情况下,通过创建诸如异步、线程和子进程之类的高层模块,Python 简化了各种...

python判断文件夹内是否存在指定后缀文件的实例

该代码主要是基于python实现判断指定文件夹下是否存在指定后缀的文件。代码如下: import os Your_Dir='你的文件夹/' Files=os.listdir(Yo...