python点操作符语法,Python 语法之操作符和表达式
转载信息:http://blog.chinaunix.net/uid-20393955-id-345384.html
File information
2009-10-23
磁针石:xurongzhong#gmail.com
参考资料:
《Python Essential Reference 4th Edition 2009》
《beginning python from novice to professional second edition 2008》
-----------------------------------------------------------------------------------------------------------------------------------
*数值运算符
x + y 加法
x - y 减法
x * y 乘法
x / y 除法
x // y 整除
x ** y 乘方
x % y 取模
–x Unary minus
+x Unary plus
Python 2,两个整数相除结果为取模,Python 3,7/4得1,Python 3中改为用浮点运算,得1.75。Python 2导入:from __future__ import division,强制转换。Future一般用于调用以后版本会实现的功能。
>>>2.75 % 0.5
0.25
>>>-3 ** 2
-9
>>> (-3) ** 2
9
长整数的处理:
>>>1000000000000000000
1000000000000000000L
2.2及以前版本处理的范围:2147483647 (or smaller than –2147483648)
16进制:
>>> 0xAF
175
八进制:
>>>010
8
变量必须赋值才能使用。
位操作符:
x << y Left shift
x >> y Right shift
x & y Bitwise and
x | y Bitwise or
x ^ y Bitwise xor (exclusive or)
~x Bitwise negation
python在位运算不会自动截断,要注意到是否会产生巨长的结果。
其他运算符:
abs(x) Absolute value
divmod(x,y) Returns (x // y, x % y)
pow(x,y [,modulo]) Returns (x ** y) % modulo
round(x,[n]) Rounds to the nearest multiple of 10-n (floating-point numbers
only)
注意pow可以作为三元运算符,一般用于加密算法。
round以远离0为目标:round(0.5)得1,round(-0.5)得-1。Python 3中有点怪异:round(0.5)和round(0.5)得0,round(1.5)得2, round(-1.5)得-2。
算数比较符:
x < y Less than
x > y Greater than
x == y Equal to
x != y Not equal to
x >= y Greater than or equal to
x <= y Less than or equal to
它们的连接,比如w < x < y < z,理解为w < x and x < y and y < z
python中的隐式转换并不多。
*复合运算符
x +=y
x -=y
x *=y
x /=y
x //=y
x **=y
x %=y
x& =y
x |=y
x ^=y
x>> =y
x<< =y
a = 3
b = [1,2]
c = "Hello %s %s"
a += 1 # a = 4
b[1] += 10 # b = [1, 12]
c %= ("Monty", "Python") # c = "Hello Monty Python"
*点号
*函数调用()
def foo(x,y,z):
return x+y+z
from functools import partial
f = partial(foo,1,2)
f(3)
这个东东和currying进程很类似。
*函数调用()
def foo(x,y,z):
return x+y+z
from functools import partial
f = partial(foo,1,2)
f(3)
这个东东和currying进程很类似。
*类型转换
int(x [,base]) Converts x to an integer. base specifies the base if x
is a string.
float(x) Converts x to a floating-point number.
complex(real [,imag]) Creates a complex number.
str(x) Converts object x to a string representation.
repr(x) Converts object x to an expression string.
format(x [,format_spec]) Converts object x to a formatted string.
eval(str) Evaluates a string and returns an object.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
set(s) Converts s to a set.
dict(d) Creates a dictionary. d must be a sequence of
(key,value) tuples.
frozenset(s) Converts s to a frozen set.
chr(x) Converts an integer to a character.
unichr(x) Converts an integer to a Unicode character (Python 2
only).
ord(x) Converts a single character to its integer value.
hex(x) Converts an integer to a hexadecimal string.
bin(x) Converts an integer to a binary string.
oct(x) Converts an integer to an octal string.
a = int("34") # a = 34
b = long("0xfe76214", 16) # b = 266822164L (0xfe76214L)
b = float("3.1415926") # b = 3.1415926
c = eval("3, 5, 6") # c = (3,5,6)
*布尔类型操作符
x or y If x is false, return y; otherwise, return x.
x and y If x is false, return x; otherwise, return y.
not x If x is false, return 1; otherwise, return 0.
*对象相等
相等:(x == y)
是同一对象:is
*运算顺序
除了乘方以外,都是由左至右的。
优先级由高到低:
(...), [...], {...} Tuple, list, and dictionary creation
s[i], s[i:j] Indexing and slicing
s.attr Attributes
f(...) Function calls
+x, -x, ~x Unary operators
x ** y Power (right associative)
x * y, x / y, x // y, x % y Multiplication, division, floor division, modulo
x + y, x- y Addition, subtraction
x<< y, x >> y Bit-shifting
x & y Bitwise and
x ^ y Bitwise exclusive or
x | y Bitwise or
x < y, x <= y, Comparison, identity, and sequence membership
tests
x > y, x >= y,
x == y, x != y
x is y, x is not y
x in s, x not in s
not x Logical negation
x and y Logical and
x or y Logical or
lambda args: expr Anonymous function
*条件表达式
if a< = b:
minvalue = a
else:
minvalue = b
等同于:
minvalue = a if a <=b else b
values = [1, 100, 45, 23, 73, 37, 69 ]
clamped = [x if x < 50 else 50 for x in values]
print(clamped) # [1, 50, 45, 23, 50, 37, 50]
python点操作符语法,Python 语法之操作符和表达式相关推荐
- Python输出格式化 格式化字符串语法 format f-string 格式化操作符% 数据类型转换 对齐方式 转换标志字符
Python输出格式化 格式化字符串语法 1.format 1.1 Format String Syntax 格式字符串语法 str.format() 方法和 Formatter 类共享相同的格式字符 ...
- c++提供的可有效分配对象空间的运算符是_Python 为什么不支持 i++ 自增语法,不提供 ++ 操作符?
在 C/C++/Java 等等语言中,整型变量的自增或自减操作是标配,它们又可分为前缀操作(++i 和 --i)与后缀操作(i++ 和 i--),彼此存在着一些细微差别,各有不同的用途. 这些语言的使 ...
- python getattr_详解 Python 的二元算术运算,为什么说减法只是语法糖?
原题 | Unravelling binary arithmetic operations in Python 作者 | Brett Cannon 译者 | 豌豆花下猫("Python猫&q ...
- python输入两个操作数和一个操作符_Python基础学习笔记贰
第四天 1.数值类型(部分): 布尔类型,整型,浮点型. 整型:python的整型结合了整型和长整型.所以python很方便进行大数运算. 浮点型:小数类型,python区分整型和浮点型根据小数点.有 ...
- 【阿里内部教程】python初阶:基础语法 python全栈自动化测试系类
目录 很多小伙伴可能都没有看过凡哥的视频,所以大家可能对凡哥不是很了解这里先和大家来个自我介绍 凡哥我已经有着十二年互联网自动化测试和测试开发工程师,拥有丰富的自动化测试平台及测试开发经验,擅长接口测 ...
- Python基础语法、python基础数据类型、python解释器、python注释符、python-range()和sum()
前段时间准备考研去了,没什么时间写博客,老师一番教诲,细想我这自考本的身份去考名校,万一没考上在这里白学了,到时候找工作也是个难事.细想自己的环境不容自己这么极端,也是自己把自己想的太美好了,怎么可能 ...
- python基础语法--python语言及其应用
python基础语法 python引言 python python语言是一种高级动态.完全面向对象的语言. python中函数.模块.数字.字符串都是对象. python完全支持继承.重载.派生.多继 ...
- Python基础教学系列— 基础语法
标识符 所谓的标识符就是对变量.常量.函数.类等对象起的名字. 首先必须说明的是,Python语言在任何场景都严格区分大小写!也就是说A和a代表的意义完全不同 python对于表示标识符的命名有如下规 ...
- python汇编指令_Python基础语法
阅读目录: 第一节:编程基础 第一节:编程基础 第二节:语言分类 第三节:高级语言的发展 第四节:程序program 第五节:Python解释器 第六节:Python基础语法 第七节:程序控制 第八节 ...
最新文章
- hp服务器硬件安装,HP Gen8 服务器安装2008 R2 帮助手册
- CentOS6.x配置tomcat搭建JSP应用服务器
- 增加ActiveDirectory证书服务器有效期与续订步骤
- python使用界面-用python编写简单ui界面窗口
- 区块链BaaS云服务(29) 溪塔科技 CITA-Cloud 二
- 60.extjs-布局 (在column布局中使用fieldset 和 在fieldset中使用column布局)
- AWS安装CDH5.3-CentOS6.4中关键操作步骤
- java上传excel文件代码,求java把上传文件的excel表中数据存入数据库中.实现录入的代码?...
- windows登录linux免密码,Windows使用SSH Secure Shell实现免密码登录Linux的方法以及使用scp2命令免密码下载文件...
- 根据四个点坐标排列出左上右上右下左下位置关系
- 增加虚拟android内存,怎么给安卓手机增加虚拟内存?
- 高端物理学名词_物理名词中英文对照
- js-beautify 不换行
- NewStarCTF 公开赛wp
- 中央财经大学C语言考研真题答案,2017年中央财经大学信息学院901C语言程序设计考研题库...
- AirPods Pro 卡顿或突然没声音的原因之一
- 数十万csdn小白难题:自学软件测试,学到什么程度可以出去找工作啊?京东offer不要了,换字节跳动....
- 2022-2028年全球及中国光纤布拉格光栅(FBG)加速度计行业投资前景分析
- 研发管理心得,从技术小白做到CTO(研发总监)的辛酸之路
- cadence SPB17.4 - allegro - allegro_free_viewer
热门文章
- HTTP协议状态码详解(HTTP Status Code)(转)
- PresentViewController切换界面
- Redis系列(三)-Redis发布订阅及客户端编程
- 中外白领和无领的一天
- 130. 被围绕的区域
- centos7 cuda测试_CentOS 7 安装cuda环境
- 热血动漫番太好看了!用Python爬取了1T的动漫,内存都爆了
- mysql如何查询不等于_mysql查询不等于
- mybatisplus代码生成连接池_SpringBoot2 高级案例(15): 配置多数据源,整合MybatisPlus增强插件...
- Qt中采用多线程实现Socket编程