Python allows for the manipulation of different types of data. In this tutorial, we will learn how to store numbers in Python.
Steps
1. Integers
Integers are whole numbers, either positive or negative. They can be stored in Python using the int keyword. For example:
1 2 3 |
x = 10 y = -5 |
2. Floats
Floats are decimal numbers, for example, 3.14 or -0.5. They can be stored in Python using the float keyword. For example:
1 2 |
x = 3.14 y = -0.5 |
3. Complex Numbers
Complex numbers have a real and imaginary component, for example, 3 + 4j. They can be stored in Python using the complex keyword. For example:
1 2 |
x = 3 + 4j y = 2j |
4. Scientific Notation
Scientific notation is a way of expressing very large or very small numbers, for example, 3.5 x 10^4 or 2.4 x 10^-7. In Python, we can use e or E to indicate scientific notation. For example:
1 2 |
x = 3.5e4 # 3.5 x 10^4 y = 2.4E-7 # 2.4 x 10^-7 |
5. Converting Data Types
We can convert one data type to another using the int(), float(), or complex() functions. For example:
1 2 3 |
x = 10 y = float(x) # convert int to float z = complex(x) # convert int to complex |
Conclusion
In this tutorial, we have learned how to store different types of numbers in Python using keywords such as int, float, and complex.
We have also learned how to convert data types using the int(), float(), and complex() functions. Now that we know how to store numbers, we can start working on more complex programs that use mathematical operations.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
x = 10 y = -5 a = 3.14 b = -0.5 c = 3 + 4j d = 2j e = 3.5e4 # 3.5 x 10^4 f = 2.4E-7 # 2.4 x 10^-7 g = int(a) # convert float to int h = float(x) # convert int to float i = complex(x) # convert int to complex |