📜  Python数字、类型转换和数学

📅  最后修改于: 2022-05-13 01:55:31.385000             🧑  作者: Mango

Python数字、类型转换和数学

先决条件: Python语言介绍

Python是一种通用的解释型、交互式、面向对象的高级编程语言。它是由 Guido van Rossum 创建的。它是一种开源编程语言。

Python中的数字类型

Python共有三种数字类型:

  • 整数
  • 漂浮
  • 复杂的

由于Python是一种松散类型的语言,我们不需要定义变量。数字类型的变量是在您为其分配值时创建的。

例子 :

Python3
# int
var1 = 3
  
# float
var2 = 3.14
  
# complex
var3 = 3j
  
# type() method return the 
# data type of the variable
print(type(var1))
print(type(var2))
print(type(var3))


Python3
# Python code to demonstrate Type conversion 
var1 = 3.14
  
# type conversion of float to int .
var2 = int(var1) 
  
print ("After converting float to integer : ", var2) 
print ("type : ",type(var2))
  
# type conversion of string to integer
var3 = "323"
var4 = int(var3) 
  
print ("After converting string to integer : ", var4) 
print ("type : ",type(var4))


Python3
a = 50
b = 30
  
# Addition of numbers 
add = a + b 
  
# Subtraction of numbers 
sub = a - b 
  
# Multiplication of number 
mul = a * b 
  
# Division of number 
div1 = a / b 
  
# Division of number 
div2 = a // b 
  
# Modulo of both number 
mod = a % b 
  
# Power 
p = a ** b 
  
# print results 
print(add) 
print(sub) 
print(mul) 
print(div1) 
print(div2) 
print(mod) 
print(p)


Python3
# Python code to demonstrate the working of 
# ceil() and floor() 
  
# importing "math" for mathematical operations 
import math 
  
a = 2.3
  
# returning the ceil of 2.3 
print ("The ceil of 2.3 is : ", end="") 
print (math.ceil(a)) 
  
# returning the floor of 2.3 
print ("The floor of 2.3 is : ", end="") 
print (math.floor(a))


Python3
# importing "math" for mathematical operations 
import math 
    
a = -10
b = 5.5
c = 15
d = 5
    
# returning the copysigned value. 
print ("The copysigned value of -10 and 5.5 is : ", end="") 
print (math.copysign(5.5, -10)) 
    
# returning the gcd of 15 and 5 
print ("The gcd of 5 and 15 is : ", end="") 
print (math.gcd(5,15))


Python3
# importing "random" for random operations 
import random 
    
# using random() to generate a random number 
# between 0 and 1 
print ("A random number between 0 and 1 is : ", end="") 
print (random.random())


输出:



类型转换

将一种数据类型(整数、字符串、浮点数等)的值转换为另一种数据类型称为类型转换。

例子:

蟒蛇3

# Python code to demonstrate Type conversion 
var1 = 3.14
  
# type conversion of float to int .
var2 = int(var1) 
  
print ("After converting float to integer : ", var2) 
print ("type : ",type(var2))
  
# type conversion of string to integer
var3 = "323"
var4 = int(var3) 
  
print ("After converting string to integer : ", var4) 
print ("type : ",type(var4))

输出:

After converting float to integer :  3
type :  
After converting string to integer :  323
type :  

的算术运算

您可以使用各种方法对数字进行加、减、乘和除。

operator

description

example

+ AdditionAdd values on either side of the operator.2 + 3 = 5
– SubtractionSubtracts right-hand value from left hand value.3 – 2 = 1
* MultiplicationMultiply values on either side of the operator2 * 3 = 6
/ DivisionDivides two operands3 / 2 = 1.5
% ModulusDivides two operands and returns remainder3 / 2 =1
** ExponentPerform power operation3 ** 2 = 9
//Floor Division3 // 2 = 1

例子:

蟒蛇3

a = 50
b = 30
  
# Addition of numbers 
add = a + b 
  
# Subtraction of numbers 
sub = a - b 
  
# Multiplication of number 
mul = a * b 
  
# Division of number 
div1 = a / b 
  
# Division of number 
div2 = a // b 
  
# Modulo of both number 
mod = a % b 
  
# Power 
p = a ** b 
  
# print results 
print(add) 
print(sub) 
print(mul) 
print(div1) 
print(div2) 
print(mod) 
print(p) 

输出:

80
20
1500
1.6666666666666667
1
20
931322574615478515625000000000000000000000000000000

数学函数:

数学模块有一组数学函数,其中一些在下面讨论。

Method

Description

math.sqrt()Returns the square root of a number
math.pow()Returns the value of x to the power of y
math.perm()Returns the number of ways to choose k items from n items with order and without repetition
math.gcd()Returns the greatest common divisor of two integers
math.floor()Rounds a number down to the nearest integer
math.ceil()Rounds a number up to the nearest integer
math.factorial()Returns the factorial of a number

示例 1:

蟒蛇3

# Python code to demonstrate the working of 
# ceil() and floor() 
  
# importing "math" for mathematical operations 
import math 
  
a = 2.3
  
# returning the ceil of 2.3 
print ("The ceil of 2.3 is : ", end="") 
print (math.ceil(a)) 
  
# returning the floor of 2.3 
print ("The floor of 2.3 is : ", end="") 
print (math.floor(a)) 

输出:

The ceil of 2.3 is : 3
The floor of 2.3 is : 2

示例 2:

蟒蛇3

# importing "math" for mathematical operations 
import math 
    
a = -10
b = 5.5
c = 15
d = 5
    
# returning the copysigned value. 
print ("The copysigned value of -10 and 5.5 is : ", end="") 
print (math.copysign(5.5, -10)) 
    
# returning the gcd of 15 and 5 
print ("The gcd of 5 and 15 is : ", end="") 
print (math.gcd(5,15)) 

输出:

The copysigned value of -10 and 5.5 is : -5.5
The gcd of 5 and 15 is : 5

要了解有关数学函数的更多信息,您可以参考 Geekforgeeks 上的这篇文章

随机数:

在Python中,我们有一组用于生成随机数的函数。这些功能用于游戏和彩票应用程序。

随机库中的方法:

  • 选择()
  • 随机数()
  • 随机的
  • 种子()

例子:

蟒蛇3

# importing "random" for random operations 
import random 
    
# using random() to generate a random number 
# between 0 and 1 
print ("A random number between 0 and 1 is : ", end="") 
print (random.random()) 

输出:

A random number between 0 and 1 is : 0.8548698466875713

geeksforgeeks 中有一篇关于随机数的详细文章。你可以参考这里的文章。