Home >Backend Development >Python Tutorial >How to calculate in python

How to calculate in python

(*-*)浩
(*-*)浩Original
2019-07-02 14:16:0722842browse

Python calculations mainly use Python operators. Let’s take a simple example: 4 5 = 9. In the example, 4 and 5 are called operands, and " " is called the operator.

How to calculate in python

The Python language supports a lot of operators. Here we only introduce the arithmetic operators used in calculations. (Recommended learning: Python video tutorial)

The following hypothetical variables: a=10, b=20:

##Add - Add two objects a b and output the result 30- Subtract- Get a negative number or One number subtracts another numbera - b Output result-10##*/%##**#Round and divide - Return the integer part of the quotient (rounded down)4 >>> -9//2Python Tutorial column to learn!
Operator
Description
Instance







Multiply- Multiply two numbers or return a string repeated several times

a * b The output result is 200


Divide - x divided by y

b / a Output result 2


Modulo - Returns the remainder of division

b % a Output result 0

Power - Returns the y power of

>>> 9//2

-5

# Example:

#!/usr/bin/python# -*- coding: UTF-8 -*-
 a = 21b = 10c = 0
 c = a + bprint "1 - c 的值为:", c
 c = a - bprint "2 - c 的值为:", c 
 c = a * bprint "3 - c 的值为:", c 
 c = a / bprint "4 - c 的值为:", c 
 c = a % bprint "5 - c 的值为:", c
 # 修改变量 a 、b 、ca = 2b = 3c = a**b print "6 - c 的值为:", c
 a = 10b = 5c = a//b print "7 - c 的值为:", c

The output result of the above example:

1 - c 的值为: 31
2 - c 的值为: 11
3 - c 的值为: 210
4 - c 的值为: 2
5 - c 的值为: 1
6 - c 的值为: 8
7 - c 的值为: 2
For more Python-related technical articles, please visit the

The above is the detailed content of How to calculate 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