什么是return语句的简单基本解释,如何在Python中使用它?
它和print声明有什么区别?
我刚刚学习(正在学习)函数参数如何在Python中工作,我开始尝试使用它没有明显的原因,当这个:
def jiskya(x, y):
if x > y:
print y
else:
print x
print(jiskya(2, 3))
Run Code Online (Sandbox Code Playgroud)
给出了输出:
>>>
2
None
Run Code Online (Sandbox Code Playgroud)
它None来自哪里?还有,这是什么?
让我们说我定义一个简单的函数,它将显示传递给它的整数:
def funct1(param1):
print(param1)
return(param1)
Run Code Online (Sandbox Code Playgroud)
输出将是相同的,但我知道当return在函数中使用语句时,可以再次使用输出.否则,print不能使用语句的值.但我知道这不是正式的定义,任何人都能为我提供一个好的定义吗?
在python中我似乎没有理解返回函数.为什么在我可以打印时使用它?
def maximum(x, y):
if x > y:
print(x)
elif x == y:
print('The numbers are equal')
else:
print(y)
maximum(2, 3)
Run Code Online (Sandbox Code Playgroud)
这段代码给了我3.但是使用return它会做同样的事情.
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
Run Code Online (Sandbox Code Playgroud)
那两者之间的区别是什么?对不起,这个巨大的菜鸟问题!
我有一个创建 DataFrame 的函数。在该功能中我可以将其打印出来。但我在返回过程中做错了,因为运行函数后我似乎无法调用 DataFrame。下面是我的虚拟代码和附加的错误。
import pandas as pd
def testfunction(new_df_to_output):
new_df_to_output = pd.DataFrame()
S1 = pd.Series([33,66], index=['a', 'b'])
S2 = pd.Series([22,44], index=['a', 'b'])
S3 = pd.Series([11,55], index=['a', 'b'])
new_df_to_output = new_df_to_output.append([S1, S2, S3], ignore_index=True)
print new_df_to_output
print type(new_df_to_output)
print dir()
return new_df_to_output
testfunction('Desired_DF_name')
print dir()
print Desired_DF_name
Run Code Online (Sandbox Code Playgroud)
DataFrame 在函数内正确打印。目录显示该函数之后没有返回DataFrame。尝试打印该数据帧返回返回以下错误
回溯(最近一次调用最后一次):文件“functiontest.py”,第 21 行,打印 Desired_DF_name NameError:名称“Desired_DF_name”未定义
我确信这是一个简单的错误,但在搜索 Stackoverflow 和 python 教程后我找不到解决方案。非常感谢任何指导。
我正在参加算法和数据结构(Python)课程.在书中有一个"堆栈"的例子,其中的方法返回某些值.在本书中,调用方法时会打印这些值.但是,当我运行程序时,没有打印任何内容.我必须自己打印返回值.这是Python 2和3之间的区别,还是我做错了什么?这是代码.
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
s=Stack(); s.push(5)
s.size()
s.isEmpty()
s.peek()
Run Code Online (Sandbox Code Playgroud)
所以,这应该打印"1","假"和"5".但事实并非如此.
我正在为变量分配一个函数,如下所示:
def hello(name):
print "Hello %r \n" % name
king = hello
print "%r, King of Geeks" % king("Arthur")
Run Code Online (Sandbox Code Playgroud)
它在终端返回:
你好'
Arthur'None,极客之王
是什么赋予了?
因此,我正在通过Wentworth等人如何像计算机科学家一样思考 Python 3指南,尝试更多地学习编程.虽然它是一个很棒的资源,但它对于在Python 3中编写的样式和"最佳实践"几乎没有什么可说的.
我正在编写有关条件的章节中的一个练习题,它要求我编写一个函数,当输入int或float'mark'时返回字符串'grade'.
我这里的直接问题是关于函数中条件的重复和函数返回的值.是否有可能以某种方式使用循环使其更简洁,而不是elif一遍又一遍地编写语句?此外,main grade函数返回null None值; 我怎样才能使这个功能"富有成效"而不是None在它被称为时打印?
这是我写的:
def grade(mark):
grds = ['First','Upper Second','Second','Third','F1 Supp.','F2','F3']
if mark >= 75.0:
print("Your grade is",grds[0])
elif mark < 75.0 and mark >= 70.0:
print("Your grade is",grds[1])
elif mark < 70.0 and mark >= 60.0:
print("Your grade is",grds[2])
elif mark < 60.0 and mark >= 50.0:
print("Your grade is",grds[3])
elif mark < 50.0 and mark >= 45.0:
print("Your grade is",grds[4])
elif mark < 45.0 and …Run Code Online (Sandbox Code Playgroud) 在python中,return()和print()会对以下代码产生不同的影响.有什么不同?为什么?
def count_wins(teamname):
wins = 0
for team in nfl:
if team[2] == teamname:
wins +=1
return wins
def count_wins(teamname):
wins = 0
for team in nfl:
if team[2] == teamname:
wins +=1
print wins
Run Code Online (Sandbox Code Playgroud)
nfl = [[''2009','1','Pittsburgh Steelers','Tennessee Titans'],['2009','1','Minnesota Vikings','Cleveland Browns']]