python学习-----函数

直接贴代码了,以后复习可以翻看。

 1#!/usr/bin/python
  2 #-*-coding: UTF-8 -*-
  3 
  4 #无参数
  5 def print_hello():
  6     print "hello"
  7 
  8 print_hello()
  9 
 10 #带参数
 11 def print_str(s):
 12     print s
 13     return s * 2
 14 print_str("loce")
 15 
 16 #带默认参数
 17 def print_default(s = "hello"):
 18     print s
 19 print_default()
 20 print_default("default")
 21 
 22 #不定长参数
 23 def print_args(s, *arg):
 24     print s
 25     for a in arg:
 26         print a
 27     return
 28 
 29 print_args("hello")
 30 print_args("hello", "world", "1")
 31 
 32 #参数次序可变
 33 def print_two(a, b):
 34     print a, b
 35 
 36 print_two(a = "a", b = "b")
 37 print_two(b = "b", a = "a")



运行:

hello
loce
hello
default
hello
hello
world
1
a b
a b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值