0% found this document useful (0 votes)
3 views3 pages

1 A Number

Python has three main numerical data types: Integer (int), Floating-point (float), and Complex (complex), each supporting various arithmetic operations. Integers represent whole numbers, floats represent decimal numbers, and complex numbers include real and imaginary parts. The document also covers type conversion between these number types and provides examples of how to use them in Python.

Uploaded by

kalpanapriyam213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

1 A Number

Python has three main numerical data types: Integer (int), Floating-point (float), and Complex (complex), each supporting various arithmetic operations. Integers represent whole numbers, floats represent decimal numbers, and complex numbers include real and imaginary parts. The document also covers type conversion between these number types and provides examples of how to use them in Python.

Uploaded by

kalpanapriyam213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Number Data Types

Python has three main numerical data types:

1. Integer (int) – Whole numbers (positive, negative, or zero).


2. Floating-point (float) – Decimal numbers.
3. Complex (complex) – Numbers with a real and imaginary part.

Python provides built-in support for these number types, and we can perform arithmetic operations on them
easily.

1. Integer (int)
 Represents whole numbers (positive, negative, or zero).
 No decimal points allowed.
 Can be of unlimited length (no size limit like in other languages).

🔹 Example:

python
CopyEdit
# Integer examples
a = 10 # Positive integer
b = -25 # Negative integer
c = 0 # Zero

print(a, type(a)) # Output: 10 <class 'int'>


print(b, type(b)) # Output: -25 <class 'int'>
print(c, type(c)) # Output: 0 <class 'int'>

2. Floating-Point (float)
 Represents decimal numbers (positive or negative).
 Can also be written using scientific notation (e or E for exponent).

🔹 Example:

python
CopyEdit
# Floating-point examples
x = 10.5 # Positive float
y = -3.14 # Negative float
z = 2.5e3 # Scientific notation (2.5 × 10³ = 2500.0)

print(x, type(x)) # Output: 10.5 <class 'float'>


print(y, type(y)) # Output: -3.14 <class 'float'>
print(z, type(z)) # Output: 2500.0 <class 'float'>
3. Complex (complex)
 Used for mathematical operations involving imaginary numbers.
 Defined as real + imaginary*j, where j represents √-1.
 The real and imaginary parts are stored as floats.

🔹 Example:

python
CopyEdit
# Complex number examples
c1 = 3 + 4j # 3 is real, 4j is imaginary
c2 = -2.5 + 6j # -2.5 is real, 6j is imaginary

print(c1, type(c1)) # Output: (3+4j) <class 'complex'>


print(c2, type(c2)) # Output: (-2.5+6j) <class 'complex'>

# Accessing real and imaginary parts


print("Real Part:", c1.real) # Output: Real Part: 3.0
print("Imaginary Part:", c1.imag) # Output: Imaginary Part: 4.0

4. Performing Arithmetic Operations on Numbers


Python allows performing various arithmetic operations on integers, floats, and complex numbers.

🔹 Example:

python
CopyEdit
# Arithmetic operations
a = 10
b = 3

print("Addition:", a + b) # Output: 13
print("Subtraction:", a - b) # Output: 7
print("Multiplication:", a * b) # Output: 30
print("Division:", a / b) # Output: 3.333...
print("Floor Division:", a // b) # Output: 3 (removes decimal part)
print("Modulus:", a % b) # Output: 1 (remainder)
print("Exponentiation:", a ** b) # Output: 1000 (10³)

5. Type Conversion Between Numbers


You can convert one number type to another using int(), float(), and complex().

🔹 Example:
python
CopyEdit
# Converting between types
num1 = 10 # Integer
num2 = 3.5 # Float
num3 = 4 + 2j # Complex

# Convert int to float


print(float(num1)) # Output: 10.0

# Convert float to int


print(int(num2)) # Output: 3 (removes decimal part)

# Convert int/float to complex


print(complex(num1)) # Output: (10+0j)
print(complex(num2)) # Output: (3.5+0j)

6. Checking Data Type


Use the type() function to check the data type.

🔹 Example:

python
CopyEdit
a = 5
b = 2.5
c = 1 + 2j

print(type(a)) # Output: <class 'int'>


print(type(b)) # Output: <class 'float'>
print(type(c)) # Output: <class 'complex'>

7. Practical Program: Calculator for Numeric Data Types


Here’s a simple Python calculator that takes two numbers as input and performs different operations.

python
CopyEdit
# Simple Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("Addition:", num1 + num2)


print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
print("Exponentiation:", num1 ** num2)

You might also like