Python是一种简单而强大的编程语言,非常适合初学者入门。
作为Python编程的基础,掌握一些实用的内置函数对于初学者朋友们来说是至关重要的。
在这里,不念将介绍8个适合初学者的实用Python内置函数。
![Python编程-8个适合初学者的内置函数 图片[1]-Python编程-8个适合初学者的内置函数-不念博客](https://www.bunian.cn/wp-content/uploads/2023/12/640-2-3.jpg)
1. print()
print('apple')print('orange')print('pear')# apple# orange# pearprint('apple') print('orange') print('pear') # apple # orange # pearprint('apple') print('orange') print('pear') # apple # orange # pear
当我们将某些内容输入print()
中时,它会被打印到终端(也称为标准输出)。
2. abs()
print(abs(-5)) # 5print(abs(6)) # 6print(abs(-5)) # 5 print(abs(6)) # 6print(abs(-5)) # 5 print(abs(6)) # 6
abs()
给出了一个数的绝对值。换句话说,负数变成正数,而正数保持不变。
3. input()
通过input()
函数,我们可以要求用户在Python程序中输入某些内容。
name = input('what is your name? ')print('your name is ' + name)# what is your name? tom# your name is tomname = input('what is your name? ') print('your name is ' + name) # what is your name? tom # your name is tomname = input('what is your name? ') print('your name is ' + name) # what is your name? tom # your name is tom
注意,input()
始终返回一个字符串值。
因此,如果用户想要数字或其他任何值,需要自己进行必要的类型转换。
4. range()
range()
作为for
循环的一部分,允许我们多次重复代码操作,而不需要多次复制和粘贴代码。
for i in range(5):print('apple')# apple# apple# apple# apple# applefor i in range(5): print('apple') # apple # apple # apple # apple # applefor i in range(5): print('apple') # apple # apple # apple # apple # apple
5. dir()
x = 'apple'print(dir(x))# ['__add__', '__class__', '__contains__', '__delattr__',# '__dir__', '__doc__', '__eq__', '__format__', '__ge__',# '__getattribute__', '__getitem__', '__getnewargs__',# '__getstate__', '__gt__', '__hash__', '__init__',# '__init_subclass__', '__iter__', '__le__', '__len__',# '__lt__', '__mod__', '__mul__', '__ne__', '__new__',# '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',# '__rmul__', '__setattr__', '__sizeof__', '__str__',# '__subclasshook__', 'capitalize', 'casefold',# 'center', 'count', 'encode', 'endswith', 'expandtabs',# 'find', 'format', 'format_map', 'index', 'isalnum',# 'isalpha', 'isascii', 'isdecimal', 'isdigit',# 'isidentifier', 'islower', 'isnumeric', 'isprintable',# 'isspace', 'istitle', 'isupper', 'join', 'ljust',# 'lower', 'lstrip', 'maketrans', 'partition',# 'removeprefix', 'removesuffix', 'replace', 'rfind',# 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',# 'split', 'splitlines', 'startswith', 'strip',# 'swapcase', 'title', 'translate', 'upper', 'zfill']x = 'apple' print(dir(x)) # ['__add__', '__class__', '__contains__', '__delattr__', # '__dir__', '__doc__', '__eq__', '__format__', '__ge__', # '__getattribute__', '__getitem__', '__getnewargs__', # '__getstate__', '__gt__', '__hash__', '__init__', # '__init_subclass__', '__iter__', '__le__', '__len__', # '__lt__', '__mod__', '__mul__', '__ne__', '__new__', # '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', # '__rmul__', '__setattr__', '__sizeof__', '__str__', # '__subclasshook__', 'capitalize', 'casefold', # 'center', 'count', 'encode', 'endswith', 'expandtabs', # 'find', 'format', 'format_map', 'index', 'isalnum', # 'isalpha', 'isascii', 'isdecimal', 'isdigit', # 'isidentifier', 'islower', 'isnumeric', 'isprintable', # 'isspace', 'istitle', 'isupper', 'join', 'ljust', # 'lower', 'lstrip', 'maketrans', 'partition', # 'removeprefix', 'removesuffix', 'replace', 'rfind', # 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', # 'split', 'splitlines', 'startswith', 'strip', # 'swapcase', 'title', 'translate', 'upper', 'zfill']x = 'apple' print(dir(x)) # ['__add__', '__class__', '__contains__', '__delattr__', # '__dir__', '__doc__', '__eq__', '__format__', '__ge__', # '__getattribute__', '__getitem__', '__getnewargs__', # '__getstate__', '__gt__', '__hash__', '__init__', # '__init_subclass__', '__iter__', '__le__', '__len__', # '__lt__', '__mod__', '__mul__', '__ne__', '__new__', # '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', # '__rmul__', '__setattr__', '__sizeof__', '__str__', # '__subclasshook__', 'capitalize', 'casefold', # 'center', 'count', 'encode', 'endswith', 'expandtabs', # 'find', 'format', 'format_map', 'index', 'isalnum', # 'isalpha', 'isascii', 'isdecimal', 'isdigit', # 'isidentifier', 'islower', 'isnumeric', 'isprintable', # 'isspace', 'istitle', 'isupper', 'join', 'ljust', # 'lower', 'lstrip', 'maketrans', 'partition', # 'removeprefix', 'removesuffix', 'replace', 'rfind', # 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', # 'split', 'splitlines', 'startswith', 'strip', # 'swapcase', 'title', 'translate', 'upper', 'zfill']
当我们将某个对象传递给dir()
时,可以打印出该对象中存在的每个方法或属性。
这在我们想要快速检查某个对象可能具有的方法/属性时会很有用(而无需上网查找文档)。
6. help()
help(print())# Help on NoneType object:# class NoneType(object)# | Methods defined here:# |# | __bool__(self, /)# | True if self else False# |# | __hash__(self, /)# | Return hash(self).# ...help(print()) # Help on NoneType object: # class NoneType(object) # | Methods defined here: # | # | __bool__(self, /) # | True if self else False # | # | __hash__(self, /) # | Return hash(self). # ...help(print()) # Help on NoneType object: # class NoneType(object) # | Methods defined here: # | # | __bool__(self, /) # | True if self else False # | # | __hash__(self, /) # | Return hash(self). # ...
当我们将某个内容传递给help()
时,它会自动打印出其文档。
如果不想上网查找某些文档,这也是很有用的。
7. len()
print(len('apple')) # 5print(len('orange')) # 6print(len('pear')) # 4print(len('apple')) # 5 print(len('orange')) # 6 print(len('pear')) # 4print(len('apple')) # 5 print(len('orange')) # 6 print(len('pear')) # 4
当我们将某个内容传递给len()
时,我们可以得到该对象的长度(当然,如果对象没有长度,例如整数,我们将得到一个错误)。
8. max()
和min()
print(max(1,3,2)) # 3print(min(1,3,2)) # 1print(max(1,3,2)) # 3 print(min(1,3,2)) # 1print(max(1,3,2)) # 3 print(min(1,3,2)) # 1
max()
查找最大的对象,而min()
查找最小的对象。
结语
通过本文,我们学习了8个适合初学者的实用Python内置函数。
这些函数包括print()
、abs()
、input()
、range()
、dir()
、help()
、len()
、max()
和min()
。
这些函数是Python编程中不可或缺的工具,能够帮助初学者执行各种常见任务,熟练掌握这些函数可以帮助朋友们更好地理解和使用Python编程语言。
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END