Python 获取最大值函数、Python3 os.write() 方法
Python 获取最大值函数
以下实例中我们使用max()方法求最大值:
# -*- coding: UTF-8 -*-# Filename : test.py # author by : www.w3cschool.cn# 最简单的 print(max(1, 2)) print(max('a', 'b'))# 也可以对列表和元组使用 print(max([1,2])) print(max((1,2)))# 更多实例 print("80, 100, 1000 最大值为: ", max(80, 100, 1000)) print("-20, 100, 400最大值为: ", max(-20, 100, 400)) print("-80, -20, -10最大值为: ", max(-80, -20, -10)) print("0, 100, -400最大值为:", max(0, 100, -400))
执行以上代码输出结果为:
2 b 2 2 80, 100, 1000 最大值为: 1000 -20, 100, 400最大值为: 400 -80, -20, -10最大值为: -10 0, 100, -400最大值为: 100
Python3 os.write() 方法
概述
os.write() 方法用于写入字符串到文件描述符 fd 中. 返回实际写入的字符串长度。
在Unix中有效。
语法
write()方法语法格式如下:
os.write(fd, str)
参数
-
fd -- 文件描述符。
-
str -- 写入的字符串。
返回值
该方法返回写入的实际位数。
实例
以下实例演示了 write() 方法的使用:
#!/usr/bin/python3import os, sys# 打开文件 fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)# 写入字符串 str = "This is w3cschool.cn site" ret = os.write(fd,bytes(str, 'UTF-8'))# 输入返回值 print ("写入的位数为: ") print (ret)print ("写入成功")# 关闭文件 os.close(fd) print ("关闭文件成功!!")
执行以上程序输出结果为:
写入的位数为: 23 写入成功 关闭文件成功!!