Numbers
Native Data Types
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, both positive and negative, without any decimal point. Examples of integers are `-10`, `0`, `42`. In Python, integers can be of arbitrary size, meaning they can be very large or very small.
Example:
```python
a = 10
b = -5
c = 0
```
2. Floating-Point Numbers (`float`):
Floating-point numbers, or floats, represent numbers with a decimal point or in exponential notation. Examples include `3.14`, `-0.001`, `2.0e3` (which is equivalent to 2000.0). Floating-point numbers can represent a wide range of values, including fractions and very large or small numbers.
Example:
```python
pi = 3.14159
temperature = -12.34
```
3. Complex Numbers (`complex`):
Complex numbers are represented by a real part and an imaginary part, both of which are floating-point numbers. The imaginary part is denoted by `j` or `J`. Example: `3 + 2j`.
Example:
```python
z = 2 + 3j
```
Python provides various arithmetic operations for numbers, including addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), modulus (`%`), exponentiation (`**`), and floor division (`//`).
```python
a = 10
b = 5
# Arithmetic operations
sum_ab = a + b # 15
difference_ab = a - b # 5
product_ab = a * b # 50
quotient_ab = a / b # 2.0
remainder_ab = a % b # 0
power_ab = a ** b # 100000
floor_div_ab = a // b # 2
```
Understanding numbers and how to use them in Python is essential for performing calculations, solving problems, and creating various applications.
Comments
Post a Comment