Numeric Types in Python:
- integer
- Floating-Point Numbers
- Complex Numbers
- Numeric Type Conversion
x = 10 # Integer
y = 3.5 # Floating-Point Number
z = 2 + 3j # Complex Number
Integer
# Integer Example
x = 10
y = 5
addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
floor_division = x // y
modulo = x % y
exponentiation = x ** y
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Floor Division:", floor_division)
print("Modulo:", modulo)
print("Exponentiation:", exponentiation)
Floating Point Number Example
# Floating-Point Number Example
x = 10.5
y = 2.5
addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
exponentiation = x ** y
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Exponentiation:", exponentiation)
Complex Number Example
# Complex Number Example
x = 2 + 3j
y = 1 - 2j
addition = x + y
subtraction = x - y
multiplication = x * y
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
Numeric Type Conversion
Converting to Integer:
# Numeric Type Conversion - Converting to Integer
y = 3.5
z = 2 + 3j
int_x = int(y)
int_y = int(z.real)
print("Integer Conversion:")
print("int_x:", int_x)
print("int_y:", int_y)
Converting to Floating-Point Number:
# Numeric Type Conversion - Converting to Floating-Point Number
x = 10
z = 2 + 3j
float_x = float(x)
float_y = float(z.real)
print("Floating-Point Conversion:")
print("float_x:", float_x)
print("float_y:", float_y)
Converting to Complex Number:
# Numeric Type Conversion - Converting to Complex Number
x = 10
y = 3.5
complex_x = complex(x)
complex_y = complex(y, 4)
print("Complex Number Conversion:")
print("complex_x:", complex_x)
print("complex_y:", complex_y)
Feel free to modify the values or experiment with different conversion scenarios to explore further.
Arithmetic Operations
- Addition, Subtraction, Multiplication, and Division
- Floor Division and Modulo
- Exponentiation
- Operator Precedence and Parentheses
# Arithmetic Operations
addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
floor_division = x // y
modulo = x % y
exponentiation = x ** y
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Floor Division:", floor_division)
print("Modulo:", modulo)
print("Exponentiation:", exponentiation)
Addition
# Addition
a = 5
b = 3
addition = a + b
print("Addition:", addition)
Subtraction
# Subtraction
a = 5
b = 3
subtraction = a - b
print("Subtraction:", subtraction)
Multiplication
# Multiplication
a = 5
b = 3
multiplication = a * b
print("Multiplication:", multiplication)
Division
# Division
a = 5
b = 3
division = a / b
print("Division:", division)
Floor Division
# Floor Division
a = 17
b = 5
floor_division = a // b
print("Floor Division:", floor_division)
Modulo
# Modulo
a = 17
b = 5
modulo = a % b
print("Modulo:", modulo)
Exponentiation
# Exponentiation
a = 2
b = 3
exponentiation = a ** b
print("Exponentiation:", exponentiation)
Operator Precedence
# Operator Precedence
a = 5
b = 3
c = 2
result = a + b * c
print("Result without parentheses:", result)
Parentheses
# Parentheses
a = 5
b = 3
c = 2
result_with_parentheses = (a + b) * c
print("Result with parentheses:", result_with_parentheses)
Built-in Functions for Numbers:
- abs(), round(), and pow()
- min() and max()
- sum() and len()
- Converting between Numeric Types: int(), float(), and complex()
- Generating Sequences: range()
# Built-in Functions for Numbers
absolute_value = abs(-10)
rounded_value = round(3.14159, 2)
minimum_value = min(5, 7, 2, 9)
maximum_value = max(5, 7, 2, 9)
sum_of_values = sum([1, 2, 3, 4, 5])
length_of_list = len([1, 2, 3, 4, 5])
converted_int = int(3.5)
converted_float = float(10)
converted_complex = complex(2, 3)
print("Absolute Value:", absolute_value)
print("Rounded Value:", rounded_value)
print("Minimum Value:", minimum_value)
print("Maximum Value:", maximum_value)
print("Sum of Values:", sum_of_values)
print("Length of List:", length_of_list)
print("Converted Integer:", converted_int)
print("Converted Float:", converted_float)
print("Converted Complex:", converted_complex)
abs()
# abs()
absolute_value = abs(-10)
print("Absolute Value:", absolute_value)
round()
# round()
rounded_value = round(3.14159, 2)
print("Rounded Value:", rounded_value)
pow()
# pow()
exponentiation = pow(2, 3)
print("Exponentiation:", exponentiation)
min() and max()
# min() and max()
minimum_value = min(5, 7, 2, 9)
maximum_value = max(5, 7, 2, 9)
print("Minimum Value:", minimum_value)
print("Maximum Value:", maximum_value)
sum() and len()
# sum() and len()
sum_of_values = sum([1, 2, 3, 4, 5])
length_of_list = len([1, 2, 3, 4, 5])
print("Sum of Values:", sum_of_values)
print("Length of List:", length_of_list)
Type Conversion Functions
# Type Conversion Functions
converted_int = int(3.5)
converted_float = float(10)
converted_complex = complex(2, 3)
print("Converted Integer:", converted_int)
print("Converted Float:", converted_float)
print("Converted Complex:", converted_complex)
range()
sequence = range(1, 10, 2)
print("Generated Sequence:", list(sequence))
By separating the code into these sections, you can easily identify and understand each function or concept being demonstrated. Feel free to modify the input values or experiment with different arguments within each section to explore the functionalities further.