Python笔记(5)-操作符

Python-操作符总结(逻辑、位、算术、比较、赋值操作符及操作符优先级 部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**比较单例模式的对象,例如,None时,应使用is 或is not,参考。接下来,我会将操作符穿插在其他文章中进行展示。a,b为数字型,c为字符串。 阅读详情

算术运算符

+加, -减, *乘, /除,**求幂运算, %取余, //整除

>>> 3/2
1
>>> 3.0/2
1.5
>>> 3.0//2
1.0
>>> 2**3
8
>>> 5%2
1
>>> a=1111
>>> b=111111L
>>> a+b
112222L

注:在2.x版本和3.0版本python中“/”的结果是不同的:

In [25]: 5/2
Out[25]: 2
In [26]: 5//2      #两种方式的结果都是5除以2等于2
Out[26]: 2
In [27]: from __future__ import division  #使用这条指令将当前2.x环境转换为3.0版本python的精确计算方式
In [28]: 5/2
Out[28]: 2.5     #现在执行5除以2得到的是2.5而不是2
In [29]: 5//2      #5//2才会只取整数位
Out[29]: 2

赋值运算符

+=, -=, *=...... (注意: a+=2 实质上是a=a+2; python中不支持a++,但支持++a)

比较运算符

>, >=, <, <=, !=, ==(注意: 结果是bool类型,只能是True或者False)

>>> 1>2
False

注:==除了会判断是否相等,还会判断是否同一类型,如'1' == 1就会返回False错误(字符串和数值不相等)

逻辑运算符

逻辑与and, 逻辑或or, 逻辑非not

#如果为空,not+变量的返回值就为True

#如果返回值为False,加上not就会返回True

>>> warn = ""
>>> not warn
True>>> a="hello"
>>> not a
False>>> not ""
True
>>> 2>5
False>>> not 2>5
True
Python 操作符 Python提供了各种操作符用于执行不同类型的操作,例如算术操作、比较操作、逻辑操作等。print(name.upper()) # 输出:'JOHN',使用字符串的upper()方法将其转换为大写。print(letters[1:3]) # 输出:['b', 'c'],使用切片操作符获取列表的部分元素。print(name[0]) # 输出:'J',使用索引操作符获取字符串的第一个字符。print(len(name)) # 输出:4,使用len()函数获取字符串的长度。 阅读详情

相关推荐

【iceberg数据一致性】iceberg如何保证高并发数据一致性

那么在高并发的时候,同时操作某张表时是如何运作的呢?其实这里iceberg引用了乐观锁的方法。其实在写入数据的时候是有一个验证的操作的,即在准备写入时会检查原文件和带写入文件的差异,如果存在本次写入数据以外的数据,则代表有其他操作已经更新了原文件,那么本次写入会取消,并重试写入的步骤,知道验证完成,写入完成,如此就保证了数据的一致性,并且也解决了HMS锁的问题。在使用iceberg写数据时,一直弄不清楚为什么iceberg写入快,并且能够保证数据的一致性。今天决定搞清楚这个问题,经过查询和理解,写下来。

你的博客 1046

易学笔记-第5章:数字/5.4 python表达式操作符/5.4.1 操作符

python表达式操作符三元选择表达式:x if y else z逻辑或: x or y逻辑与: x and y逻辑非:not x成员关系x in yx not in y对象实体测试x is yx not is y大小比较,集合子集,超集相等性操作x < yx <= yx > yx >= y位或:x | y位与:x & y位异或:x ^ y左移...

weixin_33809981的博客 105

期末求生指南:手把手教你用F12开发者工具绕过百一测评的切屏检测(JavaScript/jQuery实战)

本文详细介绍了如何利用F12开发者工具绕过百一测评的切屏检测,通过分析JavaScript/jQuery中的blur()和focus()事件监听机制,提供实战技巧动态修改代码。文章涵盖事件监听定位、代码修改方法及应对高级检测机制的策略,适合需要临时解决线上考试限制的学生参考。

weixin_30731305的博客 449

python学习笔记5-数据类型+操作符

使用int () 将小数转换为整数,采用截断方式,即向下取整,直接砍掉小数点,不会四舍五入。(例如6.7,向上取整为7,向下取整为6) 获得关于类型的信息: (1)用type():例如 >>> a = '520' >>> type( a ) (2)用isinstance():指定一个参数类型,看是否匹配,返回一个bool值 >>> ...

sinat_36183354的博客 161

用Python实现四叉树(quad tree)

import numpy as np #正方形类 class square: #lx、ly为左上角坐标(x对应行号,y对应列号),rx、ry为右下角坐标 def __init__(self,lx,ly,rx,ry): self.lx=lx self.ly=ly self.rx=rx self.ry=ry #检查该矩阵中是否满足条件 #参数:原矩阵,阈值 #返回值:布尔类型 def check(s

weixin_44164489的博客 5868

银河麒麟V10离线环境下的Python项目迁移:从Windows依赖到ARM架构适配

本文详细介绍了在银河麒麟V10离线环境下将Python项目从Windows迁移到ARM架构的完整流程。重点讲解了离线安装pip、virtualenv等基础工具的方法,以及如何解决ARM架构兼容性问题,确保项目依赖正确迁移。文章还提供了常见问题解决方案和性能优化建议,帮助开发者顺利完成国产化平台适配工作。

weixin_42639246的博客 425

易学笔记-python语言-第5章:数字/5.4 python表达式操作符/5.4.1 操作符

python表达式操作符:笔记式Python视频精讲【初级篇】 三元选择表达式:x if y else z 逻辑或: x or y 逻辑与: x and y 逻辑非:not x 成员关系 x in y x not in y 对象实体测试 x is y x not is y 大小比较,集合子集,超集相等性操作 x &lt; y x &lt;= y x...

u011830122的专栏 270

我用两个月时间,终于把优快云付费资源项目玩明白了!

关于项目的定位,这是一个知识付费项目,根据市场研究机构的数据显示,到2023年,全球知识付费市场规模将达到2000亿美元,所以说知识付费的前景是十分广阔的,当下互联网时代信息大爆炸,80后、90后、00后群体对知识付费的认可度也大幅提升,同时这群作为知识付费的主流人群已经具备了更强的消费能力,而且对于知识付费的需求更高。目前整个知识付费领域仍然在发展起步的初期,尽快在知识付费领域布局可以帮助我们在这波红利中获得一份不错的收益。而且个人认为,

白话机器学习 2万+

Python学习笔记 - 5.字符串(find方法,格式操作符%)

字符串是字符的序列使用方括号运算符逐一访问每个字符。方括号里的表达式称为索引。 在Python中, 索引是从字符串头部算起的一个偏移量, 第一个字母的 偏移量为0。另一种方法是使用负索引, 从字符串结尾倒过来计算。 表达式fruit[-1]表示最后一个字母, fruit[-2]是倒数第二个字母。通过

R9barcelona|图像处理|机器学习 3394

电力电子新手必看:COT控制模式的工作原理与典型应用场景解析

本文深入解析了COT控制模式的工作原理及其在电力电子领域的典型应用。COT控制(恒定导通时间控制)通过固定导通时间机制,解决了传统滞环控制的问题,特别适用于需要快速瞬态响应和高效率的应用场景,如CPU/GPU供电和便携设备电源管理。文章详细介绍了COT控制的核心流程、技术特点及设计实践中的关键考量,帮助电力电子新手快速掌握这一技术。

weixin_30745553的博客 525

PCL 法向量夹角剔除错误匹配点对【2026最新版】

实现了一种简单的基于对应点法线角度的对应关系去除方法。博客长期更新,本文最新更新时间为:2026年5月24日。

点云侠的博客 41万+

易学笔记-python语言-第5章:数字/5.3 内置数字工具/5.3.1 表达式操作符

表达式操作符:+:加-:减*:乘/:除%:求余==:两个数相等<:小于<=:小于等于>:大于>=:大于等于>>:右位移00011 >> 2:表示把最右边的两位移除掉 变成 000,同时在左边补充两位0,最后变成 00 000,所以最后结果是0b00000(等于十进制的0)<<:左位移错误:0b1100 <&l...

weixin_33796177的博客 125

【嵌入式DIY实例ESP32篇】-步数计数器

在本项目中,我们将使用 ESP32 微控制器和博世的 BMI160 加速度计陀螺仪模块构建一款便携式计步器

视觉与物联智能 803

《Python学习手册》学习笔记(5)之第5章数字(关键词:编程语言/Python/数字/整数/进制)

第5章 数字写在开头的读者笔记值得一读的小节1.Python表达式操作符1.1. is操作符测试对象身份,也就是内存地址,严格意义上的相等。1.2.一些我不熟悉的操作符x is y, x is not y 对象实体测试 x | y 位或,集合并集 x ^ y 位异或,集合对称差 x & y

Henry1991back的博客 4785

数学建模模型总结

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

python3.5版本的保留字总数是_Python学习分享005:保留字

分享的内容是自己的学习笔记,并不是教程。我是零基础小白,如果有什么不对的地方,请大家看到了指出来,谢谢阅读。关键字/保留字在Python中具有特殊功能的标识符称为关键字。同内置函数一样,关键字是Python已经自己定义并保留使用的,不允许开发者再定义和关键字相同名字的标识符。输入下面指令,可查看关键字(35个):import keywordprint(keyword.kwlist)可以用help(...

weixin_39864387的博客 2514

深度学习工具包 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常用操作符及优先级

算术操作符 + , - , * , / , % , ** , // 百分号(%):表示取余 5 % 2 # 结果为1  幂运算操作符(**)表示阶乘 3 ** 2 # 结果为9 幂运算时,要注意优先级的问题,幂运算操作符比其左侧的一元操作符优先级高,比其右侧的一元操作符优先级低 例如:-3 ** 2,Ta的结果为:-9 3 ** -2, Ta的结果为:0.111111111

shirley_sweet的博客 3759

Python笔记之操作符与表达式

本章节详细介绍Python中的操作符与表达式,包括算术操作符、比较操作符、逻辑操作符、位操作符以及运算优先级。操作符是Python中进行各种运算的基础,理解这些内容有助于更高效地编写程序。

K 458

python 操作符介绍

python 操作符介绍

qq_40671063的博客 2110

Python--操作符

我把常用的Python操作符分为以下4类:标准算数操作符;比较操作符;逻辑操作符;位运算操作符。除了最后一类一般只在位运算中使用,应用面不算很广,其他三类则基本涵盖了整个Python语言的基本应用。 1. 标准算数操作符: (1)  ~~+   ~~ -   ~~ *   ~~ /:加减乘除,不用多做解释了。需要注意的是,+ 号和 * 号在列表的相关运算当中,还有其他含义,这一点,我

guoziqing506的博客 3608

python操作符是什么意思_Python的操作符

1. 运算操作符a. 运算操作符i. 4个最普遍的运算符 + - * / ,以及简便的表达方式。如a = 5, 那么a = a -3 可以写成 a -= 3,这种简写表示的是【用等号右边的值给变量进行等号左边的操作】ii. 双//代表地板(floor)除法:取整数。如10//3=3,4.9//2=2.0,左边是整型结果就是整型,左边是浮点数结果就是浮点数。iii. % 是取余数的意思,如 5 /...

weixin_39765625的博客 405

Python基础——常用操作符

文章目录算数操作符 算数操作符 Python的算数操作符大部分和我们理解的一样,但尤其要注意一些特别的操作符。 一般常用的操作符有: +−∗/%∗∗// +\quad -\quad *\quad /\quad \%\quad **\quad //\quad+−∗/%∗∗// 前四个不用过多介绍了,加减乘除。 此外,Python和其他大多数编程语言一样,可以简化写法。例如:a = a+1 可以写成 a += 1 等。 ...

薛定谔的猫的博客 1796

3.Python中常用操作符及其优先级的使用方法

和大多数编程语言一样,Python也有常用的操作符,操作符是进行编程不可或缺的一部分,下面为大家介绍一下Python中常用的操作符。 1.python中的常用操作符【以Python3.6版本为准】 操作符(常用的操作符)分为: 算术操作符:【+】【-】【*】【/】【%】【**】【//】 逻辑(布尔)操作符 :【and】 【or】【not】 比较(关系)操作符:【>】【&...

愿你走出半生,归来仍是少年! 4812

Python操作符的重载

本文详细介绍了Python操作符重载的概念和应用,包括增强代码可读性、实现类的数学运算、支持自定义数据结构、简化API设计、支持更多Python内置操作和实现链式操作等。操作符重载允许自定义对象与内置操作符交互,提高代码表达力和可读性,同时强调了使用时需确保直观意义的一致性。

黎明鱼儿 1289
上一篇: Python笔记(4)内置函数
下一篇: Python笔记(6)-输入输出
ToryCode
博客等级 码龄3年 44粉丝 130原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值