* 作者:煮酒品茶 tea
* 博客:http://www.zwhset.com http://cwtea.blog.51cto.com
* 目前在京峰教育担任python讲师
#0、整数转换类型int
> 很多时候我们都需要把字符串类型转换成整数类型,或者浮点数以及其它数转换成整数类型,这个时候我们就可以用到int,如果判断字符串是否全数字并且让int函数不给异常,我们可以用到字符串的isdigit()方法。
#1、函数原型
~~~
class int(object)
| int(x=0) -> int or long # 返回int或long
| int(x, base=10) -> int or long #给定base
| 将数字或字符串转换为整数,如果没给参数返回0,字符串必须是unicode的数字类型,否则返回异常
~~~
#3、样例
~~~
In [38]: int('123')
Out[38]: 123
In [39]: int('123a')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-d0a7b61fa361> in <module>()
----> 1 int('123a')
ValueError: invalid literal for int() with base 10: '123a'
#做字符串检查
In [41]: a = '123a'
In [42]: if a.isdigit():
....: print int(a)
....: else:
....: print 0
....:
0
~~~
- python核心
- python27编译安装
- 原处修改以及拷贝
- loop
- 内置的变量
- 内置类型
- python内置方法
- 内置方法列表
- 求绝对值-abs
- 求商取余-divmod
- 交换输入-input
- [未完]文件打开函数-open
- staticmethod
- 判断迭代对象真假all
- 判断迭代对象真假any
- 枚举对象enumerate
- 整数转换类型int
- ascii码转换ord-chr-unichr
- 类型转换str
- python执行字符串表达式eval
- 类型判断isinstance
- 数学计算pow
- 数学计算和sum
- 不可变集合frozenset
- zip
- 比较cmp
- basestring类型判断
- 反转序列迭代器resversed
- python模块
- glob
- sys.argvgetopt-argparse
- os
- sys
- requests
- random
- time
- date
- zlib-gzip-bz2-zipfile-tarfile
- ftp
- paramiko
- pexpect
- 数据库模块
- redis
- mysql
- mongodb
- __builtin__
- 学生PYTHON分享
- 学生运维分享