In recent years, as the popularity of Python in the Internet industry has only increased, many people are learning Python, but if they want to truly learn it well, they must lay a good foundation. The main content of this article is about Python's arithmetic operators and comparison operators, with sample code explanations. Interested friends can learn about it.
Arithmetic operators
##Operators | Description | Example |
| Add - Add two objects | a b Output result 31 |
##-
Subtract - Get a negative number or subtract one number from another number |
a - b Output result-11 |
|
*
Multiplication - Multiply two numbers or return a string repeated several times |
a * b Output result 210 |
|
/
Division- b % a Output result 1 |
|
** |
Power - Return x raised to the y power
a**b is 10 raised to the 21st power |
|
// |
Take integer division - return the integer part of the quotient
9//2 Output result 4, 9.0//2.0 Output result 4.0 |
|
| Example code:
#!/usr/bin/python3
a = 21
b = 10
c = 0
c = a + b
print ("1 - c 的值为:", c)
c = a - b
print ("2 - c 的值为:", c)
c = a * b
print ("3 - c 的值为:", c)
c = a / b
print ("4 - c 的值为:", c)
c = a % b
print ("5 - c 的值为:", c)
# 修改变量 a 、b 、c
a = 2
b = 3
c = a**b
print ("6 - c 的值为:", c)
a = 10
b = 5
c = a//b
print ("7 - c 的值为:", c)
Copy after login
The above example output result: 1 - c 的值为: 31
2 - c 的值为: 11
3 - c 的值为: 210
4 - c 的值为: 2.1
5 - c 的值为: 1
6 - c 的值为: 8
7 - c 的值为: 2 Copy after login | | Comparison operator |
The following assumes that variable a is 10 and variable b is 20:
Operator
DescriptionExample
====
Equal - Compare whether the objects are equal(a == b) Return False. | | != |
Not equal to - Compares whether two objects are not equal(a != b) Returns true. | | > |
Greater than - Returns whether x is greater than y(a > b) Returns False. | | < |
Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. These are equivalent to the special variables True and False respectively. Note the capitalization of these variable names. (a < b) returns true. | | >= |
Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) Returns False. | | <= |
Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true. | | | The following examples demonstrate the operation of all comparison operators in Python:
#!/usr/bin/python3
a = 21
b = 10
c = 0
if ( a == b ):
print ("1 - a 等于 b")
else:
print ("1 - a 不等于 b")
if ( a != b ):
print ("2 - a 不等于 b")
else:
print ("2 - a 等于 b")
if ( a < b ):
print ("3 - a 小于 b")
else:
print ("3 - a 大于等于 b")
if ( a > b ):
print ("4 - a 大于 b")
else:
print ("4 - a 小于等于 b")
# 修改变量 a 和 b 的值
a = 5;
b = 20;
if ( a <= b ):
print ("5 - a 小于等于 b")
else:
print ("5 - a 大于 b")
if ( b >= a ):
print ("6 - b 大于等于 b")
else:
print ("6 - b 小于 b")
Copy after login
The output results of the above examples: 1 - a 不等于 b
2 - a 不等于 b
3 - a 大于等于 b
4 - a 大于 b
5 - a 小于等于 b
6 - b 大于等于 b Copy after login Related tutorials: | Python3 video tutorial |
|
The above is the detailed content of Arithmetic operators and comparison operators for basic learning in Python. For more information, please follow other related articles on the PHP Chinese website!