Numbers in Python
Numbers are a very important concept in programming.
Almost every application uses numbers.
Here, we will learn about numbers in Python.
Numbers in Python
Python supports three types of numbers.int (Integer)
In Python, int represents integer numbers.
These are numbers without a decimal point.
They can be both positive and negative.
In Python, int is a built-in data type.
Examples include 10, -5, and 0.
Creating int variables
a = 10
b = -5
c = 0
Here
Variables a, b, and c store integer values.
Note: In Python, no special syntax is required to create an int value. You can create an integer simply by writing a number.
Variables a, b, and c store integer values.
Note: In Python, no special syntax is required to create an int value. You can create an integer simply by writing a number.
Type checking in Python
The
type() function is used to check the type of a variable.
Example:
a = 10
print(type(a)) # Output: <class 'int'>
Storing binary numbers in Python
In Python, binary numbers can also be stored as integers.
Binary numbers start with
0b or 0B.
Example:
a = 0b1010 # Equal to decimal 10
print(a) # Output: 10
Storing octal numbers in Python
Octal numbers can also be stored as integers in Python.
Octal numbers start with
0o or 0O.
Example:
a = 0o12 # Equal to decimal 10
print(a) # Output: 10
Storing hexadecimal numbers in Python
Hexadecimal numbers can also be stored as integers in Python.
Hexadecimal numbers start with
0x or 0X.
Example:
a = 0xA # Equal to decimal 10
print(a) # Output: 10
Large integers in Python
Python allows you to store very large integers.
There is no fixed limit for int values in Python.
Example:
a = 1234567890123456789012345678901234567890
print(a)
Type conversion to int
You can convert other data types to int using the
int() function.
Example:
a = int(10.5) # float to int conversion
print(a) # Output: 10
b = int("20") # string to int conversion
print(b) # Output: 20
float (Floating Point Number)
In Python, float represents decimal numbers.
These numbers contain a decimal point.
They can be positive or negative.
Float is a built-in data type in Python.
Examples include 10.5, -3.14, and 0.0.
Creating float variables
a = 10.5
b = -3.14
c = 0.0
Here
Variables a, b, and c store float values.
Numbers with a decimal point are treated as float values.
Numbers without a decimal point are treated as int values.
Example: 10 is an int, but 10.0 is a float.
You can also use scientific notation (e.g., 1.5e2).
Example:
Variables a, b, and c store float values.
Numbers with a decimal point are treated as float values.
Numbers without a decimal point are treated as int values.
Example: 10 is an int, but 10.0 is a float.
You can also use scientific notation (e.g., 1.5e2).
Example:
a = 1.5e2 # Equal to 150.0
print(a) # Output: 150.0
Type checking for float
Use the
type() function to check the data type.
Example:
a = 10.5
print(type(a)) # Output: <class 'float'>
Type conversion to float
Other data types can be converted to float using the
float() function.
Example:
a = float(10) # int to float conversion
print(a) # Output: 10.0
b = float("20") # string to float conversion
print(b) # Output: 20.0
a = 10
b = 10.5
print(a + b) # Output: 20.5
Note:
When an int and a float are added, the result is a float.
Complex Numbers
Python also supports complex numbers.
A complex number has a real part and an imaginary part.
In Python, complex numbers are written using
j or J.
The value of j represents √-1.
General form: a + bj
where a is the real part and b is the imaginary part.
Example: 3 + 4j
Example:
a = 1 + 2j
print(a) # Output: (1+2j)
Creating complex variables
a = 3 + 4j
b = -1 + 0j
c = 0 - 2j
Here
Variables a, b, and c store complex numbers.
Variables a, b, and c store complex numbers.
Type checking for complex
Use the
type() function to check the data type.
Example:
a = 1 + 2j
print(type(a)) # Output: <class 'complex'>
What We Have Learned
- Numbers in Python
- Strings in Python will be covered in the next topic