


Python basic learning logical operators, member operators, operator priority
Connecting the previous two articles, this article will continue to tell you about the basic learning of Python's logical operators, member operators and operator precedence. It has high learning value and is interesting. friends to find out.
Logical operators
Python language supports logical operators. The following assumes that variable a is 10 and b is 20:
Logical expression | Description | Instance | |
---|---|---|---|
x and y | Boolean "AND" - If x is False, x and y returns False, otherwise it returns the calculated value of y. | (a and b) returns 20. | |
x or y | Boolean "or" - if x is True, it returns True, otherwise it returns the calculated value of y. | (a or b) returns 10. | |
not x | Boolean "not" - If x is True, returns False. If x is False, it returns True. | not(a and b) Return False |
#!/usr/bin/python3 a = 10 b = 20 if ( a and b ): print ("1 - 变量 a 和 b 都为 true") else: print ("1 - 变量 a 和 b 有一个不为 true") if ( a or b ): print ("2 - 变量 a 和 b 都为 true,或其中一个变量为 true") else: print ("2 - 变量 a 和 b 都不为 true") # 修改变量 a 的值 a = 0 if ( a and b ): print ("3 - 变量 a 和 b 都为 true") else: print ("3 - 变量 a 和 b 有一个不为 true") if ( a or b ): print ("4 - 变量 a 和 b 都为 true,或其中一个变量为 true") else: print ("4 - 变量 a 和 b 都不为 true") if not( a and b ): print ("5 - 变量 a 和 b 都为 false,或其中一个变量为 false") else: print ("5 - 变量 a 和 b 都为 true")
1 - 变量 a 和 b 都为 true 2 - 变量 a 和 b 都为 true,或其中一个变量为 true 3 - 变量 a 和 b 有一个不为 true 4 - 变量 a 和 b 都为 true,或其中一个变量为 true 5 - 变量 a 和 b 都为 false,或其中一个变量为 false
Description | Instance | |
---|---|---|
Returns True if a value is found in the specified sequence, False otherwise. | x is in the y sequence, returns True if x is in the y sequence. | |
Returns True if the value is not found in the specified sequence, False otherwise. | x is not in the y sequence, returns True if x is not in the y sequence. |
#!/usr/bin/python3 a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print ("1 - 变量 a 在给定的列表中 list 中") else: print ("1 - 变量 a 不在给定的列表中 list 中") if ( b not in list ): print ("2 - 变量 b 不在给定的列表中 list 中") else: print ("2 - 变量 b 在给定的列表中 list 中") # 修改变量 a 的值 a = 2 if ( a in list ): print ("3 - 变量 a 在给定的列表中 list 中") else: print ("3 - 变量 a 不在给定的列表中 list 中")
1 - 变量 a 不在给定的列表中 list 中 2 - 变量 b 不在给定的列表中 list 中 3 - 变量 a 在给定的列表中 list 中
Description | Example | |
---|---|---|
is is used to determine whether two identifiers refer to an object. | x is y, if id(x) is equal to id(y), | is returns Result 1 |
is not is to determine whether the two identifiers refer to different objects. | x is not y, if id( x) is not equal to id(y). | is not Return result 1 |
#!/usr/bin/python3 a = 20 b = 20 if ( a is b ): print ("1 - a 和 b 有相同的标识") else: print ("1 - a 和 b 没有相同的标识") if ( id(a) == id(b) ): print ("2 - a 和 b 有相同的标识") else: print ("2 - a 和 b 没有相同的标识") # 修改变量 b 的值 b = 30 if ( a is b ): print ("3 - a 和 b 有相同的标识") else: print ("3 - a 和 b 没有相同的标识") if ( a is not b ): print ("4 - a 和 b 没有相同的标识") else: print ("4 - a 和 b 有相同的标识")
1 - a 和 b 有相同的标识 2 - a 和 b 有相同的标识 3 - a 和 b 没有相同的标识 4 - a 和 b 没有相同的标识
Description | |
---|---|
Index (highest precedence) | |
Bitwise flip, unary plus sign and minus sign (the last two methods are named @ and -@) | |
Multiplication, division, modulo and integer division | |
Addition and subtraction | |
Right shift, left shift operator | |
bit'AND' | |
Bitwise operators | ##<= < > >= |
<> == != | |
= %= /= //= -= = *= **= | |
is is not | |
## in not in | |
not or and | |
The following example demonstrates the operation of all operator priorities in Python: |
(a + b) * c / d 运算结果为: 90.0 ((a + b) * c) / d 运算结果为: 90.0 (a + b) * (c / d) 运算结果为: 90.0 a + (b * c) / d 运算结果为: 50.0
Related tutorials:
Python video tutorialThe above is the detailed content of Python basic learning logical operators, member operators, operator priority. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values nor output imaginary results.

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values of the variables, not the data types. If the two values are the same, it returns a true value; if the two values are not the same, it returns a false value.

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

Magic methods in Python are special methods that allow you to add "magic" to a class. They are often named surrounded by two underscores. Python's magic method, also known as the dunder (double underline) method. Most of the time, we use them for simple things like constructors (init), string representations (str, repr) or arithmetic operators (add/mul). In fact, there are many methods that you may not have heard of but are very useful. In this article, we will sort out these magic methods! We all know the size of the iterator __len__ method, which can be used in container classes Implement the len() function on. However, if you want to get the length of a class object that implements iterator
