Python Numbers
In Python, numbers are a fundamental data type used to represent numerical values. There are three main types of numbers in Python: integers, floating-point numbers, and complex numbers.
1. Integers (int
):
Integers represent whole numbers without any decimal points. In Python, integer values can be positive or negative.
x = 5
y = -10
2. Floating-Point Numbers (float
):
Floating-point numbers represent real numbers with decimal points or numbers expressed in scientific notation.
pi = 3.14159
radius = 2.5
3. Complex Numbers (complex
):
Complex numbers have both a real part and an imaginary part. They are written in the form a + bj
, where a
is the real part, b
is the imaginary part, and j
is the imaginary unit.
z = 2 + 3j
Arithmetic Operations:
You can perform various arithmetic operations on numbers in Python, such as addition, subtraction, multiplication, division, and more.
a = 10
b = 3
sum_result = a + b
difference_result = a - b
product_result = a * b
quotient_result = a / b
remainder_result = a % b
power_result = a ** b
Type Conversion:
You can convert between different number types using type conversion functions like int()
, float()
, and complex()
.
x = 5.7
y = int(x) # Converts x to an integer
z = float("3.14") # Converts a string to a float
Mathematical Functions:
Python provides built-in functions and the math
module for more advanced mathematical operations.
import math
sqrt_result = math.sqrt(25)
sin_result = math.sin(math.radians(30)) # Calculates sine of 30 degrees
These are some basic concepts related to numbers in Python. Understanding how to work with numbers and perform arithmetic operations is essential for various mathematical and scientific applications, as well as general programming tasks.