Fundamental of python : 1.Python Numbers
Fundamentals of Python
1. Python Numbers
Python supports three primary numeric types:
- int: Integer values (e.g.,
5,-10,100). - float: Floating-point numbers (e.g.,
2.5,3.14). - complex: Numbers with a real and imaginary part (e.g.,
2 + 5j).
These types are dynamically assigned when values are stored in variables. Python also supports arithmetic operations such as:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Exponentiation (
**)
Complex numbers have attributes:
'.real'– to access the real part'.imag'– to access the imaginary part
1. Integer
Program:
Explanation:
The variable a is assigned the value 5, which is an integer. When we use type(a), Python tells us the data type of the variable a, which is an integer (int).
2. Floating Point
Program:
Explanation:
Here, the variable b is assigned a floating-point number 2.5. Python recognizes this as a float type.
3. Long Integer (Python 2 Only)
Program:
Explanation:
In Python 2, appending L denotes a long integer type for very large numbers. In Python 3, this distinction is removed, and all integers are of type int regardless of size.
4. Complex Number
Program:
Explanation:
The variable y is a complex number, consisting of a real part 2 and an imaginary part 5j. The type() function confirms that the variable is of type complex.
5. Real & Imaginary Part of a Complex Number
Program:
Explanation:
'y.real' accesses the real part of the complex number y, which is 2.0. & 'y.imag' retrieves the imaginary part of the complex number y, which is 5.0.
6. Addition
Program:
Explanation:
Adding an integer (5) and a float (2.5) results in a float (7.5). Python automatically promotes the result to a float.
7. Subtraction
Program:
Explanation:
Subtracting a float from an integer results in a float. Python always promotes to the more precise type when needed.
8. Multiplication
Program:
Explanation:
Multiplying an integer by a float yields a float result. Python maintains numerical precision by converting to float.
9. Division
Program:
Explanation:
Division in Python always returns a float, even if both operands are integers (in Python 3). The result of 2.5 / 5 is 0.5.
10. Exponentiation (Power)
Program:
Explanation:
The ** operator raises a to the power of 2, which means 5 squared, resulting in 25.
If you found this post helpful, don’t forget to share it with your fellow learners, follow for more Python tips, and drop a comment below with your thoughts or questions! 🐍💬📢

Comments
Post a Comment