python的常用内置函数
①abs() 函数返回数字的绝对值
dict()
{} ? ? ?#创建一个空字典类似于u={},字典的存取方式一般为key-value
help('math')查看math模块的用处
help(a)查看列表list帮助信息
dir(help)
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
a
①.0
next(it)
id(a)
a=["tom","marry","leblan"]
list(enumerate(a))
oct(10)
①.0. bin() 返回一个整数 int 或者长整数 long int 的二进制表示
bin(10)
'0b1010'
'0b1111'
①.1.eval() 函数用来执行一个字符串表达式,并返回表达式的值
f=open('test.txt')
bool()
False
bool(1)
True
bool(10)
bool(10.0)
isinstance(a,int)
isinstance(a,str)
class ? User(object):
? ? def__init__(self):
class Persons(User):
? ? ? ? ? super(Persons,self).__init__()
float(1)
①0
float(10)
①.0.0
iter(a)
for i in iter(a):
... ? ? ? ? print(i)
...
tuple(a)
s = "playbasketball"
len(s)
len(a)
class User(object):
? ?def __init__(self,name):
? ? ? ? ? ? self.name = name
? def get_name(self):
? ? ? ? ? ? return self.get_name
? @property
? ?def name(self):
? ? ? ? ? ?return self_name
list(b)
range(10)
range(0, 10)
class w(object):
a = w()
getattr(a,'s')
complex(1)
(1+0j)
complex("1")
max(b)
class Num(object):
...? ? a=1
.. print1 = Num()
print('a=',print1.a)
a= 1
print('b=',print1.b)
print('c=',print1.c)
delattr(Num,'b')
Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'
hash("tom")
a= set("tom")
b = set("marrt")
a,b
({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})
ab#交集
{'t', 'm'}
a|b#并集
{'t', 'm', 'r', 'o', 'a'}
a-b#差集
{'o'}
函数可以说是一个黑箱,输入一些值,然后输出一些值,所以呢return就是让函数输出值的操作.
然而,类,简单来说就是一系列函数的集合,它最主要的用途是设定对象和方法.
在Python中,我简单举个例子,我要算a+b=c,我输入a和b,输出c.
那么,函数就是这样的:
def plus(a, b):
c = a + b
return c
但是类是不同的,同样是计算a+b=c,我要先设定一种方法,比如叫做Plus,如下:
Class Plus:
def __init__(self, a, b):
self.a = a
self.b = b
def return_result(self):
self.c = self.a + self.b
return self.c
那么在主程序中你就要调用这个类,如下:
result = equation.return_result()
print result
希望可以帮到你,或者你把你的程序发过来,我看看~
下面是 Python 中一个函数的示例,该函数计算给定数字的阶乘并返回结果:n
要使用此函数,您可以从 main 函数调用它,并将 的值作为参数传入.例如:n
python编带返回值的exe程序方法.
使用ossystem函数运行其他程序os模块中的system()函数可以方便地运行其他程序或者脚本.其函数原型如下所示.ossystem(command)其参数含义如下所示.command要执行的命令,相当于在Windows的cmd窗口中输入的命令.如果要向程序或者脚本传递参数,可以使用空格分隔程序及多个参数.以下实例实现通过ossystem()函数打开系统的记事本程序.importos#使用ossystem()函数打开记事本程序ossystem('notepad')0#关闭记事本后的返回值#向记事本传递参数,打开pythontxt文件ossystem('notepadpythontxt')
以上就是土嘎嘎小编为大家整理的python返回函数教程相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!