Python中的一些常用方法

常用方法

1、input()用于获取用户的输入值,返回一个字符串类型例如:msg = input('请输入值')

#input函数
msg = input("请输入你的值")
print(type(msg))
print(msg)
请输入你的值2
<class 'str'>
2

2、type(a)用于查看a的数据类型。

a = 3.5
if str(type(a))!='int':
    print('asdad')
asdad

3、print()的标准形式为print(value,sep=' ',end='\n',file = sys.stdout,flash = False),其中sep代表参数间的分割符号,end表示结尾符号,file代表输出路径,默认为标准输出(屏幕)。

#print的sep和end和file可以改变来获得不同的格式
name = 'charli'
age = 8
phone_number = 13564
print(age,name,phone_number,sep = '|')
print(name,end = '')
print(age,end = '')
print(phone_number,end = '')
8|charli|13564
charli813564

4、dir()函数用来获取指定类或者模块包含的全部函数、方法、类、变量等属性。如:dir(str)

#列出指定类或模块包含的全部内容(函数、方法、类、变量)
from collections import deque
dir(deque)
['__add__',
 '__bool__',
 '__class__',
 '__contains__',
 '__copy__',
 '__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',
 'appendleft',
 'clear',
 'copy',
 'count',
 'extend',
 'extendleft',
 'index',
 'insert',
 'maxlen',
 'pop',
 'popleft',
 'remove',
 'reverse',
 'rotate']

5、help()函数用来查看某个函数或者方法的帮助文档。如help(str.title)

#帮助命令
help(str.title)
Help on method_descriptor:

title(self, /)
    Return a version of the string where each word is titlecased.
    
    More specifically, words start with uppercased characters and all remaining
    cased characters have lower case.

6、isinstance(a,int)判断a是否是int型

a = '123456'
print(isinstance(a,int))
False

7、id(a)获取a的地址

#id
a = 5
print(id(a))
140724614894528
发布了8 篇原创文章 · 获赞 0 · 访问量 8

猜你喜欢

转载自blog.csdn.net/weixin_42713642/article/details/105449189