Python中的函数与变量有什么用

这篇文章主要介绍Python中的函数与变量有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

专注于为中小企业提供网站建设、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业南沙免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

 

首先大家先来看一看这个代码演示,理解下Python中的函数与变量的关系:

 

def cheese_and_crackers(cheese_count, boxes_of_crackers):

print "You have %d cheeses!" % cheese_count

print "You have %d boxes of crackers!" % boxes_of_crackers

print "Man that's enough for a party!"

print "Get a blanket.\n"

 

print "We can just give the function numbers directly:"

cheese_and_crackers(20, 30)

 

print "OR, we can use variables from our script:"

amount_of_cheese = 10

amount_of_crackers = 50

 

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

 

print "We can even do math inside too:"

cheese_and_crackers(10 + 20, 5 + 6)

 

print "And we can combine the two, variables and math:"

cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

 

通过这个案例,我们可以发现函数 cheese_and_crackers传递很多的参数,然后在函数里把它们打印出来。我们可以在函数里用变量名,可以在函数里做运算,甚至可以将变量和运算结合起来。从一方面来说,函数的参数和我们的生成变量时用的 =赋值符类似。事实上,如果你可以用 =给一个东西命名,你也就可以将其作为参数传递给一个函数。

 

下面我们再来看看刚刚编写的代码的终端输出结果:

 

$ python ex19.py

We can just give the function numbers directly:

You have 20 cheeses!

You have 30 boxes of crackers!

Man that's enough for a party!

Get a blanket.

 

OR, we can use variables from our script:

You have 10 cheeses!

You have 50 boxes of crackers!

Man that's enough for a party!

Get a blanket.

 

We can even do math inside too:

You have 30 cheeses!

You have 11 boxes of crackers!

Man that's enough for a party!

Get a blanket.

 

And we can combine the two, variables and math:

You have 110 cheeses!

You have 1050 boxes of crackers!

Man that's enough for a party!

Get a blanket.

以上是“Python中的函数与变量有什么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网页名称:Python中的函数与变量有什么用
转载来于:http://csdahua.cn/article/gcpgdg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流