Python 快速入门笔记(4):表达式

本系列随笔是本人的学习笔记,初学阶段难免会有理解不当之处,错误之处恳请指正。转载请注明出处:https://www.cnblogs.com/itwhite/p/12297709.html

简介

像加减乘除、取余、赋值等这类运算,python 与其它语言没有太大的不同,本文仅介绍一些 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 
 y < x < z  x 大于 y 且小于 z ,这种方式叫:链式比较
 x is y

 x 和 y 是同一个对象,例如:
 x = y = [1, 2, 3]
 z = [1, 2, 3]
 x == y == z   # True
 x is y  # True
 x is z  # False

 x is not y   x 和 y 不是同一个对象 
 x in y   x 是容器(如序列) y 的成员 
 x not in y  x 不是容器(如序列) y 的成员

Python 中的“与”和“或”运算符

大多数编程语言都使用“&&”符号作为“与”操作运算符、“||”符号作为“或”操作运算符,python 中不支持这两种运算符,相对应地,python 中分别使用关键字 andor 来表示 “与” 和 “或” 运算符。

  • “与”操作使用 and ,python 不支持 &&
  • “或”操作使用 or ,python 不支持 ||

切片操作

Python 中 字符串列表元组 三种数据类型都属于 序列 ,序列都支持切片操作,常用切片操作的形式如下:

a[start:stop]       # items start through stop-1,左开右闭
a[start:]           # items start through the rest of the array
a[:stop]            # items from the beginning through stop-1
a[:]                # a copy of the whole array

# 另外一种形式:
a[start:stop:step]  # start through not past stop, by step
a[::-1]             # all items in the array, reversed

参考:https://stackoverflow.com/questions/509211/understanding-slice-notation

正则表达式

待补充。 

完。

猜你喜欢

转载自www.cnblogs.com/itwhite/p/12297709.html