Numbers in Python
Python provides support for working with different types of numbers, including integers, floating-point numbers, and complex numbers. In this tutorial, we will discuss the different number types in Python and how to use them in your code.
Integer Numbers
Integers, or ints , are whole numbers without a decimal point. In Python, you can define an integer by assigning a value to a variable:
# Example of defining an integer
x = 5
print(x) # Output: 5
Python also supports binary, octal, and hexadecimal integer literals. For example, to define a binary integer, you can use the prefix
0b
, followed by a string of binary digits:
# Example of binary integer literal
x = 0b1010
print(x) # Output: 10
Octal literals use the prefix
0o
, followed by a string of octal digits:
# Example of octal integer literal
x = 0o123
print(x) # Output: 83
Hexadecimal literals use the prefix
0x
, followed by a string of hexadecimal digits:
# Example of hexadecimal integer literal
x = 0x1A
print(x) # Output: 26
Floating-Point Numbers
Floating-point numbers, or floats , are numbers with a decimal point. In Python, you can define a float by assigning a value to a variable:
# Example of defining a float
x = 3.14
print(x) # Output: 3.14
Python also supports scientific notation for representing very large or very small numbers:
# Example of scientific notation
x = 3e8
print(x) # Output: 300000000.0
Complex Numbers
Complex numbers are numbers with both a real part and an imaginary part. In Python, you can define a complex number by using the
j
or
J
suffix to indicate the imaginary part:
# Example of defining a complex number
x = 3 + 4j
print(x) # Output: (3+4j)
You can access the real and imaginary parts of a complex number usingthe
real
and
imag
attributes:
# Example of accessing the real and imaginary parts of a complex number
x = 3 + 4j
print(x.real) # Output: 3.0
print(x.imag) # Output: 4.0
Type Conversion
You can convert a number from one type to another using type conversion functions. For example, you can convert a float to an integer using the
int()
function:
# Example of type conversion from float to int
x = 3.14
y = int(x)
print(y) # Output: 3
You can also convert an integer to a float using the
float()
function:
# Example of type conversion from int to float
x = 5
y = float(x)
print(y) # Output: 5.0
Similarly, you can convert a string to a number using the
int()
or
float()
functions:
# Example of type conversion from string to number
x = "123"
y = int(x)
z = float(x)
print(y) # Output: 123
print(z) # Output: 123.0
However, if the string contains non-numeric characters, the conversion will fail:
# Example of type conversion from string to number with non-numeric characters
x = "123abc"
y = int(x) # ValueError: invalid literal for int() with base 10: '123abc'
z = float(x) # ValueError: could not convert string to float: '123abc'
That's a brief introduction to numbers in Python. By understanding the different number types and how to convert between them, you can write more flexible and powerful code.