大数据微专业_1.Python基础_1.5_函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了大数据微专业_1.Python基础_1.5_函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6749字,纯文字阅读大概需要10分钟。
内容图文
![大数据微专业_1.Python基础_1.5_函数](/upload/InfoBanner/zyjiaocheng/720/3eb8a5c6d9954afeadeba0ecf16b2760.jpg)
In [1]:
''' 1.5.1 函数的概念 ''' help(list())
Help on list object: class list(object) | list(iterable=(), /) | | Built-in mutable sequence. | | If no argument is given, the constructor creates a new empty list. | The argument must be an iterable if specified. | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __gt__(self, value, /) | Return self>value. | | __iadd__(self, value, /) | Implement self+=value. | | __imul__(self, value, /) | Implement self*=value. | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value. | | __ne__(self, value, /) | Return self!=value. | | __repr__(self, /) | Return repr(self). | | __reversed__(self, /) | Return a reverse iterator over the list. | | __rmul__(self, value, /) | Return value*self. | | __setitem__(self, key, value, /) | Set self[key] to value. | | __sizeof__(self, /) | Return the size of the list in memory, in bytes. | | append(self, object, /) | Append object to the end of the list. | | clear(self, /) | Remove all items from list. | | copy(self, /) | Return a shallow copy of the list. | | count(self, value, /) | Return number of occurrences of value. | | extend(self, iterable, /) | Extend list by appending elements from the iterable. | | index(self, value, start=0, stop=9223372036854775807, /) | Return first index of value. | | Raises ValueError if the value is not present. | | insert(self, index, object, /) | Insert object before index. | | pop(self, index=-1, /) | Remove and return item at index (default last). | | Raises IndexError if list is empty or index is out of range. | | remove(self, value, /) | Remove first occurrence of value. | | Raises ValueError if the value is not present. | | reverse(self, /) | Reverse *IN PLACE*. | | sort(self, /, *, key=None, reverse=False) | Stable sort *IN PLACE*. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None
In [7]:
round(3.1415926,2)
Out[7]:
3.14
''' 1.5.2 自定义函数
如何创建? def语句
'''
In [9]:
# 定义函数 var = float(input('请输入一个数字')) def f(x): if x > 0: print('正数') elif x == 0: print('为零') else: print('负数') f(var) var2 = float(input('请输入一个数字')) f(var2) def g(): print('hell world') g()
请输入一个数字21 正数 请输入一个数字0 为零 hell world
In [10]:
# 关于return def f1(x): y = 2**x #没有return def f2(x): y = 2**x #有return return y print(f1(2),f2(2)) #return语句退出函数,并返回一个表达式,不带参数值的return语句返回None
None 4
In [12]:
def f(lst1,lst2): m = [] for i in range(len(lst1)): m.append([lst1[i],lst2[i]]) return m a = [1,2,3] b = ['a','b','c'] f(b,a)
Out[12]:
[['a', 1], ['b', 2], ['c', 3]]
In [13]:
# 默认参数 def f(x,n = 2): return(x**n) print(f(10)) print(f(10,3)) # n = 2,这里n的默认值是2,如果不输入则以默认值为主
100 1000
In [17]:
# 可变参数 def f(*x): print(x) return(x) #print(f(1,2,3,4,5,6)) f() f(1) f('a','b') f(1,2,3,[4,5,6]) print(type(f('a','b'))) # 通过*来定义可变参数 # 默认会把可变参数传入一个元组
() (1,) ('a', 'b') (1, 2, 3, [4, 5, 6]) ('a', 'b') <class 'tuple'>
In [19]:
def f(st): for s in st: print(s) st_input = input("请输入一个字符串:") f(st_input)
请输入一个字符串:hello h e l l o
In [23]:
def f(x,y,z): a = (x+y)*(x-y)*z return(a) print(f(5,3,2))
32
def f(*m):?
s = 0
lst = []
for i in m:
while str(i).isnumeric():
s += i
lst.append(i)
break
else:
print("输入的内容包含非数字")
print(lst)
return(s/len(lst))
print(m)?
print(f(1,2,3,4,5)) print(f(1,2,3,'q',4,5))
In [33]:
def area_jx(a,b): return(a*b) def area_yx(r): return(2*r*3.14) print(area_jx(10,5)) print(area_yx(10))
50 62.800000000000004
In [1]:
lst = input("请输入一个列表") print(type(lst))
请输入一个列表[1,2,3,4] <class 'str'>
In [6]:
lst = eval(input("请输入一个以字符为元素的列表:")) def f(lt): return(dict.fromkeys(lt,0)) print(f(lst))
请输入一个以字符为元素的列表:['a','b','c'] {'a': 0, 'b': 0, 'c': 0}
In [8]:
''' 1.5.3 局部变量与全局变量 定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域 局部变量只能在其被声明的函数内部访问,全局变量可以在整个程序执行范围内访问,调用函数时,所有在函数内声明的变量名称都将被加入到作用域中。 '''
Out[8]:
'\n1.5.3 局部变量与全局变量\n\n\n'
In [9]:
# 演示案例 def f(m): m = 'hehe' #函数作用:将输入变量指向’hehe‘ print('函数内为局部变量:%s'%m) a = input("请输入一个数字:") f(a) print('函数外为全局变量:%s'%a) # f(m)中,m是函数的参数,f(x)是把x的值赋予了m,但x值本身不受影响,所以执行函数后,是在函数局部’变化‘了x # 什么是局部变量? 当函数定义内声明变量的时候,他们与函数外具有相同名称的其它变量没有任何关系 # 即变量名称对于函数来说都是’局部‘的,这称为变量的作用域,所有变量的作用域是它们被定义的块,从它们的名称被定义的那个时刻开始
请输入一个数字:1 函数内为局部变量:hehe 函数外为全局变量:1
In [15]:
# gloabl语句,把局部变量变为全局变量 def f(): global a a = 'hehe' print('函数内转为全局变量:%s'%a) a = input("请输入一个数字:") print('输入变量值:%s'%a) f() print(f())
请输入一个数字:ew 输入变量值:ew 函数内转为全局变量:hehe 函数内转为全局变量:hehe None
In [ ]:
''' 1.5.4 匿名函数lambda lambda 常见匿名函数 实际使用中,尽量用def函数,少用lambda '''
In [18]:
def f(a,b,c): return(a + b + c) print(f(1,2,3)) f = lambda a,b,c:a + b + c #lambda后面直接加参数,不用括号 print(f(1,2,3)) # lambda作为匿名函数,是不需要函数名的,需要一个函数,又不想动脑去想一个函数名,这就是匿名函数 # lambda的主体是一个表达式,而不是一个代码块 # lambda只有一行,仅仅能在lambda表达式中封装有限的逻辑进去
6 6
In [20]:
count = lambda lst:len(lst) print(count([1,2,3,4,5,6,7]))
7
In [21]:
def f(*var): lst = list(var) lst_re = sorted(lst,reverse = True) return(lst_re) print(f(2,3,4,7,5,4,3,1))
[7, 5, 4, 4, 3, 3, 2, 1]
In [22]:
def f(*score): s = 0 m = [] for i in score: if i >= 90: m.append('A') elif i > 60 and i < 90: m.append('B') else: m.append('C') s += i mean = s/len(score) return([m,mean]) result = f(89,92,56,87) print(result[0]) print(result[1])
['B', 'A', 'C', 'B'] 81.0
In [ ]:
st = 'abcdsa ed122' st.isalpha() st.isspace() st.isdigit()
内容总结
以上是互联网集市为您收集整理的大数据微专业_1.Python基础_1.5_函数全部内容,希望文章能够帮你解决大数据微专业_1.Python基础_1.5_函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。