Numeric Types
int
)?
What are Integers (Integers are whole numbers with no decimal point. They can be either positive or negative. In Python, integers have unlimited precision, which means that they can be as large or as small as you need them to be, as long as you have enough memory available.
How to create Integers?
You can create an integer in Python by simply typing a number without a decimal point. For example:
x = 5
y = -10
In this case, x
is an integer with the value of 5 and y
is an integer with the value of -10.
float
)?
What are Floating-Point Numbers (Floating-point numbers, or floats, are numbers with a decimal point. They can also be positive or negative. In Python, floats are represented using the IEEE 754 floating-point standard, which allows for a limited precision of about 15 to 17 decimal digits.
How to create Floats?
You can create a float in Python by simply typing a number with a decimal point. For example:
x = 3.14
y = -2.5
In this case, x
is a float with the value of 3.14 and y
is a float with the value of -2.5.
Value Limits
The range of values that can be represented by a float is limited. The largest representable value is approximately 1.8 × 10^308
and the smallest representable value is approximately 5.0 × 10^-324
. However, due to the limited precision of floating-point numbers, you may encounter rounding errors when performing calculations with very large or very small values.
complex
)?
What are Complex Numbers (Complex numbers are numbers with a real part and an imaginary part. In Python, complex numbers are represented using the complex type. The real part and imaginary part are separated by a + or - sign.
How to create Complex Numbers?
You can create a complex number in Python by using the complex()
function or by typing the number in the form a + bj
, where a
is the real part and b
is the imaginary part. For example:
x = complex(3, 4)
y = 2 - 5j
In this case, x
is a complex number with a real part of 3 and an imaginary part of 4, and y
is a complex number with a real part of 2 and an imaginary part of -5.
Converting between Numeric Types
Python provides several functions for converting between numeric types. You can use the int()
, float()
, and complex()
functions to convert a value to the desired type. For example:
x = 10
y = float(x)
z = complex(x, 5)
In this case, y
is a float with the value of 10.0 and z
is a complex number with a real part of 10 and an imaginary part of 5.
Generating Random Values
Python provides a built-in random
module that can be used to generate random values for numerical types. To use the random
module, you first need to import it. You can then use the functions provided by the random
module to generate random values. Here are some examples:
import random
# Generate a random integer between 0 and 9
random_int = random.randint(0, 9)
# Generate a random float between 0 and 1
random_float = random.random()
# Generate a random complex number with real and imaginary parts between 0 and 1
random_complex = complex(random.random(), random.random())
In these examples, random_int
is a random integer between 0 and 9, random_float
is a random float between 0 and 1, and random_complex
is a random complex number with real and imaginary parts between 0 and 1.
Rounding
Python provides several functions for rounding numeric values. The round()
function can be used to round a floating-point number to the nearest integer or to a specified number of decimal places. The math module also provides several rounding functions that can be used for more specialized rounding operations. The round()
function takes one or two arguments. If called with one argument, it rounds the number to the nearest integer. If called with two arguments, it rounds the number to the specified number of decimal places. For example:
x = 3.14159
rounded = round(x, 2) # rounds x to 2 decimal places
In this case, rounded will have the value of 3.14.
When rounding a number that is exactly halfway between two possible results, Python uses the round-to-even strategy, also known as banker's rounding. For example:
x = 2.5
rounded = round(x) # rounds x to the nearest integer
In this case, rounded will have the value of 2. If x had been 1.5, rounded would have also been 2.
The math
module provides several rounding functions that can be used for more specialized rounding operations. The math.ceil()
function rounds a number up to the nearest integer, while the math.floor()
function rounds a number down to the nearest integer. The math.trunc()
function returns the integer part of a number (i.e., it truncates the decimal part). For example:
import math
x = 3.14159
ceil = math.ceil(x) # rounds x up to the nearest integer
floor = math.floor(x) # rounds x down to the nearest integer
trunc = math.trunc(x) # returns the integer part of x
In this case, ceil
will have the value of 4, floor
will have the value of 3, and trunc
will have the value of 3.
Minor Details and Gotchas:
Here are a few minor details and gotchas to keep in mind when working with Python's numeric types:
- When performing arithmetic operations on a
float
and anint
, Python will automatically convert theint
to afloat
. For example:
x = 5
y = 3.14
z = x + y # z will be a float with the value of 8.14
- In Python 2.x, the division operator / used integer division if both operands were integers, which could cause confusion and unexpected behavior. However, in Python 3.x, the behavior of the division operator was changed so that it always performs floating-point division, even if both operands are integers. To perform integer division in Python 3.x, you can use the double-slash operator //, which will return the quotient of the division rounded down to the nearest integer. For example:
x = 5
y = 2
z_float = x / y # z_float will be 2.5 for Python 3.x and 2 for Python 2.x
z_int = x // y # z_int will be 2 (integer division)
- When working with complex numbers, you can access the real and imaginary parts using the
real
andimag
attributes, respectively. For example:
z = complex(3, 4)
print(z.real) # prints 3.0
print(z.imag) # prints 4.0