#-*- coding:utf-8 -*-
'''
Created on 2016年12月16日
@author: Administrator
'''
'''定义函数 '''
def sey_hi():
print("hi~");
sey_hi()#调用函数
"""传参数函数"""
def sey_a_b(a,b):
return a+b
print(sey_a_b(2, 4))
def say_hell_hi(str):
print("hello "+str+"!")
say_hell_hi("jack")
##实现对hello 打印四次
def repest_str(str,tiems):
repeated_str=str * tiems
return repeated_str
repest_strings=repest_str("hello ", 4)
print(repest_strings)
print("hello "*4)
##全局变量
def foo():
global x
print(str(x))
print(x)
##默认参数的函数
def fooo(s,t=1):
repeated=s*t;
return repeated
print(fooo("jack",))
##关键字参数 选择传入
def func(a,b=10,c=8):
print("a is ",a ,"b is ",b,"c is ",c)
func(123,c=23) ##传入相应的关键字 如果指明关键字 那么顺序不太要
#varargs 参数
def print_paras(fpara,*nums,**words): ##nums 后台当做元组来对待
print("fpara "+str(fpara))
print("nums"+str(nums))# 后台当做字典来存储
print("words"+str(words))# 后台当做字典来存储
print_paras("hello",1,3,5,7,words="python",anohter_words="java")#
python 函数基础
最新推荐文章于 2025-04-16 16:55:42 发布