一、内置函数#绝对值abs()
#所有值都为真才为真all()
#只要有一个值为真就为真any()
#10进制转成二进制bin()
#10进制转成八进制oct()
#10进制转成十六进制hex()
#布尔值bool()
#字符转换成字节bytes()
#字节转换成字符str()#对文件的操作open()
#基本流程
#打开文件
op_file=open("file name","rwb+")
#操作文件op_file.operation()
#关闭文件op_file.close()#检查函数是否可以被调用callable()
#通过字节数查找Ascii中的字符chr()...
在python的官方文档中:getattr()的解释如下:
getattr(object, name[, default])Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object‘s attributes, the result is the value of that attribute. For example, getattr(x, ‘foobar‘) is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise ...
python--------------内置函数内置函数操作 1#!usr/bin/env python 2# -*- coding:utf-8 -*- 3# 1.locals()和globals() 4# def func(): 5# x=1 6# y=2 7# print(locals()) 8# print(globals()) 9# func() 10 11# 2.eval,exec,和compile 12# print(123) 13# "print(456)"#字符串 14# eval("print(456)")#吧字符串转换成python代码去执行(有返回值) 15# exec("print(7889)")#吧字符串转换成python代码去执...
主要内容: python中68个内置函数的功能及使用方法 原文:https://www.cnblogs.com/fengchong/p/9477902.html
本函数是把一个字符串表示的字符转换为字符相对应的整数,适用于UNICODE字符。比如ord(‘a’)则返回整数97,ord(‘\u2020’)则返回整数8224。与函数chr()的功能刚好相反。例子:#ord()
print(‘ord(a)‘, ord(‘a‘))
print(‘ord(u2020)‘, ord(‘\u2020‘))
print(‘ord(1)‘, ord(‘1‘))结果输出如下:ord(a) 97ord(u2020) 8224ord(1) 49 蔡军生 QQ:9073204 深圳原文:http://blog.csdn.net/caimouse/article/details/436018...
.把任意数值转化为字符串有两种方法。(1)str()用于将数值转化为易于人读的形式。print(str("我是中国人"))>>>我是中国人(2)repr()用于将数值转化为易于解释器读的形式输出的是字符串的官方标准。str出来的值是给人看的字符串,repr出来的值是给机器看的,括号中的任何内容出来后都是在它之上再加上一层引号。print(repr("我是中国人"))>>>"我是中国人" 。Python中出现的任何中文,虽然我们在编辑器里看到的是中文,但是背地里...
"""本文件:研究内置函数:globals locals"""a = 1b = 2def func(): name = ‘alex‘age = 73print(globals()) # 返回的是字典:字典里面的键值对:全局作用域的所有内容。print(locals()) # 返回的是字典:字典里面的键值对:当前作用域的所有的内容。# print(globals()) # 返回的是字典:字典里面的键值对:全局作用域的所有内容。# print(locals()) # # 返回的是字典:字典里面的键值对:当前作用域的所有的内容。func()...
1. isinstance(变量名,类型) #判断什么类型 ps:只支持输入两个参数,输入3个参数会报错>>> isinstance (a,int,float)Traceack (most recent call last): File "<stdin>", line 1, in <module>TypeError: isinstance expected 2 arguments, got 3 >>> isinstance (a,int)True >>> b=1.1234>>> isinstance(b,float)True >>> c=1+1j>>> isinstance(c,complex)True >>> d=[1,2,3,4]>>> isinstance(d,li...
__init____new____repr____str__这几个函数的优先级分别是new,str,repr,init看下面的例子:class A:def __str__(self):return "this is str"def __init__(self,value="hello world !"):self.value = valuedef __new__(self):return "this is new"def __repr__(self):return "this is repr"a=A()print(a)由上面的例子可以看出他们的出场顺序原文:http://blog.51cto.com/853056088/2089084
返回一个參数i表示的字符串。比方,chr(97)返回字符”a”。參数i的有效范围为0到1。114,111(0x10FFFF),其他范围的值会抛出异常ValueError。与之相反转换的函数是ord(),它是把一个字符串变成数值。样例:#chr()函数print(‘0x5a:‘, chr(0x5a), ‘ 97:‘, chr(97), ‘ 60:‘, chr(60))输出结果例如以下:0x5a: Z 97: a 60: <蔡军生 QQ:9073204 深圳原文:http://www.cnblogs.com/llguanli/p/6795016.html
本函数是用来判断对象object的属性(name表示)是否存在。如果属性(name表示)存在,则返回True,否则返回False。参数object是一个对象,参数name是一个属性的字符串表示。例子:#hasattr()class Foo:def __init__(self):self.x = 123def test(x):self.x = xfoo = Foo()
print(hasattr(foo, ‘x‘))
print(hasattr(foo, ‘y‘))
print(hasattr(foo, ‘test‘))输出结果如下:TrueFalseTrue蔡军生 QQ:9073204 深圳原文:http://bl...
1. 切片切片是只取列表或字符串中一部分或全部。定义一个list:l=[‘Monica‘,‘Josie‘,‘Shirley‘,‘Leo‘,‘Tina‘]s=[:] 取的是整个列表元素s=[0:2] 取得是下标为零和1的元素(不管是从前数还是从后数,切片的特点包头不包尾)s=[:2] 取得是小标为0和1的元素,当冒号前面不写下标时,默认从第一个元素开始s=[2:] 取得是下标为2一直到这个列表的最后一个s=[-3:-1] 写负数是从列表的最后开始数,要把小的写在前面s=[::2] ...
对于简单的函数来说,可以使用类似于三元运算来表示,即: lambda表达式格式: lambda [arg1[, arg2, ... argN]]: expression 先来看看三元表达式#普通的条件语句if 1 == 1:name = "budongshu"
else:name = "yangchi"
#三元表达式
name = "budongshu" if 1 == 1 else "yangchi" 正式登场lambda表达式#普通的函数
def func(arg):return arg + 1
result = func(250)
print "result = ", result #lambda表达式
my_lambda...
1.命令介绍最近学习并使用了一个python的内置函数dir,首先help一下:复制代码 代码如下:
>>> help(dir)
Help on built-in function dir in module __builtin__:dir()
dir([object]) -> list of strings
Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:
No argument: the names in the current scope.
Module object:...
如果可迭代的对象的所有元素中只要有一个元素为True就返回True,否则返回False。或者可迭代对象为空,也返回False。这个函数主要用来判断列表、元组、字典等对象是否有元素为True,提高计算速度,与之等效的代码如下:def any(iterable): for element in iterable: if element: return True return False 例子:#any()函数
a = []
b = {}
c = (1, 3, 4)
d = (None, 1, 3)print(‘a:‘, any(a), ‘b:‘, an...