Home >Backend Development >Python Tutorial >Arithmetic operators and comparison operators for basic learning in Python

Arithmetic operators and comparison operators for basic learning in Python

little bottle
little bottleOriginal
2019-04-27 14:26:492787browse

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

##OperatorsDescriptionExampleAdd - Add two objectsa b Output result 31##- */Power - Return x raised to the y powerTake integer division - return the integer part of the quotientExample 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)
The above example output result: Comparison operatorThe following assumes that variable a is 10 and variable b is 20:
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 **
a**b is 10 raised to the 21st power //
9//2 Output result 4, 9.0//2.0 Output result 4.0
1 - c 的值为: 31
2 - c 的值为: 11
3 - c 的值为: 210
4 - c 的值为: 2.1
5 - c 的值为: 1
6 - c 的值为: 8
7 - c 的值为: 2

Operator

DescriptionExample

====Equal - Compare whether the objects are equalNot equal to - Compares whether two objects are not equalGreater than - Returns whether x is greater than yGreater than or equal to - Returns whether x is greater than or equal to y. Less than or equal to - Returns whether x is less than or equal to y. 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")
The output results of the above examples: Related tutorials:
(a == b) Return False. !=
(a != b) Returns true. >
(a > b) Returns False. 06a68011356bf656d4dc21ead41287f6=
(a >= b) Returns False. <=
(a <= b) returns true.
1 - a 不等于 b
2 - a 不等于 b
3 - a 大于等于 b
4 - a 大于 b
5 - a 小于等于 b
6 - b 大于等于 b
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn