python帮助和变量强制转换

Python两种帮助方式

1.help

   help函数是Python的一个内置函数。  函数原型:help([object])。

实例:

>>> help()
Welcome to Python 2.7!  This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
help>

help> int

Help on class int in module __builtin__:

class int(object)

 |  int(x=0) -> int or long

 |  int(x, base=10) -> int or long

 |

 |  Convert a number or string to an integer, or return 0 if no arguments

 |  are given.  If x is floating point, the conversion truncates towards zero.

 |  If x is outside the integer range, the function returns a long instead.

 |

 |  If x is not a number or if base is given, then x must be a string or

 |  Unicode object representing an integer literal in the given base.  The

 |  literal can be preceded by '+' or '-' and be surrounded by whitespace.

 |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to

 |  interpret the base from the string as an integer literal.

 |  >>> int('0b100', base=0)

 |  4

 |

 |  Methods defined here:

 |

 |  __abs__(...)

 |      x.__abs__() <==> abs(x)

 |

 |  __add__(...)

help>

如果要查看某个模块或者某个函数只需要输入对应名称即可,上例中我输入的是int函数
2.dir()
   dir函数是Python的一个内置函数。  函数原型:dir([object])
实例如下:
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time     : 2017/10/24 9:08
# @Author   : hantong
# @File     : help.py
# dir()
# help()
a = 2
print(a)  
print(type(a))  #查看a的类型
print(dir(a))    #查看a(整型)的模块帮助信息
b = 'dkgkkhkj'
print(type(b))    #查看b的类型
print(dir(b))      #查看b(字符串类型)模块的帮助信息


执行结果如下:
C:\Python27\python.exe E:/迁移资料/python/pycharm2017pjb/PycharmProjects/help.py
2
<type 'int'>
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
<type 'str'>
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

进程已结束,退出代码0

3.强制数据类型转换
   int,float和str类型相互转换
实例:
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time     : 2017/10/24 14:01
# @Author   : hantong
# @File     : convert.py
a = int(123)
print(a)
print(type(a))
#打印a的数据类型:int类型
b = float(a)
print(b)
print(type(b))
#打印b的数据类型:float类型,成功把int转换成float类型
c = str(a)
print(c)
print(type(c))
#打印c的数据类型:str类型,成功把int转换成str类型

aa = float(123.90)
print(aa)
print(type(aa))
#打印查看aa的类型:
bb = int(aa)
print(bb)
print(type(bb))
#float类型转换为int类型
cc = str(aa)
print(cc)
print(type(cc))
#float类型转换为str类型
执行结果如下:
C:\Python27\python.exe E:/迁移资料/python/pycharm2017pjb/PycharmProjects/convert.py
123
<type 'int'>
123.0
<type 'float'>
123
<type 'str'>
123.9
<type 'float'>
123
<type 'int'>
123.9
<type 'str'>
 注意:str转换为int时,只能符合数学格式的数字型字符串转换成整数, 浮点数转换成整数,也只是简单的取整,而非四舍五入


   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值