Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python Numbers tutorial, we'll cover the basics of working with numbers in Python, including numeric data types, arithmetic operations, and some useful numeric functions.
Python supports several numeric data types, including:
int: Integer numbers, which can be positive or negative whole numbers without a decimal point. They have unlimited precision and can grow to any size (limited only by the available memory).
Example:
a = 42 b = -7
float: Floating-point numbers, which can have a decimal point and support real numbers (both positive and negative). They have limited precision and can represent only a certain number of decimal places.
Example:
c = 3.14 d = -0.001
complex: Complex numbers, which consist of a real part and an imaginary part, represented as a + bj
, where a
and b
are real numbers and j
is the imaginary unit.
Example:
e = 2 + 3j f = -1.5 + 4j
Python supports various arithmetic operations, including:
Addition (+
): Adds two numbers.
result = 3 + 5 print(result) # Output: 8
Subtraction (-
): Subtracts the second number from the first.
result = 10 - 4 print(result) # Output: 6
Multiplication (*
): Multiplies two numbers.
result = 7 * 6 print(result) # Output: 42
Division (/
): Divides the first number by the second, returning a float.
result = 15 / 2 print(result) # Output: 7.5
Floor Division (//
): Divides the first number by the second, returning the largest integer not greater than the result.
result = 15 // 2 print(result) # Output: 7
Modulus (%
): Returns the remainder of the division of the first number by the second.
result = 15 % 4 print(result) # Output: 3
Exponentiation (**
): Raises the first number to the power of the second.
result = 2 ** 3 print(result) # Output: 8
Python provides several useful numeric functions, such as:
abs()
: Returns the absolute value of a number.
result = abs(-5) print(result) # Output: 5
round()
: Rounds a floating-point number to the nearest integer, or to a specified number of decimal places.
result = round(3.14159, 2) print(result) # Output: 3.14
Working with integers in Python:
x = 5 y = 3 sum_result = x + y product_result = x * y print(f"Sum: {sum_result}, Product: {product_result}")
Complex numbers in Python:
j
or J
.complex_number = 3 + 4j print(f"Real part: {complex_number.real}, Imaginary part: {complex_number.imag}")
Python number operations and arithmetic:
a = 10 b = 3 division_result = a / b modulus_result = a % b print(f"Division: {division_result}, Modulus: {modulus_result}")
Type conversion in Python for numbers:
int()
, float()
, and complex()
.x = 5 y = float(x) z = complex(x) print(f"Original: {x}, Float: {y}, Complex: {z}")
Python math module for advanced numeric operations:
math
module provides functions for advanced numeric operations like square root, logarithm, and trigonometry.import math sqrt_result = math.sqrt(25) log_result = math.log(10) sin_result = math.sin(math.radians(30)) print(f"Square Root: {sqrt_result}, Logarithm: {log_result}, Sine: {sin_result}")
Random number generation in Python:
random
module for generating random numbers and making random choices.import random random_number = random.randint(1, 10) random_choice = random.choice(['apple', 'banana', 'orange']) print(f"Random Number: {random_number}, Random Choice: {random_choice}")
Numeric comparisons and relational operators in Python:
<
, >
, <=
, >=
, ==
, !=
) for numeric comparisons.x = 5 y = 8 greater_than = x > y equal_to = x == y print(f"Greater Than: {greater_than}, Equal To: {equal_to}")
Handling numerical errors and exceptions in Python:
try: result = 10 / 0 except ZeroDivisionError as e: print(f"Error: {e}")