In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the operator of Python". In daily operation, I believe that many people have doubts about how to use the operator of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use the operator of Python"! Next, please follow the editor to study!
Arithmetic operator
The arithmetic operators in Python are very rich. In addition to the most familiar addition, subtraction, multiplication and division operators, there are integral division operators, modular (remainder) operators and power operators. The following example shows the use of arithmetic operators.
"
Arithmetic operator
Version: 0.1
Author: Luo Hao
"
Print (321 + 123) # addition
Print (321,123) # subtraction
Print (321 * 123) # multiplication
Print (321 / 123) # Division operation
Print (321% 123) # Modulus Operation
Print (321 / 123) # divisible operation
Print (321 * 123) # exponentiation
Assignment operator
The assignment operator should be the most common operator, and its function is to assign the value on the right to the variable on the left. The following example demonstrates the use of assignment operators and compound assignment operators.
"
Assignment operator and compound assignment operator
Version: 0.1
Author: Luo Hao
"
A = 10
B = 3
A + = b # is equivalent to: a = a + b
A * = a + 2 # is equivalent to: a = a * (a + 2)
Print (a) # calculate what will be output here
Compare operators and logical operators
The place where the comparison operator exists is also known as the relational operator, including =,!, and =. I believe there is nothing to explain. You can understand it at a glance. The only thing you need to remind is that the comparison is equal. Please note that there are two equal signs in this place. Because = is the assignment operator, as we just mentioned above, = = is the more equal comparison operator. The comparison operator produces a Boolean value, either True or False.
There are three logical operators, and, or, and not. And literally means "and", so the and operator joins two Boolean values. If both Boolean values are True, the result of the operation is that one of the Boolean values on the left and right side of the True; is False, and the final result is False. I believe you have thought that if the Boolean value on the left of and is False, no matter what the Boolean value on the right is, the end result is False, so the value on the right will be skipped (short circuit handling) when doing the operation, which means that if the left side of the and operator is False, the expression on the right will not be executed at all. Or literally means "or", so the or operator also joins two Boolean values, and if either of the two Boolean values is True, the end result is True. Of course, the or operator is also short-circuited, and if the Boolean on the left is True, the expression on the right will not be executed at all. The not operator is followed by a Boolean value, which is used to get the opposite of that Boolean value, that is, the following Boolean value is False if it is True, and the result is True if the subsequent Boolean value is False.
"
The use of comparison operators and logical operators
Version: 0.1
Author: Luo Hao
"
Flag0 = 1 = 1
Flag1 = 3 > 2
Flag2 = 2 < 1
Flag3 = flag1 and flag2
Flag4 = flag1 or flag2
Flag5 = not (1! = 2)
Print ('flag0 =', flag0) # flag0 = True
Print ('flag1 =', flag1) # flag1 = True
Print ('flag2 =', flag2) # flag2 = False
Print ('flag3 =', flag3) # flag3 = False
Print ('flag4 =', flag4) # flag4 = True
Print ('flag5 =', flag5) # flag5 = False
Note: the comparison operator takes precedence over the assignment operator, so flag0 = 1 = 1 do 1 = = 1 to generate a Boolean True, and then assign this value to the variable flag0. The print function can output multiple values, which can be separated by using, and the output contents are separated by spaces by default.
Example 1 of the operator: Fahrenheit temperature is converted to Celsius.
Hint: the conversion formula from Fahrenheit to Celsius is: $C = (F-32)\ div 1.8$.
"
Convert Fahrenheit to Celsius
Version: 0.1
Author: Luo Hao
"
F = float (input ('Please enter Fahrenheit:')
C = (f-32) / 1.8
Print ('.1f Fahrenheit =% .1F Celsius'% (f, c))
Note: when using the print function output, you can also format the string content. The string% 1.f in the above print function is a placeholder, which will later be replaced by a variable value of type float. Similarly, if there is% d in the string, it can be replaced later with a variable value of type int, and% s will be replaced by the value of the string. In addition to this way of formatting a string, you can also format a string in the following way, where {fbure.1f} and {cbure.1f} can be thought of as {f} and {c} first, indicating that the two placeholders are replaced by the values of the variables f and c, followed by .1f indicating that this is a floating point number, leaving 1 significant digit after the decimal point.
Print (Fahrenheit Fahrenheit = {CFV .1f} Celsius')
Example 2: enter the radius of the circle to calculate the perimeter and area.
"
Enter the radius to calculate the perimeter and area of the circle
Version: 0.1
Author: Luo Hao
"
Import math
Radius = float (input ('please enter the radius of the circle:')
Perimeter = 2 * math.pi * radius
Area = math.pi * radius * radius
Print ('perimeter:% .2f'% perimeter)
Print ('area:% .2f'% area)
Example 3: enter a year to determine whether it is a leap year.
"
Input year if it is a leap year, export True otherwise export False
Version: 0.1
Author: Luo Hao
"
Year = int (input ('Please enter year:')
Is_leap = year% 4 = = 0 and year% 100! = 0 or year% 400 = = 0
Print (is_leap)
Note: the comparison operator produces Boolean values, and the logical operators and and or combine these Boolean values to get a Boolean value, which outputs True in leap years and False in normal years.
At this point, the study on "how to use the operator of Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.