📅  最后修改于: 2023-12-03 15:31:25.252000             🧑  作者: Mango
In Python, int
and float
are two fundamental data types used to represent numbers.
Integers are whole numbers, such as 1
, 13
, -7
, etc. In Python, the int
data type can represent integers of any size.
For example, we can assign an integer value to a variable like this:
x = 42
We can also perform arithmetic operations on integers:
a = 5
b = 3
c = a + b # Addition
d = a - b # Subtraction
e = a * b # Multiplication
f = a / b # Division
g = a // b # Floor division (returns the integer quotient)
h = a % b # Modulus (returns the remainder)
i = a ** b # Exponentiation
Floating point numbers, or simply floats, are numbers with decimal points or exponential notation, such as 3.14
, 2.0e-2
, 1.23e6
, etc.
We can assign a float value to a variable like this:
y = 3.14
We can perform arithmetic operations on floats as well, just like with integers:
a = 1.5
b = 0.25
c = a + b # Addition
d = a - b # Subtraction
e = a * b # Multiplication
f = a / b # Division
Sometimes we may need to convert a float to an integer, or vice versa. We can use type casting to achieve this:
x = int(3.14) # x is now equal to 3
y = float(42) # y is now equal to 42.0
In summary, int
represents whole numbers and float
represents decimal numbers or exponential notation. We can perform arithmetic operations on these types of data, and convert between them using type casting.