Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand python operands, operators, expressions and statements

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand python operands, operators, expressions and statements." Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to understand python operands, operators, expressions and statements"!

The essence of programming is data and operations. Data is represented by basic data types and data structures. Operations are various operations on these data, such as basic addition, subtraction, multiplication and division, right and wrong judgment, flow control, etc. These operations are the operators, expressions, and statements we're going to talk about today.

Hint: arithmetic and operation are often the same concepts in programming

For example, the following line of code:

a = 5; b = a + 7; c = a if a > b else b

Let's break down this line first:

Statement: Consists of expressions. This example has three statements, separated by semicolons, that is, a physical line has three logical lines;

Expression: Composed of operators and operands. In this example, the expressions are a + 7, a > b, etc.;

Operands: That is, various objects. In this example, a, b, c, 5, 7, etc.

Their relationship in one line is:

Operand + Operator-> Expression-> Statement

Operands and operators form expressions, expressions form statements

operands

An operand is a collection of objects, some of which are constants and some of which are variables.

constant

We have seen many constants in Python, such as integers: 9, 7, 8, etc., floating-point numbers: 3.14, 0.618, etc., strings: 'apes learn Python', these constants we know what they mean when we see them, this is the literal constant.

Literally constants are values that use them literally. For example, the number 8 always stands for itself; it is a constant and cannot change its value.

variables

Constants represent objects with a specific meaning, but not enough for programming purposes. Programming often requires something that can store data and perform operations on it, and this thing is a variable. To use variables frequently in programming, you need to give variables a name: variable name. The naming rules for variable names are the identifiers mentioned in Python's basic syntax. If you forgot, you can go to that section again.

In the first line of Python code, a, b, and c are variables, and 5, 12, and 5 are the values they store. If you add a = a + 3, the value of a becomes 8, and the value of variable a changes.

An assignment to a variable is to give it a value, for example a = 3, which is the value of 3 for variable a.

a = 3 #assign 3 to a, where a has the value 3b = a #assign a to b, where b has the value equal to a, also 3c, d = a, b #This is multiple assignment, equivalent to the c = a; d = b operator

An operator is the name of a specific operation performed on an operand. Different operators represent different operations, such as addition, subtraction, multiplication and division.

The Python shell makes it easy to experience the use of operators:

In [115]: 2 + 5Out[115]: 7In [116]: 8 * 9Out[116]: 72

Below we use a table to clearly list the various operators and their usage.

Example of operator name meaning

+ Add two objects and 2 + 5 gets 7; 'ape learning' + 'Python' gets 'ape learning Python'

- Subtract two numbers, or a negative number 10 - 5 gives 5;-3.1 means negative.

* Multiply two numbers, or repeat the string many times 3 * 7 to get 21;'py' * 2 to get pypy.

** Power Returns x to the y-th power 2**8 = 256; 2.3**3 = 12.167

Divide by 10 / 2 gives 5;10 / 3 gives 3.33333

//Divide down by two numbers to return the smallest integer closest to the quotient. If the divisor and dividend contain floating point numbers, the return is also floating point. 11 / 2 gets 5;-11/2 gets-6; 5/ 1.34 gets 3.0.

Divide by % modulo 10% 3 to get 1; 10% 3.3 to get 0.1;-10% 3. 3 to get 3.2, because-10 // 3.3 to get-4,-10-3.3 *-4 to get 3.2.

> 1 gets 1

8 & 9 = 8

`` bits or bits corresponding to two numbers OR `89` gets 9^bits XOR `or bits corresponding to two numbers XOR` 8^9 gets 1

The bits of a number x are all inverted, and the value is-9 of-(x+1)~8.

Greater than returns whether x is greater than y8 > 9 gets False.

= y Returns True

== Equal to compare objects equal2 == 2 returns True, 3 == 2 returns False,'str' == 'str' returns True

!= Not equal to comparison object is not equal 1 != 3 Returns True, 3 != 3 Return False

not Boolean returns False if x is True; Truex = False if x is False; not x returns True

and Boolean with x and y Returns False if x is False, otherwise returns Boolean value of y x = False; y = True; x and y returns False because x is False. In this case, Python no longer checks the Boolean value of y, because the x on the left of and is already False, and whether the y on the right is true or false does not affect the value of the entire expression, so it no longer checks whether y is true or false. This is called short-circuit evaluation.

or Boolean or returns True if x is True, otherwise returns the Boolean value of y x = True; y = False; x or y returns True. The same applies here for "short-circuit evaluation."

operator precedence (Operator precedence)

When we were young, we learned to multiply and divide first and then add and subtract. For example, if we want to calculate 2 + 5 * 6, we should calculate 5 * 6 first to get 30, and then calculate 2 + 30 to get 32. That is, multiplication operators take precedence over addition operators.

The following table shows Python's operator precedence, from low to high. Operators in the same cell have the same precedence, and the order of operation is from left to right. However, it is recommended that you group expressions (operators and operands) by using parentheses, which clearly indicates the order of operations and makes the program easier to read.

operator descriptions

lambda lambda function expression

if-else conditional expression

or Boolean or

and Boolean

not Boolean

in, not in, is, is not, =, !=, == Comparison, including membership test and identity test

`` bit or ^bit XOR

& bit and

shift

+, -plus, minus

*, @, /, //, % multiply, matrix multiply, divide, divide down, divide modulo

+x, -x, ~x integer, negative, bit not (bit reversed)

** Power operator. It is not as tightly bound as the arithmetic or bitwise unary operator to its right, so the value of 2**-1 is 0.5.

awaitawait expression (used in asyncio)

x[index], x[index:index], x(arguments...), x.attribute subscript, slice, function call, attribute reference

(expressions...), [expressions...], {key:value...}, {expressions...} Bindings or tuples display, list display, dictionary display, collection display

There are operators in the table above that we haven't touched on yet, which will be discussed in a later chapter.

The program performs operations according to the order of the table above, but we can also change the order of their calculations by parentheses. For example, in (2 + 7) * 3, we first calculate the addition in parentheses, and then multiply. This principle is the same as arithmetic learned in elementary school.

expression (Expression)

An expression is a piece of syntax that can be evaluated to a value. In other words, it is the accumulation of expression elements, such as literals, names, attribute accesses, operators, or function calls, that return a value. In contrast to many other languages, not all language structures are expressions. There are also statements that cannot be used as expressions, such as while. Assignment is also a statement, but not an expression.

From the definition of expression above, a > 3 is an expression, and a = 3 is not an expression, but an assignment statement.

Statements (Statements)

A statement is the unit of a program segment (a "block" of code). A statement can be an expression or a structure with keywords, such as if, while or for.

Examples of statements:

a = 5 #assignment statement if a > 3: #conditional statement, where a > 3 is an expression print ('a> 3')#Output statement here, I believe you have a deeper understanding of "how to understand python operands, operators, expressions and statements", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report