python入门的基础练习(二)

Python入门的60个基础练习(二) 阅读详情

11-条件表达式、三元运算符

    a = 10
    b = 20

    if a < b:
        smaller = a
    else:
        smaller = b

    print(smaller)

    s = a if a < b else b  # 和上面的if-else语句等价

    print(s)

12-判断练习:用户名和密码是否正确

    import getpass  # 导入模块

    username = input('username: ')
    # getpass模块中,有一个方法也叫getpass
    password = getpass.getpass('password: ')

    if username == 'bob' and password == '123456':
        print('Login successful')
    else:
        print('Login incorrect')

13-猜数:基础实现

    import random

    num = random.randint(1, 10) # 随机生成1-10之间的数字
    answer = int(input('guess a number: '))  # 将用户输入的字符转成整数
    if answer > num:
        print('猜大了')
    elif answer < num:
        print('猜小了')
    else:
        print('猜对了')

    print('the number:', num)

14-成绩分类1

   score = int(input('分数: '))

    if score >= 90:
        print('优秀')
    elif score >=
2024年最全Python入门的60个基础练习(二)(1) data = f.read(4) # 读 4 字节f.readline() # 读到换行符、n 结束f.readlines() # 把每一行数据读出来放到列表中f.close()f.close()f = open(‘图片地址’, ‘rb’) # 打开非文本文件要加参数 bf.close()f = open(‘/tmp/myfile’, ‘w’) # 'w’打开文件,如果文件不存在则创建\n’)f.flush() # 立即将缓存中的数据同步到磁盘。 阅读详情

相关推荐

迭代牛顿-欧拉法——机器人动力学逆解

迭代牛顿-欧拉法牛顿欧拉法的步骤向外回归(outward recursion)step 1step 2向内回归(inward recursion)step 3step 4牛顿欧拉法的三种形式无坐标系NE算法(Coordinate-Free Algorithm)连杆坐标系NE算法(Link-Coordinates Algorithm)3-D向量NE算法 牛顿欧拉法的步骤 牛顿欧拉法(NE)是求解机器人动力学逆解的一种方法。动力学逆解定义为:已知机器人的构型qqq和运动的速度q˙\dot{q}q˙​和加速度q¨

qq_41845878的博客 4105

Orbbec-Gemini-335 深度相机 Python SDK 编译与配置全记录(Windows 环境)

本篇博客将围绕 Windows 系统下 Orbbec SDK 的 Python 配置与编译过程进行详细说明,主要使用的是官方提供的 pyorbbecsdk Python wrapper 项目以及 Orbbec SDK V2 Python Wrapper 配置指南,并记录了安装与配置过程中遇到的若干问题及解决办法。

Junseer的博客 1975

AI赋能科研绘图与数据可视化高级应用

面向需发表SCI/EI/中文核心论文的研究者,重点讲解基于AI大模型和R语言的全类型科研图形绘制,通过一个个高水平论文案例,从如何画图、如何标注、如何改图、如何美化、如何组合、如何排序来分解科研绘图每个步骤,并且从数据清洗、视觉表现、出版合规等维度构建核心竞争力,帮助学者将复杂科研成果转化为“审稿人友好型”学术图表,为高水平论文的发表奠定坚实的基础。的拒稿与图表质量直接相关。(1)Rbase基础绘图系统:基础图形函数(plot、lines、points等)(尺寸、格式、分辨率、字体、配色、高分论文案例)

aishangyanxiu的博客 2765

一个三极管和稳压管构成的简易稳压电源

一、对电压精度要求不高的场所可以使用本文中所述的简易稳压电源(电流变化引起 的基极-发射极的压降 变化 对输出电压来说可以容忍)二、根据负载实际需求选择三极管,不同的三极管集电极电流不同,电流放大倍数不同(1、尽量选择负载电流(iC 或 iE)上限时,hFE仍然比较理想的三极管,2、一般三极管对基极电流没有限制,可以适当调整基极电流以获得所需输出电流)

五花带点肉 1万+

水下机器人仿真平台选型指南:从AirSim到Unity3D的深度对比

本文深入对比了AirSim+UE和Unity3D两大水下机器人仿真平台的核心架构、传感器仿真能力和水下环境建模特点。AirSim+UE凭借高精度物理仿真和丰富传感器支持成为专业研发首选,而Unity3D则以快速开发和灵活扩展见长。文章为研发团队提供了全面的选型指南,帮助优化水下实验的仿真效率与成本。

weixin_29009669的博客 456

别再为PCB和散热器文件发愁了!手把手教你用ADS导出DWG文件给工厂(附单位转换避坑指南)

本文详细解析了从ADS设计到PCB/散热器加工的DWG文件导出全流程,重点介绍了单位转换、分层导出策略及散热器文件处理等关键步骤,帮助射频工程师避免加工误差和返工。特别提供了mil与mm单位转换的精确处理方法,确保设计文件与工厂加工需求无缝对接。

weixin_30814329的博客 617

WPS占用C盘空间解决方案与优化技巧

办公软件在长期使用过程中会产生大量缓存和历史文件,这些文件往往隐藏在系统目录中,导致磁盘空间被无声占用。以WPS为例,其版本历史备份、云同步缓存和模板库是主要空间占用源。通过分析文件存储机制,可以定位到具体的缓存路径,如AppData目录下的Kingsoft文件夹。针对这一问题,有效的解决方案包括手动清理、自动化脚本和注册表优化。对于需要持续监控的场景,可以结合PowerShell脚本和任务计划实现自动化管理。在实际应用中,这些技术不仅适用于WPS,也可迁移到其他办公软件的空间优化中,是每位IT运维人员和高

weixin_30788239的博客 466

保姆级教程:ABAP 通过 HTTP POST 调用 OData 服务创建业务数据的具体例子

笔者之前的文章发布之后,有朋友在评论区留言,询问 ABAP 除了 Web Service 之外,是否也支持 HTTP 呢?ABAP 这么强大的语言,支持 HTTP 当然不在话下。网上有很多在 ABAP 系统基于 CL_HTTP_CLIENT 工具类,通过 HTTP 协议和第三方系统交互的代码例子。本文介绍笔者项目中一个实际例子,实现的是在 ABAP 系统 A 通过 HTTP 调用 OData 服务,在 ABAP 系统 B 创建业务数据的需求。下面我们按照这个套路来一步步实现。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 715

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: python入门的基础练习(一)
下一篇: Python图像处理,把两张图合在一起吧
pythonputao
博客等级 码龄6年 60粉丝 111原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址