python入门基础教程随笔4-python数据结构-函数

# encoding:utf-8

#自定义一个函数

def say_hi(): #()里面可以加参数,也可以没有参数
    print('hi')
say_hi()
say_hi()

 

 

1 def print_sum_two(a,b):
2       c = a + b
3 print(c)
4 print_sum_two(12, 120)

 

def print_some(str):
    print('我爱你' + str + "!")
print_some('中国')
print_some('小红')

def repeatd_str(str,times):
    repeatd_strs = str * times
    return repeatd_strs

repeatd_strs1 = repeatd_str('我爱你',3)
print(repeatd_strs1)

 


#全局变量与局部变量
x = 60
def fot(x):
  print('全局变量x等于' + str(x))
  x = 3
  print('局部变量x=' + str(x))
fot(x)
print(x)

#全局变量与局部变量 例子2
y = 60
print('第一次打印Y的值是:'+str(y))
def fod():
  global y
  y = 3000
  print ('y的值是:' + str(y))
fod()
print('第二次打印Y的值是:'+str(y)+',因为执行完fod函数后,y已经是一个全局变量,所以打印出来的y值是fod函数里面的y定义的值')

 

转载于:https://www.cnblogs.com/cs60/p/5598660.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值