In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 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 "how to use Python operators". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Python supports many operators, so let's start with a table listing them, and then select some of the operators we'll use in a minute to explain them.
Note: The above table actually lists the operators from top to bottom according to their precedence. Priority is the order in which an operation should be executed first if multiple operators appear in an expression. In practical development, if you are confused about the precedence of operators, you can use parentheses to ensure the order of execution of operations.
arithmetic operators
Python has a wealth of arithmetic operators, including division, modulo (remainder), and exponentiation, in addition to the most familiar addition, subtraction, multiplication, and division. The following example demonstrates the use of arithmetic operators.
"""
arithmetic operators
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) #modulo operation
print(321 // 123) #divisible operation
print(321 ** 123) #exponentiation assignment operator
The assignment operator is probably the most common operator, and its role 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 operators and compound assignment operators
Version: 0.1
Author: Luo Hao
"""
a = 10
b = 3
a += b #Equivalent to: a = a + b
a *= a + 2 #Equivalent to: a = a * (a + 2)
print(a) #Calculate what comparison operators and logical operators will be output here
Comparison operators are also called relational operators in some places, including ==,!=,=, I believe there is nothing easy to explain, you can understand at a glance, need to remind is the use of equality is ==, please note that there are two equal signs, because = is the assignment operator, we just talked about above,== is the operator of equality; inequality is used!=, This is different from the mathematical inequality sign, which was used in Python 2 to express inequality relations, so you can know it. Comparison operators produce Boolean values that are either True or False.
There are three logical operators: and, or and not. And literally means "and," so the and operator concatenates two Boolean values. If both Boolean values are True, the result of the operation is True; if one of the left and right Boolean values is False, the final result of the operation is False. As you may have thought, if the Boolean on the left of and is False, the result will always be False regardless of the Boolean on the right, so the value on the right will be skipped (short-circuiting), which means that if the Boolean on the left of and is False, the expression on the right will not execute at all. Or literally means "or," so the or operator also concatenates two Boolean values, and if either of them is True, the final result is True. Of course, the or operator also has a short-circuit function. If the Boolean value on its 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 the Boolean value, that is, if the Boolean value after not is True, the result of the operation is False; and if the Boolean value after not is False, the result of the operation is True.
"""
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 has higher priority than the assignment operator, so flag0 = 1 == 1 first does 1 == 1 to generate the Boolean value True, and then assigns this value to the variable flag0. The print function can output multiple values, which can be separated by, and the output contents are separated by spaces by default.
Example of operators Example 1: Fahrenheit to Celsius conversion.
Tip: Fahrenheit to Celsius conversion formula is:
"""
Convert Fahrenheit to Celsius
Version: 0.1
Author: Luo Hao
"""
f = float(input ('Please enter Fahrenheit: '))
c = (f - 32) / 1.8
print ('%.1f F = %.1f C ' % (f, c))
Note: When using the print function output, you can also format the string content. The string %.1f in the print function above is a placeholder, which will be replaced later by a float variable value. Similarly, if there is a %d in the string, it can be replaced by a variable value of type int, and %s is replaced by the value of the string. In addition to formatting strings in this way, strings can also be formatted in the following way, where {f:.1f} and {c:.1f} can be regarded as {f} and {c}, indicating that the output will replace these two placeholders with the values of variables f and c, followed by:.1f indicating that this is a floating point number, leaving one significant digit after the decimal point.
print(f'{f:.1f} Fahrenheit = {c:.1f} Centigrade') Example 2: Enter the radius of a circle to calculate the circumference and area. """
Enter radius to calculate circumference and area of circle
Version: 0.1
Author: Luo Hao
"""
radius = float(input ('Please enter radius of circle:'))
perimeter = 2 * 3.1416 * radius
area = 3.1416 * radius * radius
print ('perimeter: %.2f' % centimeter)
print ('Area: %.2f' % area) Example 3: Enter the year to determine whether it is a leap year. """
Input year Output True if leap year Output False otherwise
Version: 0.1
Author: Luo Hao
"""
year = int(input ('please input year:'))
is_leap = year % 4 == 0 and year % 100 != 0 or year % 400 == 0
print(is_leap)
Description: Comparison operators produce Boolean values, and logical operators and and or combine these Boolean values to obtain a Boolean value, outputting True in leap years and False in ordinary years.
"How to use Python operators" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.