Python abs, bool, dir, eval, exec

本文介绍了Python中的基本概念,包括绝对值计算、布尔值判断、函数调用列表、类定义及使用帮助文档查看函数说明等。此外还讲解了求和、表达式求值、执行Python代码片段等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

abs

print(abs(10))
print(abs(-10))
print(abs(-1.1))
10
10
1.1
abs(0.00000001 - 0) < 1e-6
True

bool

print(bool(0))
print(bool(1))
False
True
print(bool(None))
print(bool(''))
print(bool([]))
print(bool({}))
False
False
False
False
print(bool(' '))
True

dir

显示可调用的函数

dir([])
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

用help显示某个函数的简短描述

help([].append)
Help on built-in function append:

append(...) method of builtins.list instance
    L.append(object) -> None -- append object to end
class A():
    """
    A is just a test class
    """
    def __init__(self):
        pass

    def printA(self):
        pass

    def Hello(self):
        pass
a = A()
dir(A)
['Hello',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'printA']
help(A)
Help on class A in module __main__:

class A(builtins.object)
 |  A is just a test class
 |  
 |  Methods defined here:
 |  
 |  Hello(self)
 |  
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  printA(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
A.__doc__
'\n    A is just a test class\n    '

sum

sum([1,2,3,4,5])
15

eval exec

eval('10**2')
100
my_small_program = '''
for i in range(10):
    print(i, end = '')
'''
exec(my_small_program)
0123456789
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值