Python笔记:算术运算符, 赋值运算符, 布尔运算符,比较运算符和逻辑运算符
算术运算符
+
加-
减*
乘/
除%
取余(相除后的余数)**
取幂(注意^
并不执行该运算,你可能在其他语言中见过这种情形)//
相除后向下取整到最接近的整数
算术运算符[相关练习]
在过去三个月的电费是 23 美元、32 美元和 64 美元。这三个月的每月平均电费是多少?
print((23 + 32 + 64) / 3)
一个区域是宽 9 块砖,长 7 块砖,另一个区域是宽 5 块砖,长 7 块砖。一包砖有 6 块。问:(1)需要多少块砖?(2) 如果购买 17 包砖,每包有 6 块砖。剩下多少块砖?
# Fill this in with an expression that calculates how many tiles are needed. print(9*7 + 5*7)# Fill this in with an expression that calculates how many tiles will be left over. print(17*6 - (9*7 + 5*7))
赋值运算符
注:图片来源网络
赋值运算符[相关练习]
题目 A: 这道练习的注释(以 # 开头的行)提供了创建和修改变量的说明。请在每条注释后面根据说明写一行代码。
[注意] 这段代码使用了科学记数法来定义很大的数字。4.445e8
等于 4.445 * 10 ** 8
,也就是 444500000.0
。
# The current volume of a water reservoir (in cubic metres)
reservoir_volume = 4.445e8
# The amount of rainfall from a storm (in cubic metres)
rainfall = 5e6# decrease the rainfall variable by 10% to account for runoff
rainfall *= 1-0.1
# add the rainfall variable to the reservoir_volume variable
reservoir_volume += rainfall
# increase reservoir_volume by 5% to account for stormwater that flows
# into the reservoir in the days following the storm
reservoir_volume *= 1+0.05
# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume *= 1-0.05
# subtract 2.5e5 cubic metres from reservoir_volume to account for water
# that's piped to arid regions.
reservoir_volume -= 2.5e5
# print the new value of the reservoir_volume variable
print(reservoir_volume)
题目 B: 思考下面代码的输出:
# 这是关于山景城人口和人口密度的原始数据。
mv_population = 75000
mv_area = 11.995
mv_density = mv_population/mv_area
print(int(mv_density)) # 6252
布尔型运算符、比较运算符和逻辑运算符
布尔数据类型存储的是值
True
或False
,通常分别表示为1
或0
。通常有
6
个比较运算符会获得布尔值:符号使用情况 布尔型 运算符 5 < 3 False 小于 5 > 3 True 大于 3 <= 3 True 小于或等于 3 >= 5 False 大于或等于 3 == 5 False 等于 3 != 5 True 不等于 需要熟悉的三个逻辑运算符
逻辑使用情况 布尔型 运算符 5 < 3 and
5 == 5False and
- 检查提供的所有语句是否都为 True5 < 3 or
5 == 5True or
- 检查是否至少有一个语句为 Truenot
5 < 3True not
- 翻转布尔值
布尔型运算符、比较运算符和逻辑运算符[相关练习]
- 哪个地区人口密度更高?里约还是旧金山?
sf_population, sf_area = 864816, 231.89
rio_population, rio_area = 6453682, 486.5san_francisco_pop_density = sf_population / sf_area
rio_de_janeiro_pop_density = rio_population / rio_area# Write code that prints True if San Francisco is denser than Rio, and False otherwise
print(san_francisco_pop_density > rio_de_janeiro_pop_density) # False
- 为何 Python 使用
==
检查是否相等,而不是=
?
- 因为
=
用来为变量赋值的
Python笔记:算术运算符, 赋值运算符, 布尔运算符,比较运算符和逻辑运算符相关推荐
- Python入门--算术运算符,位运算符,比较运算符,布尔运算符,赋值运算符
#Python中常用的运算符 #算数运算符 #赋值运算符 #比较运算符 #布尔运算符 #位运算符#算术运算符 --> 标准算术运算符.取余运算符.幂运算符 # 标准运算符 --> 加+减- ...
- 在学习Python基础中需要知道的知识点:运算符大全,收藏,以后方面查询(算术运算符、赋值运算符、比较运算符、位运算符、逻辑运算符、成员运算符、身份运算符、运算符优先级))
一.算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘或是返回一个被 ...
- python中算术运算符_python中的基本算术运算符有哪些
python中的基本算术运算符有哪些 发布时间:2020-11-16 14:54:33 来源:亿速云 阅读:74 今天就跟大家聊聊有关python中的基本算术运算符有哪些,可能很多人都不太了解,为了让 ...
- C++primer 第 4 章 表达式 4.1基础 4 . 2 算术运算符 4 .3 逻辑和关系运算符 4 . 4 赋值运算符 4 .5 递增和递减运算符 4.6成员访问运算符
表达式由一个或多个运算对象(operand)组成,对表达式求值将得到一个结果(result) 字面值和变量是最简单的表达式(expression),其结果就是字面值和变量的值.把一个运算符(opera ...
- python笔记:#010#运算符
运算符 目标 算数运算符 比较(关系)运算符 逻辑运算符 赋值运算符 运算符的优先级 数学符号表链接:https://zh.wikipedia.org/wiki/数学符号表 01. 算数运算符 是完成 ...
- python基本数据类型(四)-集合与运算符-python3笔记
1.集合 2.字典 3.运算符优先级 1.集合 创建:() set() 注意:创建空的集合要用set() 特点:元素唯一,无序 运算: &(交集) |(并集) -(差集) 方法: s.add( ...
- Python的输入指令、格式化输出、基本运算符
Python的输入指令.格式化输出.基本运算符 Python的输入指令input name = input('Could I know your name please?') 在Python3版本下, ...
- python中find函数运算结果类型_Python 运算符与数据类型
Python 运算符 运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算,在Python中运算符大致可以分为7种类型:算术运算符.比较运算符.赋值运算符.逻辑运算符.位运算等,下面的例子将 ...
- python中基本运算符_Python中的基本运算符及示例
python中基本运算符 Operators are symbols which tells the interpreter to do a specific operation such as ar ...
最新文章
- SAP战略中的机器学习
- python太难学了-为何编程那么难?新手该怎么学Python?
- python 基础教程:对 property 属性的讲解及用法
- 简单实例讲解linux的module模块编译步骤
- Windows下使用Caffe-Resnet
- 银行家算法总结及实现
- linux下解压 编译 安装,Linux 下开发环境安装配置-编译、解压、超链、
- MTK:屏幕模板机制
- python latex显示不出来_10 个加速Python数据分析的简单的小技巧
- java实现:判断是否是素数
- bulk interface驱动_USB驱动程序(较详细)一
- 课设-基于51单片机+超声波模块的避障小车(源码+原理图+Protel仿真)
- Cyclone IV系列FPGA串口远程烧写详解
- java实习生面试一些技巧
- ZeroTier 和 FRP 速度对比实验(附搭建方法)
- 计算机如果没有什么 就无法启动,电脑开机没有任何反应
- buflab-计算机系统实验
- 单片机中 读引脚 和读锁存器的区别
- 开启Windows的文件大小写区分功能
- 实证研究的步骤_开题报告的研究方法怎么写(最新整理)