Python Numbers

Welcome to The Coding College, your one-stop destination for mastering Python! In this tutorial, we’ll explore Python Numbers, their types, and how to work with them effectively. Whether you’re a beginner or brushing up on your Python knowledge, this guide will help you understand the fundamentals of numeric data in Python.

What are Numbers in Python?

In Python, numbers are one of the most commonly used data types. Python provides three main types of numeric data:

  1. Integers (int): Whole numbers, positive or negative, without decimal points.
  2. Floats (float): Numbers with decimal points.
  3. Complex Numbers (complex): Numbers with real and imaginary parts.
# Examples of Python Numbers  
x = 10        # Integer  
y = 3.14      # Float  
z = 2 + 3j    # Complex  

print(type(x))  # Output: <class 'int'>  
print(type(y))  # Output: <class 'float'>  
print(type(z))  # Output: <class 'complex'>  

Python Number Types

1. Integer (int)

Integers are whole numbers, and their size is unlimited in Python.

x = 100  
y = -50  

print(x, y)      # Output: 100 -50  
print(type(x))   # Output: <class 'int'>  

2. Float (float)

Floats represent numbers with decimal points. They are commonly used in calculations requiring precision.

pi = 3.14159  
e = 2.71828  

print(pi, e)       # Output: 3.14159 2.71828  
print(type(pi))    # Output: <class 'float'>  

3. Complex (complex)

Complex numbers have real and imaginary parts, denoted as a + bj.

z = 4 + 5j  
print(z)            # Output: (4+5j)  
print(z.real)       # Output: 4.0  
print(z.imag)       # Output: 5.0  
print(type(z))      # Output: <class 'complex'>  

Type Conversion Between Numbers

You can convert numbers between types using int(), float(), or complex().

# Convert float to int  
x = int(3.5)  
print(x)  # Output: 3  

# Convert int to float  
y = float(10)  
print(y)  # Output: 10.0  

# Convert int to complex  
z = complex(5)  
print(z)  # Output: (5+0j)  

Numeric Operations in Python

Python supports a variety of mathematical operations:

Basic Operations:

a = 10  
b = 3  

print(a + b)  # Addition: 13  
print(a - b)  # Subtraction: 7  
print(a * b)  # Multiplication: 30  
print(a / b)  # Division: 3.3333...  

Advanced Operations:

print(a % b)   # Modulus: 1  
print(a ** b)  # Exponentiation: 1000  
print(a // b)  # Floor division: 3  

Python’s math Module

The math module provides advanced mathematical functions:

import math  

# Square root  
print(math.sqrt(16))  # Output: 4.0  

# Power  
print(math.pow(2, 3))  # Output: 8.0  

# Rounding  
print(math.ceil(3.4))  # Output: 4  
print(math.floor(3.7))  # Output: 3  

Random Numbers in Python

Python’s random module is used to generate random numbers:

import random  

# Random integer  
print(random.randint(1, 10))  # Random number between 1 and 10  

# Random float  
print(random.random())  # Random float between 0 and 1  

# Random choice from a list  
my_list = [10, 20, 30, 40]  
print(random.choice(my_list))  # Random item from the list  

Practical Examples

Example 1: Calculate the Area of a Circle

import math  

radius = float(input("Enter the radius of the circle: "))  
area = math.pi * radius ** 2  
print(f"The area of the circle is: {area}")  

Example 2: Random Number Guessing Game

import random  

target = random.randint(1, 10)  
guess = int(input("Guess a number between 1 and 10: "))  

if guess == target:  
    print("You guessed it right!")  
else:  
    print(f"Wrong! The correct number was {target}.")  

Learn Python Numbers at The Coding College

Understanding Python numbers is critical for solving real-world problems in data science, engineering, and beyond. At The Coding College, you’ll find:

  • Detailed Python Tutorials
  • Hands-On Practice Examples
  • Expert Tips to Build Your Programming Skills

Conclusion

Python’s support for various number types and operations makes it a powerful tool for developers. By understanding integers, floats, and complex numbers, you’ll be able to handle a wide range of numeric computations in your projects.

Leave a Comment