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

Example Analysis of Python arithmetic Operation and arithmetic expression

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "Python arithmetic operation and example analysis of arithmetic expression". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Arithmetic operators and arithmetic expressions

Arithmetic operator is the most basic operator of programming language. In addition to +, -, *, /, and% (remainder), the arithmetic operators provided in the Python language tutorial also provide two operators that are not provided in C #: exponentiation (*) and integer division (/ /). Let's understand these two arithmetic operators through a piece of code:

#-*-coding:utf-8-*- x = 3.3y = 2.2a = x x/y11 print print a # output 13.827086118, that is, 2.2th power of 3.3.In C #, the Pow method can be used to realize the power operation b = x print b # output 1.0.To divide the whole part of the return quotient c = x/y11 print c # output 1.5. pay attention to the difference between ordinary division and integral division.

Assignment operator and assignment expression

Assignment is to assign a new value to a variable. In addition to simple = assignment, both Python and C # support compound assignment, such as x + = 5, which is equivalent to x = x + 5.

The Python language tutorial does not support the self-increment and self-subtraction operators in C #. For example, statements such as xincrement + are prompted with syntax errors in Python. C # programmers may be accustomed to this expression (or why it is called ClearCraft +). In Python, just write x + = 1 honestly.

Logical operators and logical expressions

The logical operators of Python are quite different from those of C#. Python replaces the logical operators & &, | and! in the C# language with the keywords and, or and not. In addition, the operands involved in logical operations in Pyhton are not limited to Boolean types, and values of any type can participate in logical operations. See the discussion in Section 1.2.2 (Boolean types).

Connecting operands or expressions with logical operators is a logical expression. Like C #, logical expressions in Python are "short-circuited", meaning that the value to the right of the logical expression is evaluated only when needed, such as the expression an and b, which evaluates b only if an is True. Think about it, if (0 and 10amp 0): will this statement throw an exception with a divisor of zero?

Also note that in the Python language tutorials, the logical operations performed by and and or do not return Boolean values, but return one of the values they actually compare. Here is an example:

Print 'a'and 'b' # output b print''and 'b' # output empty string

Relational operators and relational expressions

Relational operation is actually a kind of logical operation, and the return value of relational expression is always Boolean. The comparison operators in Python are exactly the same as C #, including = =,! =, >, = and

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