python学习笔记

这篇博客介绍了Python中用于优化的`scipy.optimize.minimize`函数,用于找到函数的最小值。同时,讲解了如何进行文件的读取和写入,包括使用`f.open`和`with open`语句进行基本操作,以及按行读取文件内容。此外,还展示了处理输入异常的方法,确保用户输入的有效性。
摘要由CSDN通过智能技术生成

数据结构

函数

常用库

pandas

dataframe

scipy.optimize

minimize

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html

def minimize(fun, x0, args=(), method=None, jac=None, hess=None,
             hessp=None, bounds=None, constraints=(), tol=None,
             callback=None, options=None):
             
fun: 目标函数(最小化结果)    func(x, *args) -> float  
			x为一维的变量向量
x0: 初始猜测解;

错误和异常

异常处理

type/except

try:
  	x=int(input("input an num: "))
  	break
except ValueError:
  	print("is not num, please try again")

IO

文件读取

读文件

https://blog.csdn.net/xrinosvip/article/details/82019844

f = open('filename', 'r')
fileInfo=f.read()
f.close()

with open(desc_file) as content:
		dict_desc_info = yaml.load(content)
    
# 按行读取
for line in f.readlines():
  	print(line.strip())

写文件

f = open('filename', 'w')
f.write('hello, python!')
f.close()

with open('filename', 'w') as f:
  	f.write('hello, python!')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值