Python 语法之操作符和表达式

本文详细介绍了Python中的各种基本运算符,包括数值运算符、位运算符、比较运算符以及复合运算符等,并提供了丰富的示例说明。此外,还讲解了Python中的类型转换方法和布尔类型操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载信息:http://blog.chinaunix.net/uid-20393955-id-345384.html

File information

2009-10-23

磁针石:xurongzhong#gmail.com

博客:oychw.cublog.cn

参考资料:

《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]

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数工具箱: 优化工具箱:用于解决运动学逆解路径规划中的优化问题。 Simulink:可视化建模环境,适合构建仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值