More on Conditions - To Compare -Comparing Sequences and Other Types

The conditions used in while and if statements can contain any operators, not just comparisons.

The comparison operators in and not in check whether a value occurs (does not occur) in a sequence. The operators is and is not compare whether two objects are really the same object; this only matters for mutable objects like lists. All comparison operators have the same priority, which is lower than that of all numerical operators.

Comparisons can be chained. For example, a < b == c tests whether a is less than b and moreover b equals c.

Comparisons may be combined using the Boolean operators and and or, and the outcome of a comparison (or of any other Boolean expression) may be negated with not. These have lower priorities than comparison operators; between them, not has the highest priority and or the lowest, so that A and not B or C is equivalent to (A and (not B)) or C. As always, parentheses can be used to express the desired composition.

The Boolean operators and and or are so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined. For example, if A and C are true but B is false, A and B and C does not evaluate the expression C. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument.

It is possible to assign the result of a comparison or other Boolean expression to a variable. For example,

>>> string1, string2, string3 = ‘‘, ‘Trondheim‘, ‘Hammer Dance‘

>>> non_null = string1 or string2 or string3
>>> non_null
‘Trondheim‘

>>>non_null = string1 and string2 and string3
>>>non_null

‘‘

Note that in Python, unlike C, assignment cannot occur inside expressions. C programmers may grumble about this, but it avoids a common class of problems encountered in C programs: typing = in an expression when == was intended.

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. Some examples of comparisons between sequences of the same type:

(1, 2, 3)              < (1, 2, 4)
[1, 2, 3]              < [1, 2, 4]
‘ABC‘ < ‘C‘ < ‘Pascal‘ < ‘Python‘
(1, 2, 3, 4)           < (1, 2, 4)
(1, 2)                 < (1, 2, -1)
(1, 2, 3)             == (1.0, 2.0, 3.0)
(1, 2, (‘aa‘, ‘ab‘))   < (1, 2, (‘abc‘, ‘a‘), 4)

Note that comparing objects of different types with < or > is legal provided that the objects have appropriate comparison methods. For example, mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the interpreter will raise a TypeErrorexception.

时间: 2024-08-27 00:52:21

More on Conditions - To Compare -Comparing Sequences and Other Types的相关文章

Python 2.7.8 学习笔记(001)python manuals/the python tutorial

从今天开始学python, python有点意思,第一感觉界面和matlab有点像. 手头没有什么资料,就从安装好了的一个python 2.7.8,里面有个英文版的manual,那就只好从这里开始吧,为什么不是中文版的呢??那就边看边翻译吧. python漫游指南:python是一种简单易学功能强大的编程语言.它有高效的数据结构和简单但有效的面向对象编程方法.python优雅的语法和动态拼写以及解释特性,使得它在许多平台上成为一种理想的脚本语言和快速程序开发工具. python的解释器和扩展标准

Python数据结构(二)

5.2. The del statement There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

监控视频相关数据集

BOSS dataset Website: Datasets are available here. Dataset: The BOSS project aims at developing an innovative and bandwidth efficient communication system to transmit large data rate communications between public transport vehicles and the wayside. I

difflib文件差异对比

1.两个字符串差异对比: #!/usr/bin/env python # -*- coding:utf-8 -*- import difflib text1 = '''text1: This module provides classes and functions for comparing sequences. including HTML and context and unified diffs. difflib document v7.4 add string ''' text1_li

二、业务服务监控

二.业务服务监控 1.文件内容差异对比方法 difflib模块实现文件内容差异对比,difflib作为python的标准库模块,无需安装,作用是对比文本之间的差异,且支持输出可读性比较强的HTML文档,与linux下的diff命令相似.我们可以使用difflib对比代码,配置文件的差别,在版本控制方面是非常有用. (1)示例:两个字符串的差异对比 通过使用difflib模块实现两个字符串的差异对比,然后以版本控制风格进行输出 [/root/text1_lines.py] #! /usr/bin/

学习python自动化运维diff-text

1.通过html 比较diff_text两文本不同之处 #!/usr/bin/env python import difflib text1 = """text1: This module provides classes and functions for comparing sequences. including HTML and context and unified diffs. difflib document v7.4 add string "&quo

文件内容差异对比方法

一.两个字符串的对比 1.两个字符串的对比输出 #!/bin/env python import difflib text1 = """text1: This module provides classes and functions for comparing sequences v7.5""" text1_lines = text1.splitlines() text2 = """text2: This modu

PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations

BACKGROUND OF THE INVENTION? 1. Field of the Invention? The present invention relates generally to heterogeneous computer systems.? 2. Background Art? Computers and other such data processing devices have at least one control processor that is genera