Python中的函数与变量讲解

264 篇文章 2 订阅
162 篇文章 3 订阅

不知道大家在学习Python的时候,有没有发现一个问题,函数里边的变量和脚本里边的变量好像是无关的。本文今天要讲的就是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经验分享

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

Python学习路线

这里把Python常用的技术点做了整理,有各个领域的知识点汇总,可以按照上面的知识点找对应的学习资源。
在这里插入图片描述

学习软件

Python常用的开发软件,会给大家节省很多时间。
在这里插入图片描述

学习视频

编程学习一定要多多看视频,书籍和视频结合起来学习才能事半功倍。
在这里插入图片描述

100道练习题

在这里插入图片描述

实战案例

光学理论是没用的,学习编程切忌纸上谈兵,一定要动手实操,将自己学到的知识运用到实际当中。
在这里插入图片描述
最后祝大家天天进步!!

上面这份完整版的Python全套学习资料已经上传至CSDN官方,朋友如果需要可以直接微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员二飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值