Python Numeric Types
Python provides several numeric types to work with numbers. The commonly used numeric types are integers (`int`), floating-point numbers (`float`), and complex numbers (`complex`).
1. Integers (`int`):
- Integers are whole numbers without a fractional component.
- Example:
```python
x = 10
y = -5
```
Right Method:
- You can perform various mathematical operations on integers, such as addition, subtraction, multiplication, and division.
- Example:
```python
a = 5 + 3 # Addition: a = 8
b = 10 - 4 # Subtraction: b = 6
c = 2 * 6 # Multiplication: c = 12
d = 20 / 5 # Division: d = 4.0 (float division)
e = 20 // 5 # Integer Division: e = 4
f = 7 % 3 # Modulo (Remainder): f = 1
```
Wrong Method:
- Trying to perform operations that result in a type mismatch or undefined behavior is considered incorrect.
- Example:
```python
x = 10 / 3 # Incorrect division, should use float division
y = 2 + "3" # Incorrect addition, mixing integer and string
```
2. Floating-Point Numbers (`float`):
- Floating-point numbers represent decimal values with fractional parts.
- Example:
```python
a = 3.14
b = -2.5
```
Right Method:
- Similar to integers, you can perform mathematical operations on floating-point numbers.
- Example:
```python
c = 2.5 + 1.2 # Addition: c = 3.7
d = 5.3 - 2.7 # Subtraction: d = 2.6
e = 2.5 * 3.5 # Multiplication: e = 8.75
f = 10.0 / 3.0 # Division: f = 3.3333333333333335
```
Wrong Method:
- Floating-point arithmetic is subject to precision limitations, which can lead to small errors in certain operations.
- Example:
```python
x = 0.1 + 0.2 # Incorrect addition, result is not exactly 0.3
```
3. Complex Numbers (`complex`):
- Complex numbers consist of a real part and an imaginary part.
- Example:
```python
z = 3 + 2j
w = -1j
```
Right Method:
- Complex numbers support arithmetic operations like addition, subtraction, multiplication, and division.
- Example:
```python
u = (2 + 3j) + (1 - 2j) # Addition: u = (3 + 1j)
v = (4 + 5j) * (3 - 2j) # Multiplication: v = (22 + 7j)
```
Wrong Method:
- Performing unsupported operations on complex numbers will result in errors.
- Example:
```python
x = (2 + 3j) / 0 # Incorrect division by zero
y = (3 + 4j) + 5 # Incorrect addition, mixing complex and integer
```
Type Conversion in Python
Type conversion, also known as type casting, is the process of converting one data type to another in Python. Python provides built-in functions to facilitate type conversion.
1. Implicit Type Conversion:
- Implicit type conversion, also called automatic type conversion, occurs automatically when Python converts one data type to another without any explicit instructions from the programmer.
- Implicit type conversion takes place in situations where the target data type can accommodate the values of the source data type without any loss of information.
- Example:
```python
a = 10 # int
b = 3.14 # float
c= 5j #complex
d = a + b # a is implicitly converted to float
print(d) # Output: 13.14
```
2. Explicit Type Conversion:
- Explicit type conversion, also known as type casting, is done explicitly by the programmer using built-in functions.
- Explicit type conversion is necessary when the target data type cannot directly accommodate the values of the source data type.
- Python provides several built-in functions for explicit type conversion, including `int()`, `float()`, `str()`, `list()`, `tuple()`, and `dict()`, among others.
- Example:
```python
x = 10.5 # float
# Explicitly convert x to an integer
y = int(x)
print(y) # Output: 10
```
Right Method:
- Use the appropriate type conversion function based on the desired target data type.
- Ensure that the values being converted can be accommodated without loss of information or precision.
- Example:
```python
x = "15" # string
# Explicitly convert x to an integer
y = int(x)
print(y) # Output: 15
```
Wrong Method:
- Performing type conversion that results in loss of information or unsupported conversions can lead to incorrect results or errors.
- Example:
```python
x = "Hello" # string
# Incorrect conversion to integer
y = int(x) # Raises ValueError: invalid literal for int() with base 10: 'Hello'
```
<< Previous Next >>