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

Python Minimalist tutorial (6) operators

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

Share

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

Operator, the addition, subtraction, multiplication and division that we use in our daily life, is a kind of operator. Of course, this is generally called the arithmetic operator, which is used to deal with numeric operations.

But in computer languages, there are many operators. Used to deal with situations where they are not in use.

There are mainly the following categories:

Arithmetic operator comparison operator logic operator member operator arithmetic operator

Arithmetic operators are everyday addition, subtraction, multiplication and division, and these operators can be used for numeric type operations in computer languages.

Operator description example + add 5 subtract 5-3 result is 11-subtract 5-3 result is 2 * multiplication 3 * 6 result is 18 times power, x to the y power 3 * * 3 result is 27 / division, general division 5max 2 result is 2.5pm / take integral division, also known as floor division, only take the integer part of quotient 5Universe 2 result is 2% remainder, commonly used in parity and whether can be divisible judgment 5% 2 result is 1

Addition and multiplication can also be used for string operations:

> > S1 = 'hello' > S2 =' world' > concatenation of S1 + S2 # string 'helloworld' > S1 * 3 # string multiplies several times, which is equivalent to how many times to repeat the string.' Hellohellohello' comparison operator

The comparison operator is to determine whether it is equal, greater than, less than, etc. The returned results are Boolean values, which are mainly used for conditional judgment.

Operator description example = =, to determine whether equal (must have two equal signs), equal returns True, unequal returns false equal = unequal, unequal returns True, equality returns False5! = 6, returns True > greater than, left greater than right returns True, otherwise returns False5 > 4, returns True= greater than or equal to 5 > = 5, returns True number > 'nemo' >' hello' # n after h So n is bigger than h True

Numeric types cannot be compared with strings.

> >'a'> 1 # comparison operator > does not support comparing TypeError: > 'not supported between instances of' str' and 'int' logical operators between str types and int types

Logical operators, also known as Boolean operators, are mainly used to join multiple conditional operations. The logical operators in Python simply and rudely use English words instead of symbols, which is easy to remember.

Operator describes the example and and operation. If the condition before and after and is True, return True, if one is not True, return False3 > 2 and 2 > 4 return, Falseor or operation, return True as long as one of the conditions before and after or is True, return False3 > 2 or 2 > 4 only if both conditions are False, return Truenot non-operation, followed by not condition, select not 3 > 2 for the result of the condition, and return False member operator

The member operators are only in and not in, and are mainly used to determine whether a value is in a sequence (later).

Operator description example in determines whether a value is in a sequence, returns True if it is not, returns False'h' in 'hello', if not, returns Truenot in to determine whether a value is not in a sequence, returns True if not, and returns False'x' not in' hello', if it returns the True identity operator

In Python, there is an identity operator is that determines whether the object of a variable is the same object. In data types, all data types in Python are objects. That is, objects are stored in all variables.

In Python, there is a function id () that specifically checks whether the object is the same, and id () is used to return the address of the object in memory. If the content stored in the same memory address is the same object.

> a = 1 > > b = 1784179728 > id (a) 1740733557424 > > an is bFalse > c = 1 > id (c) 1784179728 > an is c # is equivalent to id (a) = id (c) True

Identity operator to understand first, if there is any doubt can be raised in the comments, if really can not understand, you can skip, generally speaking, rarely involved!

Thinking about this section

3 > 2 > 1, what is returned?

(3 > 2) > 1, what is the return? Why?

What about the return of (3 > 2) = 1? Do you understand anything?

What about'a'<'A'? Why?

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

Internet Technology

Wechat

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

12
Report