点击蓝字 关注我们
小伙伴们,大家好呀!~
今天我们学习关于Python的
二十个函数
print("hello,world!")
2. len() - 返回序列对象的长度
fruits = ["apple","banana","cherry"]print(len(fruits)) #输出:3
3. input() - 从用户获取输入
name = input("请输入您的名字:")print("您好," + name)
4. str() - 将对象转换为字符串
number = 10print("The number is:" + str(number))
5. int() - 将对象转换为整数
age = int(input("请输入您的年龄:"))print("您的年龄是:"+ str(age))
6. float() -将对象转换为浮点数
pi = float("3.14")print(pi)
7. range() - 返回指定范围的整数序列
numbers = list(range(1,6))print(numbers) # 输出:[1,2,3,4,5]
8. max() - 返回最大值
numbers = [5,10,3,8]print(max(numbers)) # 输出:10
9. min() - 返回最小值
numbers = [5,10,3,8]print(min(numbers)) # 输出:3
10. sum() - 返回序列对象中元素的总和
numbers = [1,2,3,4,5]print(sum(numbers)) #输出:15
11. abs() - 返回一个数的绝对值
print(abs(-10)) # 输出:10
12. round() - 四舍五入取整
print(round(3.1415,2))# 输出:3.14
13. sorted() - 对序列对象进行排序
numbers = [5,2,8,3,1]print(sorted(numbers)) # 输出:[1,2,3,5,8]
14. type() - 返回对象的类型
print(type("Hello")) # 输出:
15. str.upper() - 将字符串转换为大写
text = "hello"print(text.upper()) # 输出:HELLO
16. str.lower() - 将字符串转换为小写
text = "WORLD"print(text.lower()) # 输出:world
17. str.capitalize() -将字符串的首字母大写
text = "hello world"print(text.capitalize()) # 输出: Hello world
18. str.split() - 将字符串以空格分割为列表
text = "hello world"print(text.split()) # 输出:['hello', 'world' ]
19. str.replace() - 替换字符串中的指定字符
text = "hello world"print(text.replace("world","Python")) # 输出:hell
20. list.append() - 向列表末尾添加元素
fruits = ["apple","banana"]fruits.append("cherry")print(fruits) # 输出:['apple', 'banana', 'cherry' ]
END
本期的学习就到这里啦
关于Python的函数还有很多
感兴趣的同学可以自己在网上学习
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/pythonbc/43868.html