python基础之函数

       函数犹如小型程序,可用来执行特定的操作。Python提供了很多函数,可用来完成很多神奇的任务。实际上,也可以自己编写函数,因此我们通常将pow 等标准函数称为内置函数 。

>>> pow(2,3)          ---冥运算
8

>>> len("python")    ---统计字符串长度
6

>>> abs(-10)     ---计算绝对值
10

>>> round(10 / 3)   ---圆整为最相近的整数
3

>>> round(1.234, 2)     ---四舍五入
1.23

>>> float(8)     ---将数字转换为浮点数
8.0

>>> help(pow)     ---查看函数的用法

>>> int(12.3)      ---转换为整数
12

>>> id(2)      ---查看对象的内存地址
31023904

>>> type(2)    ---查看对象类型
<type 'int'>

>>> divmod(6, 4)    ---返回了商和余数
(1, 2)

>>> dir(math)           ---查看模块中包含的工具
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

>>> ord('a')      ---能够返回某个字符(注意,是一个字符,而不是多个字符组成的串)所对应的ASCII值(是十进制的)
97

>>> chr(97)     ---根据整数值返回字符
'a'

>>> alst = [1,2,3,4,5,6]
>>> alst[::-1]
[6, 5, 4, 3, 2, 1]
>>> list(reversed(alst))    #反转      
[6, 5, 4, 3, 2, 1]

>>> all_users.insert(0,"python")    # list.insert(i,x)就是向列表插入元素的函数
>>> all_users
['python', 'qiwsir', 'github', 'io']

>>> t = 123, "qwe", [456], "python"
>>> t
(123, 'qwe', [456], 'python')
>>> tls = list(t)   #元组转换为列表
>>> tls
[123, 'qwe', [456], 'python']
>>> t_tuple = tuple(tls)    #列表转换为元组
>>> t_tuple
(123, 'qwe', [456], 'python')

>>> ad = {"name":"qiwsir", "lang":"python"}
>>> bd = ad
>>> bd
{'name': 'qiwsir', 'lang': 'python'}
>>> id(ad)
65062640
>>> id(bd)
65062640
>>> cd = ad.copy()     #字典拷贝
>>> cd
{'name': 'qiwsir', 'lang': 'python'}
>>> id(cd)
65039888

>>> a = {"name":"qiwsir"}
>>> a.clear()    #清空字典中所有元素
>>> a
{}

 


 


 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值