In the Python programming language, numbers are used in a wide range of fields, from mathematical operations to financial calculations. Python has a powerful and flexible syntax that allows us to work with different types of numbers. In this article, we’ll explore how to work with numbers in Python and use various number types, along with example code snippets.
Number Types
Python has three main types of numbers:
- int: Represents integers (whole numbers).
- float: Known as floating-point numbers (decimal numbers).
- complex: Represents complex numbers.
Integers (int)
Integers are numbers without a decimal part. In Python, integers are defined using the int data type.
# Integer examples
x = 10
y = -5
z = 0
print(x) # Output: 10
print(y) # Output: -5
print(z) # Output: 0
Floating-Point Numbers (floats)
Floating-point numbers are numbers with a decimal part. In Python, floating-point numbers are defined using the float data type.
pythonKodu kopyala# Floating-point number examples
a = 3.14
b = -2.5
c = 0.0
print(a) # Output: 3.14
print(b) # Output: -2.5
print(c) # Output: 0.0
Complex Numbers (complex)
Complex numbers consist of both real and imaginary parts. In Python, complex numbers are defined using the complex data type, where the imaginary part is represented by the letter j.
# Complex number examples
comp1 = 2 + 3j
comp2 = -1 - 1j
print(comp1) # Output: (2+3j)
print(comp2) # Output: (-1-1j)
Number Conversions
Converting between number types in Python is very straightforward. You can use the int(), float(), and complex() functions to perform conversions.
# Number conversions
x = 10
y = 3.14
z = 1 + 2j
# Conversion from int to float
a = float(x)
print(a) # Output: 10.0
# Conversion from float to int
b = int(y)
print(b) # Output: 3
# Conversion from int to complex
c = complex(x)
print(c) # Output: (10+0j)
# Conversion from float to complex
d = complex(y)
print(d) # Output: (3.14+0j)
Mathematical Operations
Python provides various operators to perform basic mathematical operations.
# Basic mathematical operations
x = 10
y = 3
# Addition
print(x + y) # Output: 13
# Subtraction
print(x - y) # Output: 7
# Multiplication
print(x * y) # Output: 30
# Division
print(x / y) # Output: 3.3333333333333335
# Integer division
print(x // y) # Output: 3
# Modulo (remainder)
print(x % y) # Output: 1
# Exponentiation
print(x ** y) # Output: 1000
The Math Module (math)
Python provides the math module for more advanced mathematical operations. This module includes functions for square roots, trigonometric functions, logarithms, and more.
import math
# Square root
print(math.sqrt(16)) # Output: 4.0
# Exponentiation
print(math.pow(2, 3)) # Output: 8.0
# Pi value
print(math.pi) # Output: 3.141592653589793
# Trigonometric functions
print(math.sin(math.pi / 2)) # Output: 1.0
print(math.cos(0)) # Output: 1.0
# Logarithm
print(math.log(100, 10)) # Output: 2.0
Random Numbers (random)
To generate random numbers, you can use Python’s random module. This module provides functions for generating random numbers in specific ranges, making random selections, and more.
import random
# Random number between 0 and 1
print(random.random())
# Random integer in a specific range
print(random.randint(1, 10))
# Random floating-point number in a specific range
print(random.uniform(1.0, 10.0))
# Random selection
choices = ['apple', 'banana', 'cherry']
print(random.choice(choices))
Conclusion
Working with numbers in Python is both flexible and powerful. Understanding different number types and operations allows you to develop more complex projects in Python. In this article, we’ve covered the basic number types and some operations you can perform with them. Thanks to Python’s rich libraries, mathematical operations and number manipulation can be done easily and efficiently.