1) What is complex explain with example?
Ans- In Python, a complex number is a numeric data type used to represent numbers
in the form \( a + bj \), where \( a \) and \( b \) are real numbers, and \( j \)
is the imaginary unit, which satisfies the equation \( j^2 = -1 \). Complex numbers
are commonly used in mathematics, engineering, and physics to represent quantities
involving both real and imaginary parts.
Here's a simple explanation with an example in Python:
```python
# Define a complex number
z = 3 + 4j
# Print the complex number
print("Complex number:", z)
# Accessing real and imaginary parts
print("Real part:", z.real) # Output: 3.0
print("Imaginary part:", z.imag) # Output: 4.0
# Addition of complex numbers
w = 1 - 2j
sum = z + w
print("Sum of complex numbers:", sum) # Output: (4+2j)
# Multiplication of complex numbers
product = z * w
print("Product of complex numbers:", product) # Output: (11+2j)
```
In this example:
- `z` is a complex number with real part \( a = 3 \) and imaginary part \( b = 4
\).
- The real part of `z` is accessed using `z.real`, which returns `3.0`.
- The imaginary part of `z` is accessed using `z.imag`, which returns `4.0`.
- Addition and multiplication of complex numbers are demonstrated using the `+` and
`*` operators respectively.
2) Define Oct(),Hex(),Bin()?
Ans- In Python, `oct()`, `hex()`, and `bin()` are built-in functions used to
convert numbers from decimal to octal, hexadecimal, and binary representations
respectively.
Here's how each function works:
1. `oct()`: This function converts a decimal number to its octal representation. It
takes a single argument, which is the decimal number to be converted, and returns a
string representing the octal value.
Example:
```python
decimal_number = 25
octal_representation = oct(decimal_number)
print("Octal representation of", decimal_number, "is", octal_representation) #
Output: 0o31
```
2. `hex()`: This function converts a decimal number to its hexadecimal
representation. Similar to `oct()`, it takes a single argument, the decimal number,
and returns a string representing the hexadecimal value.
Example:
```python
decimal_number = 25
hexadecimal_representation = hex(decimal_number)
print("Hexadecimal representation of", decimal_number, "is",
hexadecimal_representation) # Output: 0x19
```
3. `bin()`: This function converts a decimal number to its binary representation.
Like the previous functions, it takes a single argument, the decimal number, and
returns a string representing the binary value.
Example:
```python
decimal_number = 25
binary_representation = bin(decimal_number)
print("Binary representation of", decimal_number, "is", binary_representation) #
Output: 0b11001
```