码迷,mamicode.com
首页 > 编程语言 > 详细

深入解答11道Python基础面试题

时间:2017-09-05 19:43:01      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:面试题   pre   dict   int   位置   举例   nbsp   引用传递   函数传参   

1. Python的参数传递是值传递还是引

举例说明Python函数参数传递的几种形式,并说明函数传参是值传递还是引用传递

 

一、位置参数

调用函数时根据函数定义的参数位置来传递参数。例子:

def print_hello(name, sex):
sex_dict = {1: u‘先生‘, 2: u‘女士‘}
print (‘hello %s %s, welcome to python world!‘ %(name, sex_dict.get(sex, u‘先生‘)))


# 两个参数的顺序必须一一对应,且少一个参数都不可以
print_hello(‘tanggu‘, 2)#输出:hello tanggu 女士, welcome to python world!
print_hello(‘tanggu‘, 3)#输出:hello tanggu 先生, welcome to python world!(字典中没有3的值,所以返回“先生”)



# 字典中get()用法:dict.get(key, default=None)
#key -- 字典中要查找的键。default -- 如果指定键的值不存在时,返回该默认值值。

 

深入解答11道Python基础面试题

标签:面试题   pre   dict   int   位置   举例   nbsp   引用传递   函数传参   

原文地址:http://www.cnblogs.com/wangyanyan/p/7481508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!